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

时间:2014-02-13 21:51:11

标签: c# javascript asp.net

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

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;
                }
            }

        } 

此代码工作正常。正如您在代码中看到的那样,它应该具有名为handlerevent的按钮,但处理程序永远不会被调用。现在当按钮被创建并且被点击时,它正在调用一个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,它将有一个用于编辑主题(按钮)数据库的表单值然后更新数据库,然后在更新之后,我将使用modalpopup重定向来回发页面进行刷新。

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

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

2 个答案:

答案 0 :(得分:1)

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

以下是best explanation之一。

答案 1 :(得分:0)

我发现了导致问题的原因,那就是JavaScript功能。

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

出于某种原因,我不知道,它不允许调用eventhandler。删除JavaScript后,按钮就能调用事件处理程序。也许它与按钮在OnclientClick上调用JavaScript函数并使按钮也调用事件处理程序有关。我不知道。 但它现在有效。

相关问题