Ask a Question related to ASP.NET Building Controls, Design and Development.
-
guy #1
How do you set up the OnClick with ASP 2.0 ?
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...
guy Guest
-
onClick question
I have a list menu in a form where I want to submit the form when option value is clicked. -
Onclick event
I would like for certain Contribute users to be able to add on onclick event on links. How can they do this ? Thanks! -
onclick
What is going with mine code ??. When I select page2 from radio button, I should have "form2" display on the page but it seems to me nothing... -
cfgrid and onclick
I have a cfgrid in select mode row. On entering the page is no row selected. So I have a form button which is deactivated by default. Now, if the... -
OnClick problem
Here's something I cant explain: I have a page with a form on it (checkboxes) and a hyperlink which is supposed to send the form to the server... -
guy #2
Re: How do you set up the OnClick with ASP 2.0 ?
And the answer is:
You have to put
guy Guest
-
guy #3
Re: How do you set up the OnClick with ASP 2.0 ?
You have to add a Panel or PlaceHolder control on the page first.
guy Guest
-
senfo #4
Re: How do you set up the OnClick with ASP 2.0 ?
guy wrote:
Maybe I don't understand your problem, but the following code works fine> 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...
>
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
senfo Guest
-
Brennan Stehling #5
Re: How do you set up the OnClick with ASP 2.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.
[url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.as px[/url]
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
[url]http://brennan.offwhite.net/blog/[/url]
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...Brennan Stehling Guest



Reply With Quote

