Re: How do you set up the OnClick with ASP 2.0 ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Init Handler Not Firing After Page_Init
    ... I'm having a problem trying to get an Init handler to fire for a Placeholder ... I tried to attach a literal control to a placeholder: ... The event handler receives an argument of type EventArgs. ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Init Handler Not Firing After Page_Init
    ... I'm having a problem trying to get an Init handler to fire for a Placeholder ... I tried to attach a literal control to a placeholder: ... The event handler receives an argument of type EventArgs. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Event handlers
    ... I am to find out some control I want to use ... MATLAB doesn't seem to be looking for the function ... It doesn't matter if I close MATLAB down and start again, on executing the above line I cannot get the event handler to be called. ...
    (comp.soft-sys.matlab)
  • Re: Custom Control - Notifcation Messages
    ... 'The function CWnd::OnNotify handles notification messages. ... Instead, you provide a handler ... Notify the parent. ... I just didn't realize that WM_NOTIFY was control specific. ...
    (microsoft.public.vc.mfc)
  • Re: How can I stop controls from executing default keyboard and mouse behavior?
    ... > acted upon even if the Ctrl keys were also being pressed. ... If the grid is not really "on" the page control, ... > handler needed if you press up arrow or down arrow when it has focus. ...
    (alt.comp.lang.borland-delphi)