asp.net动态按钮与事件处理程序不会工作

时间:2018-02-08 10:06:04

标签: javascript c# asp.net webforms

您好我在用户点击行时创建了触发事件的按钮。 当我在母版页上创建“隐藏”按钮时,它会起作用,但是当我在MainContent上创建这些按钮时,它不会触发事件:

List<Button> btnCollection = new List<Button>();

protected void OnRowTask(object sender, GridViewRowEventArgs e)
    {        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {


            string CellValue = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "id"));
            string urlAddress = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "url_address"));

            Button dummyBtn= new Button();
            dummyBtn.Attributes["style"] = "display:none;";
            dummyBtn.ID = "divBtn_" + CellValue;
            dummyBtn.Text = urlAddress;
            dummyBtn.Click += new EventHandler(this.rowButton_Click);
            PlaceHolderForHiddenBtn2.Controls.Add(dummyBtn);
            btnCollection.Add(divButton);

            e.Row.Attributes.Add("onclick", "return rowclick(" + CellValue + ");");

        }
        Session["hiddenBtnCollectionTask"] = btnCollection;
    }

点击活动:

private void rowButton_Click(object sender, EventArgs e)
    {
//some code to exectute
}

在页面加载时,我创建了在行创建的事件中添加的相同按钮:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["hiddenBtnCollectionTask"] != null)
        {
            List<Button> btnGenerateList = (List<Button>)Session["hiddenBtnCollectionTask"];
            foreach (Button btn in btnGenerateList)
            {
                PlaceHolderForHiddenBtn2.Controls.Add(btn);
            }
        }
}

应该触发button.click()的JS函数 - 事件:

function rowclick(id) {
            var button = document.getElementById('MainContent_divBtn_' + id);
            button.click();

            return false;
        }

有什么想法吗?

0 个答案:

没有答案