How do you set up the OnClick with ASP 2.0 ?

G

guy

In my 1.1 project, I was creating dynamically a LinkButton, and stored
it into a Table.Row.Cell.Controls(). I was adding an MyOnClick handler
to the Click property. I was doing this during the Load phase. It
worked. Porting my code to 2.0, the Click event do not work anymore. It
is not generated in the intermediate c# files created by the
pre-compiling.

After some investiguation on the net, it appears that one must connect
a Click handler during the OnInit phase. But in that phase the
ViewState is empty, so I cannot re-create my controls. So this works
only if the control was on the postback page. Other posts seems to
indicate that one must now use the AddAttributes to generate an
"OnClick" attribute, calling a javascript, which will handle the detail
of the submit if I want a post back.

Is this the best way to generate a button click event handler, on a
dynamically created control ?

So many steps...
 
S

senfo

guy said:
In my 1.1 project, I was creating dynamically a LinkButton, and stored
it into a Table.Row.Cell.Controls(). I was adding an MyOnClick handler
to the Click property. I was doing this during the Load phase. It
worked. Porting my code to 2.0, the Click event do not work anymore. It
is not generated in the intermediate c# files created by the
pre-compiling.

After some investiguation on the net, it appears that one must connect
a Click handler during the OnInit phase. But in that phase the
ViewState is empty, so I cannot re-create my controls. So this works
only if the control was on the postback page. Other posts seems to
indicate that one must now use the AddAttributes to generate an
"OnClick" attribute, calling a javascript, which will handle the detail
of the submit if I want a post back.

Is this the best way to generate a button click event handler, on a
dynamically created control ?

So many steps...

Maybe I don't understand your problem, but the following code works fine
for me:

protected override void CreateChildControls()
{
LinkButton btn = new LinkButton();

btn.Text = "Click Me";
btn.Attributes.Add("onclick", "alert('Testing');");

this.form1.Controls.Add(btn);
}

I just placed that in my Default.aspx.cs file and it works just fine.

Am I missing something?

Hope that helps,
 
B

Brennan Stehling

I believe I understand what your problem is. You are binding a
collection to a GridView (or other databound control) and when you
click a button you want to handle that event as a PostBack. To do that
you can handle the RowCommand event on the GridView. That will provide
the LinkButton as the sender argument to the event handler. What you
want to do is attach the CommandName and CommandArgument so that button
so you can pull it from the sender.

To set the properties I handle the RowDataBound event. I use that to
get to the LinkButton when the DataItem is defined so I can set the
CommandArgument to a value which make sense for the event handler.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

If you are doing something other than a GridView, the technique is
similar. There should be a RowCommand event while the event to bind to
the row or item will be named differently.

This sounds a little confusing at first, but once you have done it a
few times it is easy. Let me know if you have any questions.

Brennan Stehling
http://brennan.offwhite.net/blog/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top