开发者

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 linkbut开发者_如何学Cton, 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


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.


in the HTML write the below

<asp:LinkButton runat="server" id="lnk1" text="INBOX" OnClick="lnkBtn_Click" />
<asp:LinkButton runat="server" id="lnk2" text="DRAFT" OnClick="lnkBtn_Click" />
<asp:LinkButton runat="server" id="lnk3" text="SENT" OnClick="lnkBtn_Click" />


Instead of using the text property use the source to find which button is clicked. Rest all looks fine to me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