简体   繁体   English

动态添加链接按钮和事件处理程序,处理程序不触发

[英]Dynamically adding link button and event handler, Handler not firing

Here in the code behind I am creating a link and adding a click event handler: 在下面的代码中,我正在创建链接并添加click事件处理程序:

LinkButton newX = new LinkButton();
newX.Text = "x";
newX.Attributes.Add("problem", problems[p]);
newX.Click += new System.EventHandler(this.RemoveItemFromBucket);

The link is appearing fine on the page. 该链接在页面上显示正常。 However, when I run in Debug mode and set a break point on the first line of the handler: 但是,当我在“调试”模式下运行并在处理程序的第一行上设置断点时:

public void RemoveItemFromBucket(object sender, EventArgs e)
        {
            string problem = (sender as LinkButton).Attributes["problem"];
...
        }

The event does not fire. 该事件不会触发。

Posting my load and PreInit code as requested: 根据要求发布我的负载和PreInit代码:

protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["elders"] == null)
            {
                Session["elders"] = (from s in masterDB.SnoMedElders select s).ToList();
            }

            if (Session["snoMed"] == null) {
                Session["snoMed"] = (from s in masterDB.mrconso_SnoMed2014_LimitedToDiseaseBranches select s).ToList();
            }

            if (Session["relations"] == null)                
            {
                Session["relations"] = (from s in masterDB.mrrel_SnoMed2014_LimitedToDiseaseBranches select s).ToList();
            }           
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserRole"] == null)
                Response.Redirect("Login.aspx");

            UnmappedNum.Text =  ((from t in (Session["elders"] as List<SnoMedElder>)
                                select t.SnoMedScui).Distinct().ToList().Count() -
                                (from t in masterDB.tbl_patients_problems_to_snomed_buckets_2014s
                                select t.SnoMedScui).Distinct().ToList().Count() + 600).ToString();
        }

Edit: Figured out the problem. 编辑:找出问题。 The problem is that my whole page is in an ajax update panel. 问题是我的整个页面都在ajax更新面板中。 When I add an element dynamically, it does not get added into the update panel so the whole page is reloading. 当我动态添加元素时,它不会添加到更新面板中,因此整个页面都在重新加载。 How do I add an element to an update panel? 如何将元素添加到更新面板?

You don't need to do it so low-level. 您不需要做得如此低级。

Just place that line control in the aspx: 只需将该行控件放在aspx中:

<asp:LinkButton runat="server" ID="btnTest" OnClick="btnTest_Click" Text="x"></asp:LinkButton>

...and in the code-behind: ...以及背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    btnTest.Attributes.Add("problem", problems[p]);
}

protected void btnTest_Click(object sender, EventArgs e)
{
    string problem = (sender as LinkButton).Attributes["problem"];
    //or even
    problem = btnTest.Attributes["problem"]
}

Think about this, if your page posts back, there is no button for an event to fire from. 考虑一下这一点,如果您的页面回发,则没有按钮可触发事件。 You need to add the link button in page oninit or page load 您需要在页面oninit或页面加载中添加链接按钮

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

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