PC Review


Reply
Thread Tools Rate Thread

Cheap Q: How to execute Javascript on a LinkButton

 
 
Matthias S.
Guest
Posts: n/a
 
      24th Mar 2005
Hi,

I've got a LinkButton on my webform. The Button, once clicked by the
user, should perform some time consuming operation. When the user
actually clicks the Button, I'd like to execute some JavaScript at the
client asking, if he is really sure that he wants to do that. Only if
the user clicks OK, the form should be passed back to the server.

The LinkButton is part of a simple ascx I've written in order to wrap up
functionality. Can somebody please point me into the right direction?

Thanks in advance!
--
/Matthias
 
Reply With Quote
 
 
 
 
Fernando Arámburu
Guest
Posts: n/a
 
      24th Mar 2005
On the Page_Load of the Code Behind put

this.MyLinkButton.Attributes["OnClick"] = "javascript: return
yourJavascriptFunction();";

That javascript function does what you want on the client browser and if you
want to stop the postback event, because the user click "Cancel", just
return false on the function and this will stop it.

I hope this help

Fernando Arámburu

"Matthias S." <postamt@_remove_emvoid_remove_.de> escribió en el mensaje
news:(E-Mail Removed)...
> Hi,
>
> I've got a LinkButton on my webform. The Button, once clicked by the
> user, should perform some time consuming operation. When the user
> actually clicks the Button, I'd like to execute some JavaScript at the
> client asking, if he is really sure that he wants to do that. Only if
> the user clicks OK, the form should be passed back to the server.
>
> The LinkButton is part of a simple ascx I've written in order to wrap up
> functionality. Can somebody please point me into the right direction?
>
> Thanks in advance!
> --
> /Matthias



 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      24th Mar 2005
In v1.1 you have to do this programitically. In v2.0 you can use the OnClientClick
property.

<form id="form1" runat="server">
<asp:LinkButton runat="server" ID="_foo" Text="DoWork"></asp:LinkButton>
</form>

protected void Page_Load(object sender, EventArgs e)
{
_foo.Attributes.Add("onclick", "return confirm('Yes?')");
}


-Brock
DevelopMentor
http://staff.develop.com/ballen



> Hi,
>
> I've got a LinkButton on my webform. The Button, once clicked by the
> user, should perform some time consuming operation. When the user
> actually clicks the Button, I'd like to execute some JavaScript at the
> client asking, if he is really sure that he wants to do that. Only if
> the user clicks OK, the form should be passed back to the server.
>
> The LinkButton is part of a simple ascx I've written in order to wrap
> up
> functionality. Can somebody please point me into the right
> direction?
> Thanks in advance!
>




 
Reply With Quote
 
dgk
Guest
Posts: n/a
 
      24th Mar 2005
On Thu, 24 Mar 2005 10:35:20 -0800, Brock Allen
<(E-Mail Removed)> wrote:

>In v1.1 you have to do this programitically. In v2.0 you can use the OnClientClick
>property.
>
> <form id="form1" runat="server">
> <asp:LinkButton runat="server" ID="_foo" Text="DoWork"></asp:LinkButton>
> </form>
>
> protected void Page_Load(object sender, EventArgs e)
> {
> _foo.Attributes.Add("onclick", "return confirm('Yes?')");
> }
>
>
>-Brock
>DevelopMentor
>http://staff.develop.com/ballen
>


So confirm is a function that will be downloaded from the .js file?
 
Reply With Quote
 
Fernando Arámburu
Guest
Posts: n/a
 
      24th Mar 2005
Me again, but in another question.
What a good advance in ASP.NET 2.0 OnClientClick
That´s good!!

Fernando Arámburu

"Brock Allen" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
> In v1.1 you have to do this programitically. In v2.0 you can use the

OnClientClick
> property.
>
> <form id="form1" runat="server">
> <asp:LinkButton runat="server" ID="_foo"

Text="DoWork"></asp:LinkButton>
> </form>
>
> protected void Page_Load(object sender, EventArgs e)
> {
> _foo.Attributes.Add("onclick", "return confirm('Yes?')");
> }
>
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
> > Hi,
> >
> > I've got a LinkButton on my webform. The Button, once clicked by the
> > user, should perform some time consuming operation. When the user
> > actually clicks the Button, I'd like to execute some JavaScript at the
> > client asking, if he is really sure that he wants to do that. Only if
> > the user clicks OK, the form should be passed back to the server.
> >
> > The LinkButton is part of a simple ascx I've written in order to wrap
> > up
> > functionality. Can somebody please point me into the right
> > direction?
> > Thanks in advance!
> >

>
>
>



 
Reply With Quote
 
Matthias S.
Guest
Posts: n/a
 
      24th Mar 2005
