繁体   English   中英

不使用动态创建的按钮触发按钮处理程序事件

[英]Button handler event not firing with dynamically created buttons

我正在根据数据库值动态创建按钮,这是生成按钮的代码。

test.GetSubjects();
        int subjectid = 0;

        // Current row count.
        int rowCtr;// = 0;
        // Total number of cells per row (columns).
        int cellCtr;
        // Current cell counter.
        int cellCnt;

        //count number of rows in dataset
        int rN = test.dsSubjects.Tables[0].Rows.Count;

        cellCnt = 4;


        for (rowCtr = 1; rowCtr <= rN; rowCtr++)
        {
            // Create a new row and add it to the table.
            TableRow tRow = new TableRow();
            Table1.Rows.Add(tRow);

            for (cellCtr = 1; cellCtr <= 4; cellCtr++)
            {
                //
                Button button = new Button();
                //
                HyperLink link = new HyperLink();
                // Create a new cell and add it to the row.
                TableCell tCell = new TableCell();

                button.Click += ButtonClick;
                /* If the rowcounter is equal to the record numbers
                 * then it has to break because if not it will throw an error
                 * saying that there is no row at ending position */

                if (rowCtr == rN)
                    break;

                string myStr = test.dsSubjects.Tables[0].Rows[rowCtr - 1]["SubjectName"].ToString();
                int myID = Convert.ToInt32(test.dsSubjects.Tables[0].Rows[rowCtr - 1]["SubjectID"].ToString());

                button.ID = Convert.ToString(myID);
                button.Text = myStr;
                //button.PostBackUrl = "~/WebForm2.aspx?SubjectID=" + myID;
                button.CssClass = "DynamicButtonOverlay";
                button.OnClientClick = " return ShowModalPopup()";
                tCell.Controls.Add(button);

                tCell.CssClass = "DynamicButtonOverlay";
                tRow.Cells.Add(tCell);
                rowCtr++;
                /* If the cellcount is 3 then it needs to break, if not then 
                 * you'll miss every 4rth record, don't know why. But this works */

                if (cellCtr == 4)
                {
                    rowCtr = rowCtr - 1;
                    break;
                }
            }

        } 

此代码可以正常工作。 正如您在代码中看到的那样,它应该具有称为处理程序事件的按钮,但永远不会调用处理程序。 现在,当创建按钮并单击按钮时,它正在调用javascript函数以显示ajaxmodalpopup,这是javascript代码

<script type="text/javascript">
    function ShowModalPopup() {
        $find("mpe").show();
        return false;
    }
    function HideModalPopup() {
        $find("mpe").hide();
        return false;
    }

我制作的事件处理程序是这样的。

private void ButtonClick(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        Label1.Text = "howdy";
    }

这个处理程序用于测试,这就是为什么我让它看起来像它那样。

我在处理程序上设置了一个断点,但是当我单击一个按钮时,它根本没有调用处理程序,我也不知道为什么。 我需要这些按钮中的任何一个来运行事件,因为调用modalpopup时,我将把按钮id(这是数据库中的id)传递给modalpopup,它将有一个用于编辑subject(button)数据库的形式值,然后更新数据库,然后在更新后,我将使用modalpopup重定向来回发页面以刷新。

所以有两个问题:按钮没有调用处理程序,如果我可以使它工作,那么我需要获取被调用按钮的ID,以便可以填充modalpopup中的字段。

按钮的生成是在pageload事件上完成的。

根据添加控件的时间,您将必须在Page Init或OnLoad的每次回发中重新创建控件。

这是最好的解释之一

我发现了导致此问题的原因,这是JavaScript函数。

<script type="text/javascript">
function ShowModalPopup() {
    $find("mpe").show();
    return false;
}
function HideModalPopup() {
    $find("mpe").hide();
    return false;
}

出于某种原因(我不知道),它不允许调用事件处理程序。 删除JavaScript后,按钮便可以调用事件处理程序。 可能与使按钮在OnclientClick上调用JavaScript函数以及使按钮还调用事件处理程序有关。 我不知道。 但现在可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM