Adding events programmatically is not working .....

tengstedt

New member
Joined
Mar 24, 2003
Messages
2
Hi,
I am trying to create an ASP.NET table programmatically and I want some of the cells to contain a LinkButton with an OnClick event handler associated with it.

The thing is that when I add the event handler programmatically it does not work, but adding it manually on a static LinkButton on the page itself it works fine. (Simplified examples below)

Has anyone experienced the same? and found a solution?

Kind regards,
Lars Betak Tengstedt
Edlund A/S, Denmark

THIS WORKS (DoStuff is executed):
===========

mytable.aspx:
-------------

<asp:table>
<asp:tablerow>
<asp:tablecell>
<asp:linkbutton OnClick=DoStuff>My Link</asp:linkbutton>
</asp:tablecell>
</asp:tablerow>
</asp:table>

mytable.aspx.cs:
----------------
protected void DoStuff(object sender, System.EventArgs e){...}

THIS DOES NOT WORK (DoStuff is NOT executed) :(
=====================

mytable.aspx:
-------------

<asp:table id=mytable>
<asp:tablerow>
<asp:tablecell>
</asp:tablecell>
</asp:tablerow>
</asp:table>


mytable.aspx.cs:
----------------
Page_Load(...)
{
LinkButton myLB = new LinkButton();
myLB.ID = "myLB";
myLB.Click += new System.EventHandler(this.DoStuff);
myLB.Text = "My Link";
myLB.EnableViewState = true;
mytable.Rows[0].Cells[0].Controls.Add(myLB);
}

protected void DoStuff(object sender, System.EventArgs e){...}
 
You need to create a sub/function and reference it as

vb.net (should be similiar for c#)
AddHandler dg.EditCommand, DoThis

Where dg.EditCommand is whatever you want to set off the handler, and DoThis is the name of the function

Hope that helps some.

Alternately, you could just put
url.com onClick=DoThis>Click here to do whatever</a>

Then add the JavaScript to page
Dim strLit As New LiteralControl
strLit.Text = "<script>DoThis{}</script>"
 
Tengstedt:
Has anyone experienced the same? and found a solution?

Your code looks correct, all except for the placement of;
LinkButton myLB = new LinkButton();

This should be global to your class. Putting it inside the Page_Load event would destroy it after that event is done executing. This is probably your problem. Make myLB a member variable inside your class...

C#:
LinkButton myLB;
void Page_Load() {
   myLB = new LinkButton();
   myLB.Click += new EventHandler(DoStuff);
   // etc..
}

If that isnt the problem then the only other thing I can think of is that adding the control dynamically is effecting it strangely. Try using regular HTML to place the LinkButton on the form, and see if it works with the event handler that way (without the OnClick, obviously).

vb.net (should be similiar for c#)
AddHandler dg.EditCommand, DoThis

If memory serves correctly, there is no AddHandler or Handles in C#. C# has to do all events through delegates.

Alternately, you could just put
url.com onClick=DoThis>Click here to do whatever</a>

He has that in his first example, but hes trying to do it dynamically.

Then add the JavaScript to page
Dim strLit As New LiteralControl
strLit.Text = "<script>DoThis{}</script>"

You can do this without JavaScript.
 
Answer found

I found the answer to the question myself.

It seems I left out one important detail - the creation of the table and thereby the controls also was actually put in an if(!Page.IsPostBack) like this:

Page_Load()
{
if(!Page.IsPostBack)
CreateTableWithControls();
}

OnClickButton()
{
DoStuff...
CreateTableWithControls(); // This time knowing the link was clicked
}

The CreateTableWithControls() has to be executed in PAGE_LOAD (or before) or else the event handler will not work !

It seems strange to me that you HAVE to create the table and the controls in Page_Load. It would be nice to create the table when you know what the user input is!

Well, I found an answer and seems like I will have to take action accrodingly.

Thank for the answers,
Lars
 
Back
Top