[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

How do you set up the OnClick with ASP 2.0 ?

guy

11/1/2006 1:43:00 PM

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...

4 Answers

guy

11/4/2006 2:39:00 PM

0

And the answer is:

You have to put

guy

11/4/2006 2:43:00 PM

0

You have to add a Panel or PlaceHolder control on the page first.

Fao, Sean

11/5/2006 1:07:00 AM

0

guy wrote:
> 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,

--
Sean

offwhite

11/7/2006 6:27:00 PM

0

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.rowdata...

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...


guy wrote:
> 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...