开发者

Programmatically assigned eventhandler not showing up in HTML onClick attribute

I'm currently having a problem with event handlers assigned in the code-behind not appearing in the HTML code of the .aspx page. Below is the code fragment that does that:

HtmlTable table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();

foreach(XmlNode item in IDList)
{
    LinkButton btn = new LinkButton();
    btn.Text = item.InnerText;
    btn.Click += new EventHandler(btn_Click);

    HtmlTableCell cell = new HtmlTableCell();
    cell.Controls.add(btn);
    row.cells.add(cell);
}

table.rows.add(row);

Something like that, more or less. So when I run the page and inspected the buttons using FireBug, I noticed that the eventhandler names for the buttons created have been changed to the LinkButtons' unique ID(ct125, 126开发者_JS百科, 127, etc), I think, and not "btn_Click"

I'm hoping to hear if anyone else has faced this problem before and found a solution for it. Thanks.


your buttons might be using submit behavior for your buttons. Try setting "UseSubmitBehavior=false" for the buttons.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx


When creating controls dynamically, you should assign them Id also, otherwise asp.net will auto-assign them ids like ctl{somenumber}. Also, you need to recreate these controls on each postback, otherwise your eventhandlers will not work.


I was able to get this code to work as expected. I dynamically add 4 LinkButtons and wire the Click event to a function btn_Click. When I click each link, they call the btn_Click method server-side:

protected void Page_Load(object sender, EventArgs e)
{
    HtmlTable table = new HtmlTable();
    HtmlTableRow row = new HtmlTableRow();

    for (int loop = 1; loop < 5; loop++)
    {
        LinkButton btn = new LinkButton();
        btn.Text = "Test " + loop.ToString();
        btn.Click += new EventHandler(btn_Click);

        HtmlTableCell cell = new HtmlTableCell();
        cell.Controls.Add(btn);
        row.Cells.Add(cell);
    }

    table.Rows.Add(row);

    // Add the table to a placeholder.
    phInputs.Controls.Add(table);
}

protected void btn_Click(object sender, EventArgs e)
{
    // Do something to catch a breakpoint.
    var x = 10;
}


Ok, I got it fixed, but I'm absolutely bewildered at why it got fixed this way.

{
    HtmlTable table = new HtmlTable();
    HtmlTableRow row = new HtmlTableRow();
    int btnIndex = 0;

    foreach(XmlNode item in IDList)
    {
        LinkButton btn = new LinkButton();
        btn.Text = item.InnerText;
        btn.Click += new EventHandler(btn_Click);
        btn.ID = "Btn_Page" + btnIndex.ToString();

        HtmlTableCell cell = new HtmlTableCell();
        cell.Controls.add(btn);
        row.cells.add(cell);
    }

    table.rows.add(row);
}

protected void Btn_Page_0(object sender, EventArgs e)
{
    whatever eventhandler code
}  

When I inspected the HTML again, the names of the event handler assigned to the buttons are still being changed to its ID.

<a id="Btn_Page_0" href="javascript:_doPostback('Btn_Page_0','')">

So for each of those buttons, their event handlers are changed to Btn_Page_0, Btn_Page_1, Btn_Page_2, etc.

Strange thing is, they all go back to the Btn_Page_0 event handler when clicked, instead of btn_click

So.....yeah. Kinda boggles my mind why it worked like this, but it works anyhow...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