Answered by:
Not able to fire the event on dynamically created checkbox in webforms

Question
-
User-534047397 posted
Hi ,
I have dynamically created the checkbox using Literal from code behind. It's not triggering any event / function.
I tried onclick , onserverchange methods. But these also not working. Please suggest any idea to resolve this problem.
Here is my code
public void CreateDynamicCheckbox() {
StringBuilder stbuilder= new StringBuilder();
for (int i = 1; i < 6; i++)
{
stbuilder.Append("<li><input type = 'checkbox' id= 'chk" + i.ToString() + "' class='side-checkbox' runat='server' onserverchange ='callfunction' value='" + i.ToString() + "'/></li>");
DivTagASPControl.Controls.Add(new Literal { Text = stbuilder.ToString() });
}
}
public void callfunction()
{
//some code
}Wednesday, July 18, 2018 11:38 AM
Answers
-
User-1171043462 posted
You cannot build Server Side controls using String concatenation.
You can create a dynamic CheckBoxList as shown below
HTML
<form id="form1" runat="server">
<asp:Panel ID = "Panel1" runat="server">
</asp:Panel>
</form>
Code
protected void Page_Load(object sender, EventArgs e) { CheckBoxList checkBoxList = new CheckBoxList(); for (int i = 1; i < 6; i++) { checkBoxList.Items.Add(new ListItem { Value = i.ToString(), Text = i.ToString() }); } checkBoxList.AutoPostBack = true; checkBoxList.SelectedIndexChanged += new EventHandler(CheckBoxList_SelectedIndexChanged); Panel1.Controls.Add(checkBoxList); } protected void CheckBoxList_SelectedIndexChanged(object sender, EventArgs e) { foreach (ListItem item in (sender as CheckBoxList).Items) { if (item.Selected) { string text = item.Text; string value = item.Value; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 18, 2018 1:06 PM -
User-369506445 posted
hi
your code is not correct,
if you want to call a code behind <g data-gr-id="260" id="260" class="gr_ gr_260 gr-alert gr_gramm Grammar only-ins replaceWithoutSep">method</g>, you have to use an asp control like <g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" id="14" data-gr-id="14"><g class="gr_ gr_142 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="142" data-gr-id="142">mudassarkhan</g></g> sample
if you want to call a <g class="gr_ gr_16 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del" id="16" data-gr-id="16">java script</g> function use below like :
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script> function callfunction(parameters) { alert(parameters); } </script> </head> <body> <form runat="server"> <ul> <div id="DivTagASPControl" runat="server" ></div> </ul> </form> </body> </html>
code behind
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CreateDynamicCheckbox(); } } public void CreateDynamicCheckbox() { StringBuilder stbuilder = new StringBuilder(); for (int i = 1; i < 6; i++) { stbuilder.Append("<li><input type = 'checkbox' id= 'chk" + i.ToString() + "' class='side-checkbox' runat='server' onclick ='callfunction("+i+");' value='" + i.ToString() + "'/></li>"); } DivTagASPControl.Controls.Add(new Literal { Text = stbuilder.ToString() }); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 18, 2018 1:41 PM
All replies
-
User475983607 posted
Correct, you cannot string build a server control as you've experienced. You must add the control to the markup or add the control in the code behind.
The recommended approach in web forms is to use a data binding control like a repeater to create dynamic controls as that's what data bound controls are for. The Getting Started tutorials on this site explain Web Forms fundamentals and common programming patterns.
https://www.asp.net/web-forms/overview/getting-started
Data bound controls.
Wednesday, July 18, 2018 11:50 AM -
User-1171043462 posted
You cannot build Server Side controls using String concatenation.
You can create a dynamic CheckBoxList as shown below
HTML
<form id="form1" runat="server">
<asp:Panel ID = "Panel1" runat="server">
</asp:Panel>
</form>
Code
protected void Page_Load(object sender, EventArgs e) { CheckBoxList checkBoxList = new CheckBoxList(); for (int i = 1; i < 6; i++) { checkBoxList.Items.Add(new ListItem { Value = i.ToString(), Text = i.ToString() }); } checkBoxList.AutoPostBack = true; checkBoxList.SelectedIndexChanged += new EventHandler(CheckBoxList_SelectedIndexChanged); Panel1.Controls.Add(checkBoxList); } protected void CheckBoxList_SelectedIndexChanged(object sender, EventArgs e) { foreach (ListItem item in (sender as CheckBoxList).Items) { if (item.Selected) { string text = item.Text; string value = item.Value; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 18, 2018 1:06 PM -
User-369506445 posted
hi
your code is not correct,
if you want to call a code behind <g data-gr-id="260" id="260" class="gr_ gr_260 gr-alert gr_gramm Grammar only-ins replaceWithoutSep">method</g>, you have to use an asp control like <g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" id="14" data-gr-id="14"><g class="gr_ gr_142 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="142" data-gr-id="142">mudassarkhan</g></g> sample
if you want to call a <g class="gr_ gr_16 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del" id="16" data-gr-id="16">java script</g> function use below like :
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script> function callfunction(parameters) { alert(parameters); } </script> </head> <body> <form runat="server"> <ul> <div id="DivTagASPControl" runat="server" ></div> </ul> </form> </body> </html>
code behind
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CreateDynamicCheckbox(); } } public void CreateDynamicCheckbox() { StringBuilder stbuilder = new StringBuilder(); for (int i = 1; i < 6; i++) { stbuilder.Append("<li><input type = 'checkbox' id= 'chk" + i.ToString() + "' class='side-checkbox' runat='server' onclick ='callfunction("+i+");' value='" + i.ToString() + "'/></li>"); } DivTagASPControl.Controls.Add(new Literal { Text = stbuilder.ToString() }); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 18, 2018 1:41 PM