Thanks. That helped!

Matthias

Brock Allen wrote:
> In v1.1 you have to do this programitically. In v2.0 you can use the
> OnClientClick property.
>
> <form id="form1" runat="server">
> <asp:LinkButton runat="server" ID="_foo"
> Text="DoWork"></asp:LinkButton>
> </form>
>
> protected void Page_Load(object sender, EventArgs e)
> {
> _foo.Attributes.Add("onclick", "return confirm('Yes?')");
> }
>
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
>> Hi,
>>
>> I've got a LinkButton on my webform. The Button, once clicked by the
>> user, should perform some time consuming operation. When the user
>> actually clicks the Button, I'd like to execute some JavaScript at the
>> client asking, if he is really sure that he wants to do that. Only if
>> the user clicks OK, the form should be passed back to the server.
>>
>> The LinkButton is part of a simple ascx I've written in order to wrap
>> up
>> functionality. Can somebody please point me into the right
>> direction?
>> Thanks in advance!
>>

>
>
>


--
/Matthias
 
Reply With Quote
 
Brock Allen
Guest
Posts: n/a
 
      24th Mar 2005
confirm is an intrinsic javascript function -- you should be able to run
this sample "as-is".

-Brock
DevelopMentor
http://staff.develop.com/ballen



> On Thu, 24 Mar 2005 10:35:20 -0800, Brock Allen
> <(E-Mail Removed)> wrote:
>> In v1.1 you have to do this programitically. In v2.0 you can use the
>> OnClientClick property.
>>
>> <form id="form1" runat="server">
>> <asp:LinkButton runat="server" ID="_foo"
>> Text="DoWork"></asp:LinkButton>
>> </form>
>> protected void Page_Load(object sender, EventArgs e)
>> {
>> _foo.Attributes.Add("onclick", "return confirm('Yes?')");
>> }
>> -Brock
>> DevelopMentor
>> http://staff.develop.com/ballen

> So confirm is a function that will be downloaded from the .js file?
>




 
Reply With Quote
 
jasonkester
Guest
Posts: n/a
 
      25th Mar 2005
Brock Allen wrote:
> In v1.1 you have to do this programitically. In v2.0 you can use the

OnClientClick
> property.
>
> <form id="form1" runat="server">
> <asp:LinkButton runat="server" ID="_foo"

