I'm work on a project in Asp.net (C#), I've the following code (C#) that generate dynamically html buttons (for each item in my list) at run time:
In my .aspx file:
<div id="myDiv" runat="server" ...></div>
In my .aspx.cs file:
List<Item> list;
private void Display()
{
foreach (Item item in list)
AddButton(item);
}
private void AddButton(Item item)
{
HtmlButton button = new HtmlButton();
button.InnerText = item.Content;
button.ID = item.name;
button.ServerClick += new EventHandler(Item_Click);
myDiv.Controls.Add(button);
}
private void Item_Click(Object sender, EventArgs e)
{
// HERE I WANT TO DO SOMETHING ON THE SPECIFICALLY
// CLICKED BUTTON,
// THE PROBLEM IS THAT I CAN'T KNOW WHICH BUTTON OF
// THE DYNAMICALLY ADDED BUTTONS (BY THE AddButton()
// FUNCTION) IS CLICKED, NOTE: I HAVE MORE THAN 10 ITEMS IN
// list. I MEAN I CAN'T PASS THE CLICKED BUTTON AS A
// PARAMETER TO THIS Item_Click FUNCTION, IN GENERAL, WHAT I
// WANT IS SOMETHING LIKE:
// Item_Click(Object sender, EventArgs e, HtmlButton clickedButton)
// WITH SUCH DECLARATION OF Item_Click FUNCTION I CAN DO WHAT
// I WANT ON THE CLICKED BUTTON, BUT IT DOESN'T WORK THAT
// WAY, BECAUSE EventHandler's PARAMETERS ARE: Object,
// EventArgs, AND IT WOULD NOT HELP ME TO CREATE MY CUSTOM
// EVENTHANDLER BECAUSE HtmlButton.ServerClikc IS OF TYPE
// EventHandler.
}
Any suggestions?
Thanks for Help!
You must fill list in page_load without if(!IsPostBack)
void Page_Load(object sender, EventArgs e )
{
// don`t use if(!IsPostBack) because every postback your contols leave and you must register your controls.
list=new List<Item>();
list.Add(...);
list.Add(...);
list.Add(...);
}
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.