Error with the event handlers of dynamic linkbutto

2020-04-15 15:13发布

问题:

I am retrieving data from database depending upon the texboxes input and storing in a datatable , after then from datatable im sending data into dynamic table and displaying table in a panel,in the table all the data of the first column are of linkbuttons, i wrote event handler for dynamic link buttons , but the event handler is not triggering, and i want to store the linkbutton text in a string in the event handler, but the event handler is not triggering.

Code:

protected void Button1_Click(object sender, EventArgs e)
{
   // GridView1.
    DataTable dt = new DataTable();

    OleDbConnection con = new OleDbConnection(str);
    con.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = con;
    cmd.CommandText = "select ID,title,desc from [SO] where ID='" + TextBox1.Text.ToString() + "'or title='" + TextBox2.Text.ToString() + "'";
    OleDbDataAdapter db = new OleDbDataAdapter(cmd);
    db.Fill(dt);
    Table tb = new Table();
    tb.BorderColor = Color.Black;
    tb.BorderWidth = 2;

    DataRow dr;
    for (int i = 0; i < dt.Rows.Count; i++)
    {

        TableRow tr = new TableRow();
        tr.BorderColor = Color.Black;
        tr.BorderWidth = 2;
        tr.ID = "tr" + i;
        TableCell tc = new TableCell();
        tc.BorderColor = Color.Black;
        tc.BorderWidth = 2;
        tc.ID = "tc" + i;
        TableCell tc1 = new TableCell();
        tc1.BorderColor = Color.Black;
        tc1.BorderWidth = 2;
        tc1.ID = "tc1" + i;
        TableCell tc2 = new TableCell();
        tc2.BorderColor = Color.Black;
        tc2.BorderWidth = 2;
        tc2.ID = "tc2" + i;
        LinkButton t = new LinkButton();
        t.BorderColor = Color.Black;
        t.BorderWidth = 2;
        t.ID = "t" + i;
        t.Click += new EventHandler(t_edit);

        TextBox t1 = new TextBox();
        t1.BorderColor = Color.Black;
        t1.BorderWidth = 2;
        t1.ID = "t1" + i;
        TextBox t2 = new TextBox();
        t2.BorderColor = Color.Black;
        t2.BorderWidth = 2;
        t2.ID = "t2" + i;

        dr = dt.Rows[i];
        t.Text = Convert.ToString(dr["ID"]);
        t1.Text = Convert.ToString(dr["title"]);
        t2.Text = Convert.ToString(dr["desc"]);
        tc.Controls.Add(t);
        tc1.Controls.Add(t1);
        tc2.Controls.Add(t2);
        tr.Cells.Add(tc);
        tr.Cells.Add(tc1);
        tr.Cells.Add(tc2);
        tb.Rows.Add(tr);

    }
    Panel1.Controls.Add(tb);

}
protected void t_edit(object sender, EventArgs e)
{

}

k but by using the sessions concept im retrieving the total table so that the linkbuttons are also retrieving , and i want to add the linkbttons on a button click , here the problem is the eventhandler is not assiging to the linkbutton, and im adding linkbuttons on button click,not on page load.

回答1:

You must have to use Page_Init or Page_Load event handler to write code that create controls dynamically. Please read MSDN pages on How to add controls dynamically and ASP.NET Page life cycle articles.



回答2:

You can add event handlers to the Page_Load event but the important think to remember is that they must be added on every page load. It is common to do setup type tasks such as this in a !Page.IsPostBack clause. When wiring up event handlers that's not the case otherwise they will seem to disappear

if(!Page.PostBack)
{
    control.EventRaised += new EventHandler(EventResponse)
}

is wrong and will result in the handler disappearing on postback



标签: asp.net