CheckedChanged event for checkbox not firing for dynamic checkboxes - asp.net

I seem to be having an issue getting the event handler for a group of dynamic checkboxes. The code is posted here. I thought this would be pretty straight forward, the checkboxes do not appear in a repeater, datagrid, etc. They appear in a table which is located inside a div which positioned in the center of the screen. Any help would greatly appreciated.
foreach (SelectAssignedRolesByUserResult role in assignedRoles)
{
CheckBox cb = new CheckBox();
cb.ID = string.Format("CheckBox_{0}_{1}", role.role_nm, role.role_id);
cb.Text = role.role_nm;
cb.Attributes.Add("role_id", role.role_id.ToString());
cb.Attributes.Add("assigned_role_id", role.assigned_role_id.ToString());
cb.Checked = (role.assigned_role_id > 0);
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
TableCell cell = new TableCell();
TableRow row = new TableRow();
cell.Controls.Add(cb);
row.Cells.Add(cell);
TableAssignedRoles.Rows.Add(row);
}

You don't mention where the code that dynamically adds the checkboxes is called. I'm guessing you put this in the Page_Load event handler, or in a sub that is called from within Page_Load.
If so, move it from Page_Load to Page_Init.
This is a very non-technical explanation of the reasoning for this:
This is because whether or not the controls are selected happens when the page parses the Viewstate. In the page lifecycle, the Page_Init loads the initial controls, then the viewstate is applied, and then the Page_Load fires.
Added
And don't forget to add
cb.AutoPostBack = true;

Can we see some more code? At what point in the lifecycle are you calling the above code?
If you're not recreating the checkboxes in exactly the same way on every postback, such that each Checkbox is assigned the same ID and can load ViewState properly, you'll lose your event handlers.

Related

Asp Checkboxlist Loses Values After PostBack

