Hi all,
I'm creating some asp buttons dynamicly and I associated them an event handler
btnDel.Click += new EventHandler(btnDel_Click);
but the first time I click on the button goes to the Page_load but the btnDel_Click is not fired.
Somebody knows the reason
Thanks in advance.
P.D.: Sorry for my english.

dynamic asp button click doesn't work first time
Lejing
Thanks for you answer, but I have an asp table where I'm adding these asp buttons, so I think it's no neccesary to add them to the page (this).
The question is that the event isn't firing the first time I click on the button, if I click twice it's goes ok.
Learning VB
Feel free to correct me if I'm wrong. I'm far from an asp.net expert.
Well, yes. And no. Normally asp.net wires events in page_init(). Though, in this particuluar case the buttons arn't initialized when the page_init() is executed. And naturally therefor no wiring can be done that early. Assigning the event in the page_load() method does work (as i shown in my sample code in a previous post).
Therefor, I'm quite positive that the event is actually triggered, but the user doesn't see any affect until he refreshes the page.
uma kadagi
This sounds like a postback problem to me.
In what method are you associating your event
For future reference. You will probably get better answers in: http://forums.asp.net
MerrickDeWitt
Hi,
You will also required to add control to your page's control collection after you load control in memory.
Add this piece of code.
this.Controls.Add(controlname);
Hope this should help.
Thanks
ILoveOregon
Hi all,
Here is the snippet of the code:
TableCell colDel = new TableCell();
btnDel.Click += new EventHandler(btnDel_Click);
colDel.Controls.Add(btnDel);
fila.Cells.Add(colDel);
Table1.Rows.Add(fila);
where Table1 is an asp table and btnDel is an .asp button (not a custom one).
the Event handler is the next one:
protected void btnDel_Click(object sender, EventArgs e){
.....
}
Thanks to all.
Eran Kampf
Ahh, sorry... Not sure this is of any help to you, but i wrote the following, and it works for me:
public
partial class dummy : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
for (int i = 0; i < 10; i++){
Button btn = new Button();btn.ID = i.ToString();
btn.Text = i.ToString();
btn.Click +=
new EventHandler(btn_Click);PlaceHolder1.Controls.Add(btn);
}
}
void btn_Click(object sender, EventArgs e){
Response.Redirect(
http://msdn.microsoft.com);}
}
Are you absolutely sure your event isn't triggered I mean, in case it's triggered but you just dont see the results of it until you refresh the page !
Jason D. Camp
Hi,
Can u give me snippet for adding control. Are you using any .net control or made any custom control.
newbiedogg
I think also that can be a postback problem but I don't know why only the first time
P.D.: Thanks for the page http://forums.asp.net
digitalmercenary
Hi,
Whenever one is assigning event handler, it should be done prior to page load ideally. I think when you are assigning a event handler, that does not happen first time when page loads but when u do something, control gets added to the table and then you assign event handler. So somehow in ur code, u will have to make sure that each time page loads, event handler code should be executed. You can say this is postback problem.
Try to find right place to put event assignment code.
Regards
InstantKarma
I am going to see if I'm assigning the event in the correct part, but as I mentioned at the begining is a dynamic button, there is not only one button, it depends on some data stored in the DB, so I can't use <asp:Button ID="btnDel" runat="server" Text="Delete" OnClick="btnDel_Click" />, because I don't know how many button I have to add.
Thanks.
arikve
I may be wrong, but I'm quite sure you assign the event after the page is initialized. That way nothing will happen the first time you click the button (other than the page being posted back). The second time you click it, it'll work since the event was assigned after the first page load.
Sounds confusing enough
Either you could look into the page event order of asp (googling for page_init page_load page_unload would probably give you a hint)
Or the easier way...
Let asp.net hande the event wiring. by simply removing your btnDel.Click += new EventHandler(btnDel_Click);
and in the .aspx file, add a OnClick attribute to your button. May look something like this:
<
asp:Button ID="btnDel" runat="server" Text="Delete" OnClick="btnDel_Click" />Did that work
F.Costa
I have solved this problem by changing the asp button to html button, and adding them an "id" instead of taking the .net one ("ctl_1", ...).
I realized also, that the only the first button hadn't the click event associated the first time. Also, the others were taking the arguments of the previous button, i.e., the second button was taking the arguments of the first button, the third one was taking the second one arguments, and so on.
Well, I don't why this was happening but I know that now is correct.
Thanks to all.
Bye.
Victor.