简体   繁体   中英

Dynamically created linkbuttons' common event not firing

Whenever DropDownList SelectedIndexChanged, I am adding LinkButtons as ul-li list in codebehind. Each linkbuttons were assigned with IDs and a common Click event. Problem is code in Click event is not executed or maybe event is not triggered. My code below: [Edit] I tried like this as suggested in other posts ( dynamically created list of link buttons, link buttons not posting back )

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
   populate();
}

protected override void OnInit(EventArgs e)
{
   base.OnInit(e);
   populate();
}

void populate()
{
   HtmlGenericControl ulList = new HtmlGenericControl("ul");
   panel.Controls.Add(ulList);

   foreach (DataRow dr in drc) 
   {
      HtmlGenericControl liList = new HtmlGenericControl("li");
      ulList.Controls.Add(liList);

      var lnk = new LinkButton();
      lnk.ID = dr["col1"].ToString();
      lnk.Text = dr["col1"].ToString();
      lnk.Click += Clicked;
      liList.Controls.Add(lnk);
   }
}              

private void Clicked(object sender, EventArgs e)
{
   var btn = (LinkButton)sender;
   label1.Text = btn.ID.ToString();
}

Im missing something. Any help please.

Here the issue is with the ViewState . When the selected index of the dropdownlist changes there is a postback which takes place and the previous state is lost, so at this point you have to maintain the state of the controls.

Now in your code actually the state of the control is lost and so the click event does not fire. So the solution is to maintain the state of the controls.

The Below is a working example, you can just paste it and try.

This is my page load .

protected void Page_Load(object sender, EventArgs e)
        {
            for (var i = 0; i < LinkButtonNumber; i++)
                AddLinkButton(i);
        }

Similarly you have to maintain the state of the previously added control like this.

private int LinkButtonNumber
        {
            get
            {
                var number = ViewState["linkButtonNumber"];
                return (number == null) ? 0 : (int)number;
            }
            set
            {
                ViewState["linkButtonNumber"] = value;
            }
        }

The below is my SelectedIndexChanged Event for the DropDownList

protected void Example_SelectedIndexChanged(object sender, EventArgs e)
        {
            AddLinkButton(LinkButtonNumber);
            LinkButtonNumber++;
        }

I have a function which dynamically creates the controls, which is called on the page load and on SelectedIndexChanged .

private void AddLinkButton(int index)
        {
            LinkButton linkbutton = new LinkButton { ID = string.Concat("txtDomain", index) };
            linkbutton.ClientIDMode = ClientIDMode.Static;
            linkbutton.Text = "Link Button ";
            linkbutton.Click += linkbutton_Click;
            PanelDomain.Controls.Add(linkbutton);
            PanelDomain.Controls.Add(new LiteralControl("<br />"));
        }

And this is the Click event for the LinkButton

void linkbutton_Click(object sender, EventArgs e)
        {
            //You logic here
        }

I solved it using brute code lol. Since controls' event were not bound on postback then we recreate them on postback. So in my Page_Load I called the module that re-creates the controls, thus binding them to corresponding event. This works, but...

Re-creating these controls create duplicates ( Multiple Controls with same ID were found ) and you will get into trouble in instances of finding a control by ID like using panel.FindControl . To remedy this scenario, I put a check if same control ID already existed before recreating them, and voila! It works.

protected void Page_Load(object sender, EventArgs e)
{
   populate();
}

void populate()
{
   HtmlGenericControl ulList = new HtmlGenericControl("ul");
   panel.Controls.Add(ulList);

   foreach (DataRow dr in drc) 
   {
      HtmlGenericControl liList = new HtmlGenericControl("li");
      ulList.Controls.Add(liList);

      if (liList.FindControl(dr["col1"].ToString()) == null)
      {
          var lnk = new LinkButton();
          lnk.ID = dr["col1"].ToString();
          lnk.Text = dr["col1"].ToString();
          lnk.Click += Clicked;
          liList.Controls.Add(lnk);
      }
   }
} 

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