I have a checkboxlist control that I need to check the values of when a Submit button is clicked by the user onscreen(C#).
This checkboxlist is part of a user control that I am referencing within my page markup.
However, when I check the values of the checkboxlist within the code of the submit button, all of the values are gone (i.e it says there are no items at all in the checkboxlist control).
Anyone know why this would be happening? I am doing the exact same thing in code in another place with another checkboxlist user control and it works perfectly.
I don't have my exact code to hand but below is a simplified version of what I am doing.
Basically I am binding data to the Checkboxlist only when it is NOT a postback on the usedr control.
USER CONTROL WHICH CONTAINS ONLY THE CHECKBOXLIST CONTROL Page_Load()
If(!IsPostBack)
{
foreach(var item in myVals)
{
ListItem i = new ListItem();
i.Text = item.Text;
i.Value = item.Value;
i.Selected = false;
myCheckBoxListControl.Add(i);
}
}
Now I have a submit button function which checks the values in the checkboxlist...
SubmitButton_Click()
{
foreach(ListItem item in myCheckBoxListControl.Items)
{
// process each one here. The code never gets in here as there are never any items in the checkboxlist
}
}
Anyone know why the CheckboxList has lost all of its items by the time the submit button function gets executed? The checkboxlist has EnableViewState set to true.

Asp.net Dynamic link Button not raising event

I have a tabular data, in which at last column of every row a dynamic link button is added.
LinkButton link = new LinkButton();
link.Text = "Edit";
link.ID = dt.Rows[dt.Rows.IndexOf(dtRow)][0].ToString() + "|" + dt.Rows[dt.Rows.IndexOf(dtRow)][1].ToString();
link.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
cell.Controls.Add(link);
link.Click += new EventHandler(EditClicked);
The edit link is shown and on click it does the post back also But the event EditClicked is not fired at all.
Your problem is that you're dynamically creating your LinkButton and not recreating it again when your page is loaded.
If you dynamically create a control and then at postback, you don't create it again (in the Page_Load or preferably in the Page_Init) the event will not be fired.
One way to solve this is by using a hidden field:
When you dynamically create the linkbuttons, set a special value to a hidden field.
Then, in the Page_Load (in the if (IsPostback) ) check the hidden field, and if it has the special value - recreate all those controls again.

ImageButton inside cell table doesn't work

If I create ImageButton dynamically and add this into table cell
TableRow tr = new TableRow();
TableCell tc = new TableCell();
ImageButton imagebutton= new ImageButton();
imagebutton.imageurl=imageurl;
imagebutton.click+= new System.Web.UI.ImageClickEventHandler(click);
tc.Controls.Add(imagebutton);
tr.Controls.Add(tc);
Table1.Rows.Add(tr);
The click event doesnt' work.
However, if I use Panel instead of table the imagebutton works. Morevoer If I add imagebutton to the page directly it also works.
But I need store imagebutton in a table cell.
What is a problem?
How to make working imagebutton inside asp table?
The method click is:
protected void click(object sender, EventArgs e)
{
Response.Write("<script type='text/javascript'>alert('OK!');</script>");
}
What is a problem?
This seems to be a classic asp.net page life-cycle problem. It's cause is probably that, a the time the click should be handled, your button, as well as your table, do not exist because you create it later in the page life cycle (say in pre_render), or you only create when not on postback, or in another control event handler.
How to make working imagebutton inside asp table?
Your table and button creation , as well as click handler binding should be located in
page_load (even better, in Init, when using view_state for dynamic controls)
The code should run whether on postback or not.
Hope this will help

asp:Button Click event not being fired

I am dynamically adding rows in an asp table. In each row of the table I am also including a button which has a SelectProduct_Click event.
The problem is that even though I am registering the click event, the event is not being fired.
The button is being added in this way:
btnSelect = new Button();
btnSelect.ID = "btnSelect";
btnSelect.CommandArgument = od.ProductId;
btnSelect.Click += new EventHandler(this.SelectProduct_Click);
btnSelect.CssClass = "button";
btnSelect.Text = "Select";
cell = new TableCell();
cell.Controls.Add(btnSelect);
row.Cells.Add(cell);
How can I get my button to fire on click?
You need to learn about the ASP.NET page lifecycle.
In order for dynamic controls to fire their events on postback, they need to be recreated and attached to the event handler again.
The best place to create (and re-create) dynamic controls is in the OnInit event handler.
#Oded - you are absolutely right about the right timing to add dynamic controls. However it is not written on which event he is trying to add the button.

Calling the RowUpdate Method for dynamically created GridView

I have dynamically created a grid inside another grid. The parent grid is a static one created in aspx page and the child grid is created dynamically. Now, when a user clicks on the "Save" button in the Child grid I want the RowUpdating method to be triggered. Since I have created the Columns of the Child grid using TemplateGridView, I defined the Command name as follows in the TemplateGridView:
case ListItemType.Footer:
ImageButton BtnSave = new ImageButton();
BtnSave.ID = "Btn" + _columnName;
BtnSave.ImageUrl = "~/Images/saveIcon.jpg";
BtnSave.CommandName = "Update";
BtnSave.CausesValidation = true;
container.Controls.Add(BtnSave);
break;
Also, called the RowUpdating function where the ChildGrid's is created.
Tried the same with the RowCommand Argument too. But didn't work.
Kindly somebody help me on how to trigger an rowUpdate or rowcommand for a dynamically created grid.
NewDg.RowDataBound += new GridViewRowEventHandler(NewDg_RowDataBound);
NewDg.RowUpdating += new GridViewUpdateEventHandler(NewDg_RowUpdating);
NewDg.RowEditing += new GridViewEditEventHandler(NewDg_RowEditing);
NewDg.RowCommand += new GridViewCommandEventHandler(NewDg_RowCommand);
Thanks in advance.
Try using a CommandField for the button column instead of a TemplateField. That's probably the easiest solution.
I believe the RowUpdating event is fired when the CommandName is something like Update$RowIndex.
Where do you add the EventHandlers? Remember that for dynamically created controls, EventHandlers should be added in Page_Init/PreInit in order for .net to see the event being called.

Resources