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?

Byc
  • 183
  • 2
  • 11

2 Answers2

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"]
}
Arkadiusz Kałkus
  • 17,101
  • 19
  • 69
  • 108
  • Sorry, my fault, I oversimplified the question. I am adding and removing many of these at runtime. I need to do it dynamically. – Byc Dec 23 '14 at 21:06
  • 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? – Byc Dec 23 '14 at 21:07
  • Okay. I'll think about it. – Arkadiusz Kałkus Dec 23 '14 at 21:09
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

prospector
  • 3,389
  • 1
  • 23
  • 40
  • 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? – Byc Dec 23 '14 at 21:08
  • @Byc http://stackoverflow.com/questions/553073/adding-controls-dynamically-to-an-updatepanel-in-asp-net-ajax – prospector Dec 23 '14 at 21:17
  • 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); – Byc Dec 23 '14 at 21:25
  • I am trying the above with no luck. Any advice? – Byc Dec 23 '14 at 21:25