Search code examples
asp.netlinkbutton

3 linkbuttons one event handler


I am a beginner in asp.net, I've done my research but not very clear.

I have 3 links lkn1,2,3

Basically, I am looking at something like this:

protected void lnkBtn_Click(object sender,EventArgs e)
{
  LinkButton lnkRes = sender as LinkButton;
  string text = lnkRes.Text.Trim();
  string sql = ""
  if(text.ToUpper() == "INBOX")
  {
     sql = "SELECT * FROM InboxTbl where receiver_id = "helloworld";
  }
  else if(text.ToUpper() == "DRAFT")
  {
     sql = "SELECT * FROM Inbox where sender_id="HelloWorld";
  }
  else if(text.ToUpper() == "SENT")
  {
     sql = "SELECT * FROM Inbox where sender_id="HelloWorld";
  }
  if(sql != "")
  {
      SqlDataAdapter adp = new SqlDataAdapter(sql,ConnectionString);
      DataSet ds = new DataSet();
      adp.Fill(ds,"tbl");
      GridView1.DataSource = ds.Tables["tbl"].DefaultView;
      GridView1.DataBind();
  }
}

How do I write this code and where should I write it, so that depending on the text of the linkbutton, the respective sql statement is executed?

If this has anything to do with event handling.. 3links one event.. pls send me some links I could read and understand


Solution

  • Here is a link describing add event handlers. With event handling, when a certain action occurs, the response is to call a certain function (in our case lnkBtn_Click). For 3 different LinkButtons to use the same function, we just put the same function on the OnClick attribute - this is what makes the event handler for all three LinkButtons the same function.