Answered by:
Add a Listener from code

Question
-
User-1529238378 posted
Hello,
I wanted to know the way to add a listener like OnCheckedChanged on a CheckBox in the .cs file:
var attend = new CheckBox();
attend.ID = "chkbxSubmeeting_" + numSubmeetingsOnWeb;
//Add OnCheckedChanged and associate to a void (for attend CheckBox),
tCellDesc.Controls.Add(attend);
Friday, May 25, 2018 10:32 AM
Answers
-
User753101303 posted
Hi,
Most often with simple question start by looking at the doc. For example https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checkedchanged(v=vs.110).aspx#Examples show a code sample such as :
// Manually register the event-handling method for the // CheckedChanged event of the CheckBox control. checkbox1.CheckedChanged += new EventHandler(this.Check_Clicked);
Is this what you are looking for? It's best to create controls in Init: https://support.microsoft.com/EN-US/help/317794 (or once created they should be created again in Init). Also it's like better to avoid them unless you really need them.
For example with a single checkbox another option would be to render the control but keep it hidden in the HTML view (and show the control as needed) or you also have a Visible property that won't render any markup at all to the browser. If it depends on data you could also use https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 25, 2018 10:46 AM -
User283571144 posted
Hi Badbytes,
According to your description and codes, I found you don't add the autopostback attribute to the checkbox.
So the page will not postback if you check the checkbox.
I suggest you could add below codes to your checkbox, then it will work well.
attend.AutoPostBack = true;
Details codes as below:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace TestWebForm { public partial class Test : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { BuildSubMeetingTable(); } protected void Page_Load(object sender, EventArgs e) { } private void BuildSubMeetingTable() { string numSubmeetingsOnWeb = "aaa"; var tCellDesc = new TableCell(); var attend = new CheckBox(); attend.ID = "chkbxSubmeeting_" + numSubmeetingsOnWeb; attend.CheckedChanged += new EventHandler(this.Check_Clicked); attend.AutoPostBack = true; tCellDesc.Controls.Add(attend); var desc = new Literal(); desc.Text = @" " + "aaa"; tCellDesc.Controls.Add(desc); tRow.Cells.Add(tCellDesc); } protected void Check_Clicked(object sender, EventArgs e) { System.Diagnostics.Debug.WriteLine("salut"); } } }
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 26, 2018 3:50 AM
All replies
-
User753101303 posted
Hi,
Most often with simple question start by looking at the doc. For example https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checkedchanged(v=vs.110).aspx#Examples show a code sample such as :
// Manually register the event-handling method for the // CheckedChanged event of the CheckBox control. checkbox1.CheckedChanged += new EventHandler(this.Check_Clicked);
Is this what you are looking for? It's best to create controls in Init: https://support.microsoft.com/EN-US/help/317794 (or once created they should be created again in Init). Also it's like better to avoid them unless you really need them.
For example with a single checkbox another option would be to render the control but keep it hidden in the HTML view (and show the control as needed) or you also have a Visible property that won't render any markup at all to the browser. If it depends on data you could also use https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 25, 2018 10:46 AM -
User-1529238378 posted
Thanks for your help! i will consult the doc!
Friday, May 25, 2018 10:50 AM -
User-1529238378 posted
Unfortunately after some testing my code is not triggering The Check_clicked void.
void Check_Clicked(Object sender, EventArgs e)
{
// i m not seeing message in output Visual Studio
System.Diagnostics.Debug.WriteLine(e);
}
var attend = new CheckBox();
attend.ID = "chkbxSubmeeting_" + numSubmeetingsOnWeb;
attend.CheckedChanged += new EventHandler(this.Check_Clicked);
tCellDesc.Controls.Add(attend);
any Idea why?
Friday, May 25, 2018 11:26 AM -
User753101303 posted
Where is the code that creates the control ? Also it needs to be created each time the page runs (keep in mind that the page class is created from scratch for each new http request).
Also depending on the overall goal you might have a simpler solution (I believe you are trying to render/process a list of checkboxes based on data ?)
Friday, May 25, 2018 12:51 PM -
User-1529238378 posted
This is done like that :
protected void Page_Load(object sender, EventArgs e){ BuildSubMeetingTable(); } private void BuildSubMeetingTable( ){ #region first cell var tCellDesc = new TableCell(); var attend = new CheckBox(); attend.ID = "chkbxSubmeeting_" + numSubmeetingsOnWeb; attend.CheckedChanged += new EventHandler(this.Check_Clicked); tCellDesc.Controls.Add(attend); var desc = new Literal(); desc.Text = @" " + submeeting.Description; tCellDesc.Controls.Add(desc); tRow.Cells.Add(tCellDesc); #endregion #region second cell var tCellDate = new TableCell(); tCellDate.HorizontalAlign = HorizontalAlign.Center; var dates = new Literal(); if (submeeting.Start != "" && submeeting.Finish != "") // dates.Text = submeeting.Start.Substring(0, 10) + " - " + submeeting.Finish.Substring(0, 10); dates.Text = submeeting.Start.Substring(8, 2) + "/" + submeeting.Start.Substring(5, 2) + "/" + submeeting.Start.Substring(0, 4) + " - " + submeeting.Finish.Substring(8, 2) + "/" + submeeting.Finish.Substring(5, 2) + "/" + submeeting.Finish.Substring(0, 4); else dates.Text = @" "; tCellDate.Controls.Add(dates); tRow.Cells.Add(tCellDate); #endregion #region third cell // Sub meeting allows remote ? var tCellRemote = new TableCell(); tCellRemote.HorizontalAlign = HorizontalAlign.Center; if (submeeting.Remote == "true") { // enable header when at least one is remote enabled LabelRemote.Visible = true; var remote = new CheckBox(); remote.ID = "chkbxSubmeetingRemote_" + numSubmeetingsOnWeb; //remote.Enabled = false; tCellRemote.Controls.Add(remote); } tRow.Cells.Add(tCellRemote); #endregion }
Friday, May 25, 2018 1:16 PM -
User753101303 posted
What if you call that from Page_Init instead ? Also it is done for the first checkbox but not for the other one... For now it doesn't seems it needs to be really dynamic ?
Friday, May 25, 2018 1:45 PM -
User-1529238378 posted
i have done this:
void Check_Clicked(Object sender, EventArgs e) { System.Diagnostics.Debug.WriteLine("salut"); CheckBox chkSelectedSub = sender as CheckBox; GridViewRow row = chkSelectedSub.NamingContainer as GridViewRow; GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent; GridViewRow grdrow = (GridViewRow)((CheckBox)sender).NamingContainer; } protected void Page_Init(object sender, EventArgs e) { BuildSubMeetingTable(); } private void BuildSubMeetingTable( ) { #region first cell var tCellDesc = new TableCell(); var attend = new CheckBox(); attend.ID = "chkbxSubmeeting_" + numSubmeetingsOnWeb; attend.CheckedChanged += new EventHandler(this.Check_Clicked); tCellDesc.Controls.Add(attend); var desc = new Literal(); desc.Text = @" " + submeeting.Description; tCellDesc.Controls.Add(desc); tRow.Cells.Add(tCellDesc); #endregion }
Unfortunately System.Diagnostics.Debug.WriteLine("salut") print nothing on VStudio Output Console when i check/uncheck the checkboxes that have been created :((
Friday, May 25, 2018 3:02 PM -
User-1529238378 posted
tried a lot of others possibilities without sucess.
Any other help will be appreciated
Friday, May 25, 2018 5:29 PM -
User283571144 posted
Hi Badbytes,
According to your description and codes, I found you don't add the autopostback attribute to the checkbox.
So the page will not postback if you check the checkbox.
I suggest you could add below codes to your checkbox, then it will work well.
attend.AutoPostBack = true;
Details codes as below:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace TestWebForm { public partial class Test : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { BuildSubMeetingTable(); } protected void Page_Load(object sender, EventArgs e) { } private void BuildSubMeetingTable() { string numSubmeetingsOnWeb = "aaa"; var tCellDesc = new TableCell(); var attend = new CheckBox(); attend.ID = "chkbxSubmeeting_" + numSubmeetingsOnWeb; attend.CheckedChanged += new EventHandler(this.Check_Clicked); attend.AutoPostBack = true; tCellDesc.Controls.Add(attend); var desc = new Literal(); desc.Text = @" " + "aaa"; tCellDesc.Controls.Add(desc); tRow.Cells.Add(tCellDesc); } protected void Check_Clicked(object sender, EventArgs e) { System.Diagnostics.Debug.WriteLine("salut"); } } }
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 26, 2018 3:50 AM -
User-1529238378 posted
Thanks a lot Brando !!
It helps me a lot
Monday, May 28, 2018 8:02 AM