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);
Related
I have an ASP.NET WebForms page with several buttons added programmatically like this:
private void AddExportButton(Control control, Action clickAction) {
LinkButton exportButton = new LinkButton {
Text = "Export",
EnableViewState = false /*Buttons will be recreated on each Postback anyway.*/
};
exportButton.Click += (sender, e) => clickAction();
control.Controls.Add(exportButton);
}
Now this works, as long as the AddExportButton() method is called along the path from the OnLoad() or OnPreLoad() method. It does not fire the handler action however, when AddExportButton() called from the OnLoadComplete() method.
I would like to add/create the buttons also when another event handler (coming from a dropdown) gets called. This only happens after the OnLoad(), which will break my code.
Why is this, and how can I use anonymous methods as event handlers in this case?
See this nice cheat sheet about the ASP.NET Page LifeCycle by Léon Andrianarivony for more info about the order of the page/control creation.
In the page life cycle, the internal RaisePostBackEvent method (which raises the button's Click event) occurs between OnLoad and OnLoadComplete. If you wait until OnLoadComplete to add the LinkButton and hook up its Click event, then obviously the event won't be raised: it's too late.
(The fact that you're using an anonymous method is irrelevant.)
Can you add the export button in the .aspx but set its Visible property to false when you don't want it to appear?
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 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.
I want to create a asp.net button control / link button control when user makes an ajax call to my server page (another asp.net page) and I wnt to do something on the button click event of this button. How to do this ? Do i need to use delegate ?
No, you don't need a delegate for this, it will be created for you. In your AJAX callback you should do something like this:
Button btn = new Button();
btn.Click += MyOnClickMethod; // must use +=, not =
btn.Text = "click me";
myUpdatePanel.Controls.Add(btn); // the AJAX UpdatePanel that you want to add it to
myUpdatePanel.Update(); // refresh the panel
The method MyOnClickMethod must have the same signature as a normal Click handler. Something like this will do:
protected void MyOnClickMethod(object sender, EventArgs e)
{
// do something
}
that's about it. There are many intricacies involved with dynamic controls, but the basis is as laid out above.
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";