Fao, Sean
11/5/2006 1:07:00 AM
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