Text="DoWork"></asp:LinkButton>
> </form>
>
> protected void Page_Load(object sender, EventArgs e)
> {
> _foo.Attributes.Add("onclick", "return confirm('Yes?')");
> }
>
> [plus some AutoEventWireup magic to point that linkButton to

_foo_Click()
>


No offence intended to the quoted author here, but does anybody
actually write code that looks like this? The code below is
functionally equivilant, yet is readable as HTML:

<form id="form1" runat="server">
<a ID="_foo" onserverclick="_foo_Click" onclick="return
confirm('Yes?')" runat="server">DoWork</a>
</form>

I cannot understand why anbody would choose to use <asp:LinkButton>
over <a>, <asp:TextBox> over <input type=text>, <asp:label> over
<span>, etc. Every HTML tag is capable of taking a runat=server
attribure and exposing its DOM properties to the server. Why then,
would we use Microsoft proprietary equivilants for these things, when
we get little or no added functionality from them?

Could it possibly be for cross-platform compatability? Is there
actually a case where <asp:LinkButton...> will render anything other
than <a href=...>? Somehow I doubt it.

The only explaination I can come up with is that since the Drag & Drop
designer uses the <asp:...> tags, developers just assume that they
cannot use HTML in their ASPXs anymore. Laziness and undereducation?
This hardly sounds possible in all cases. The quoted author being an
evidence disproof, as he is clearly a skilled and knowledgeable .NET
developer.

Any comment?


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

 
Reply With Quote
 
JiangZemin
Guest
Posts: n/a
 
      25th Mar 2005
Hi,
if you have several of these tags and need to handle a bunch of attributes
for each of them, i think its cleaner and easier to maintain if you can put
all that in one section, rather than scattered all around the HTML. This
advantage isnt apparent when looking at simplified sample code of course.
I used to feel the same way you did (why not just keep it in the tags) but
then i had to maintain someone's elses code who did it with Attributes.Add
and it was much more pleasant.

JMHO,
JiangZemin

"jasonkester" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Brock Allen wrote:
>> In v1.1 you have to do this programitically. In v2.0 you can use the

> OnClientClick
>> property.
>>
>> <form id="form1" runat="server">
>> <asp:LinkButton runat="server" ID="_foo"

> Text="DoWork"></asp:LinkButton>
>> </form>
>>
>> protected void Page_Load(object sender, EventArgs e)
>> {
>> _foo.Attributes.Add("onclick", "return confirm('Yes?')");
>> }
>>
>> [plus some AutoEventWireup magic to point that linkButton to

> _foo_Click()
>>

>
> No offence intended to the quoted author here, but does anybody
> actually write code that looks like this? The code below is
> functionally equivilant, yet is readable as HTML:
>
> <form id="form1" runat="server">
> <a ID="_foo" onserverclick="_foo_Click" onclick="return
> confirm('Yes?')" runat="server">DoWork</a>
> </form>
>
> I cannot understand why anbody would choose to use <asp:LinkButton>
> over <a>, <asp:TextBox> over <input type=text>, <asp:label> over
> <span>, etc. Every HTML tag is capable of taking a runat=server
> attribure and exposing its DOM properties to the server. Why then,
> would we use Microsoft proprietary equivilants for these things, when
> we get little or no added functionality from them?
>
> Could it possibly be for cross-platform compatability? Is there
> actually a case where <asp:LinkButton...> will render anything other
> than <a href=...>? Somehow I doubt it.
>
> The only explaination I can come up with is that since the Drag & Drop
> designer uses the <asp:...> tags, developers just assume that they
> cannot use HTML in their ASPXs anymore. Laziness and undereducation?
> This hardly sounds possible in all cases. The quoted author being an
> evidence disproof, as he is clearly a skilled and knowledgeable .NET
> developer.
>
> Any comment?
>
>
> Jason Kester
> Expat Software Consulting Services
> http://www.expatsoftware.com/
>



 
Reply With Quote
 
Brock Allen
Guest
Posts: n/a
 
      25th Mar 2005
> No offence intended to the quoted author here, but does anybody
> actually write code that looks like this? The code below is
> functionally equivilant, yet is readable as HTML:


Well, for this situation, the question/sample was requested for a LinkButton.
There are times when the question asked is along the wrong path and thus
sometimes need readjusting. But then other times, for the sake of simplicity,
it is easier to accept the flawled lead and just answer the question to get
the dev up and running.

> I cannot understand why anbody would choose to use <asp:LinkButton>
> over <a>, <asp:TextBox> over <input type=text>, <asp:label> over
> <span>, etc. Every HTML tag is capable of taking a runat=server
> attribure and exposing its DOM properties to the server. Why then,
> would we use Microsoft proprietary equivilants for these things, when
> we get little or no added functionality from them?


There are times, though, when it is preferable to use a server side object
to do interesting work. Now of course, there are usually two choices between
HtmlControls and WebControls. WebControls, in my experience, tend to provide
a richer and more consistent API. Also, WebControls fire server side event
by default, so if I require a server event then these controls will be my
preference. When I am not interested in taking over tight control over the
client rendering/behavior, I go with WebControl. This is the majority of
the time I use server-side controls. The only time I choose HtmlControls
is when I need a server side object to initialize some data on the control
and I want control of the client side tag (with script typically) but then
from then on the control/tag will be client side only, meaning no postbacks.

> Could it possibly be for cross-platform compatability? Is there
> actually a case where <asp:LinkButton...> will render anything other
> than <a href=...>? Somehow I doubt it.


But as you've noted, at the end of the day, these things need to render as
good old HTML. For a simple control, you're right, there's little cross browser
compatibility. For other controls there is some merit to that argument. I
imagine you're aware of those other controls (and benefits).

> The only explaination I can come up with is that since the Drag & Drop
> designer uses the <asp:...> tags, developers just assume that they
> cannot use HTML in their ASPXs anymore. Laziness and undereducation?
> This hardly sounds possible in all cases.


So I don't follow what you're getting at? Why does it matter which control
you use? We've agreed that they both render as HTML. Why is it such an issue?
Each provides its own pros and cons based upon the situation upon which it's
being used and what the dev's trying to accomplish. As I mentioned above,
I prefer to use WebControls as they facilitate and ease my development (in
general).

-Brock
DevelopMentor
http://staff.develop.com/ballen



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FA: CHEAP CHEAP CHEAP HARD DRIVES! Qonox Storage Devices 4 24th Feb 2005 12:15 PM
FA: CHEAP CHEAP CHEAP HARD DRIVES! Qonox Computer Hardware 0 23rd Feb 2005 04:00 PM
FA: CHEAP CHEAP CHEAP HARD DRIVES! Qonox DIY PC 0 23rd Feb 2005 04:00 PM
FA: CHEAP CHEAP CHEAP 52X CDROM BURNER Qonox Computer Hardware 1 6th Feb 2005 02:43 AM
Execute javascript on a DataGrid's LinkButton click? =?Utf-8?B?RGF2ZQ==?= Microsoft ASP .NET 2 18th Jun 2004 04:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:51 PM.