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.
Related
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.
i have a custom control which made by CodeBehind only (Class Library)
the control will create a asp button with a click event
i create the button in RenderControl method
and then set the event handler
Button m_oBTN = new Button();
m_oBTN.Text = "Submit";
m_oBTN.ID = "btnSubmit";
m_oBTN.CssClass = "btnSubmit";
m_oBTN.Click += new System.EventHandler(this.btnSubmit_Click);
however, it can success render the button, but no event has trigger when button click
do i need to use method like rasieEvent.....(i forgot name)?
how can i trigger the event when button is click?
Most probably because you are not setting the event handler correctly! This line should be:
m_oBTN.Click += new System.EventHandler(m_oBTN);
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.
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.
This is probably a simple question but I am not an ASP.NET developer and I am quite stuck.
I have a simple search routine that returns between zero and several hundred results. Each of these must be added to the page as a button and I want to set the text of the button and the CommandArgument property so that when the button is clicked I can read the CommandArgument back and react accordingly.
However, when I click the button the event does not run at all. How can I get it to run?
The code for building the button list (simplified for readability) is as follows:
foreach (SearchResult sr in searchResults)
{
Button result = new Button();
result.Text = sr.name;
result.CommandArgument = sr.ID.ToString();
AccountSearchResults.Controls.Add(result);
result.Click += new EventHandler(SearchResultClicked);
AccountSearchResults.Controls.Add(new LiteralControl("<br/>"));
}
At the minute to test, I have popped a label on the form to put the CommandArgument in. This code is never executed though.
void SearchResultClicked(object sender, EventArgs e)
{
Label1.Text = ((Button)sender).CommandArgument;
}
You mentioned in another answer that you are adding these when a button is clicked. Looking at your code, I would suggest that you try setting a unique ID for each button added, then ensure that on loading the page that buttons with the same IDs and CommandArgument values are reloaded. When a dynamically loaded button is clicked, it must still exist on the page after postback for the event to fire.
I think the ID is all you need, plus your requirement for the CommandArgument). You could put the ID information in the ViewState if you can't get it repeat without a long search process.
Where are you adding this buttons?
if you are adding them inside another control then the event might be raising in the parent control. This happens on DataRepeaters and DataGrids for example.
I think you need to use the OnCommand event handler, rather than the OnClick i.e. try changing this:
result.Click += new EventHandler(SearchResultClicked);
to this:
result.Command += new EventHandler(SearchResultClicked);
UPDATE
Try changing the type of second argument to your event hander from EventArgs to CommandEventArgs. You might also have to set the CommandName property on your button i.e.
result.CommandName = "foo";