Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want execute server click event method in C#, asp.net. i create dynamically id for delete button,but its not calling that server event and its not working for me currently, but it goes to page load event, but not calling that onserverclick button event. in click event i write set of code that has be called.

C#
String sFinal=""; 
String sSlider = "<button id=<bid> runat=\"server\" onserverclick=\"btnDelete_ServerClick\" >Delete</button>";
                                    while (i < dt.Rows.Count)
                                    {
                                        sFinal = sFinal + sSlider.Replace("<bid>", "btn" + i.ToString());
                                        i++;
                                    }
Posted
Updated 6-May-15 2:09am
v2

Html buttons do not have a onserverclick method. See http://www.w3schools.com/tags/tag_button.asp[^] You could set the type to submit for it to post the page back.

Or, you can dynamically add a button but this is done by using C# code, not building a string. Google for "c# dynamically add button."
 
Share this answer
 
For the ASP.NET engine to recognize your buttons you have to add them to the object model of your page and not as a string...
C#
while (i < dt.Rows.Count)
{
  Button oBtn = new Button();

  oBtn.ID = string.Format("btn_{0}", i);
  oBtn.Click += new EventHandler(btnDelete_ServerClick);
  // set any other property here...

  Page.Controls.Add(oBtn);
  i++;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900