简体   繁体   中英

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:

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:

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. 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:

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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