0

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?

1
  • show your page load and page oninit code Commented Dec 23, 2014 at 21:01

2 Answers 2

0

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"]
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, my fault, I oversimplified the question. I am adding and removing many of these at runtime. I need to do it dynamically.
That said, I figured out my problem, now I need to figure out the solution. 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?
Okay. I'll think about it.
0

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

4 Comments

yeah I just figured this out. The rest of the page is in an AJAX update panel. How do I add to it so that these events are AJAX?
LinkButton newX = new LinkButton(); UpdatePanel1.ContentTemplateContainer.Controls.Add(newX); newX.Text = "x"; newX.Attributes.Add("problem", problems[p]); newX.Click += new System.EventHandler(this.RemoveItemFromBucket);
I am trying the above with no luck. Any advice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.