Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   How to pass parameters from the markup to the button onclick event code? (http://www.velocityreviews.com/forums/t695508-how-to-pass-parameters-from-the-markup-to-the-button-onclick-event-code.html)

AAaron123 08-20-2009 09:05 PM

How to pass parameters from the markup to the button onclick event code?
 
In a button onclick event I need two integers and a string from the aspx.

That is, these values are known in the aspx markup and I need to pass them
to the .vb onclick event.

How can I do that?


Thanks


PS
I been reading similar things with Google but can't find this answer there.


AAaron123 08-20-2009 10:21 PM

Re: How to pass parameters from the markup to the button onclick event code?
 
Mark Rae [MVP] wrote:
> "AAaron123" <aaaron123@roadrunner.com> wrote in message
> news:OeHMGodIKHA.3000@TK2MSFTNGP04.phx.gbl...
>
>> In a button onclick event I need two integers and a string from the
>> aspx. That is, these values are known in the aspx markup and I need to
>> pass them to the .vb onclick event.
>>
>> How can I do that?

>
> In the button's OnClientClick method add the values to an
> <asp:HiddenField /> control.
>
> Separate them with an "unusual" character e.g. "" which the
> server-side code can use to split them out again - .Split('')



I saw this on the Internet and tried it.


On the button element add
SomeInteger="78"



in .vb add

Public _SomeInteger As Integer

Public Property SomeInteger() As Integer

Get

Return _SomeInteger

End Get

Protected Set(ByVal Value As Integer)

_SomeInteger = Value

End Set

End Property



But the Set was never called.

Would some variation of this work?

Simply define a new attribute with a property?

Thanks, I'll try to do as you suggested.








AAaron123 08-20-2009 11:34 PM

Re: How to pass parameters from the markup to the button onclick event code?
 
Mark Rae [MVP] wrote:
> "AAaron123" <aaaron123@roadrunner.com> wrote in message
> news:%23qr1HSeIKHA.1252@TK2MSFTNGP04.phx.gbl...
>
>> Would some variation of this work?

>
> No.
>
>
>> Thanks, I'll try to do as you suggested.

>
> Good idea!


I can't get past getting this correct

OnClientClick="document.getElementById('<%=HiddenF ield1.ClientID%>' ).value='66';"

The button is inside a GridView which is inside a asp:Content



I've been able to get the HiddenField1 control in the code behind but
haven't been able to set the value in the aspx.



Thanks






AAaron123 08-21-2009 01:23 AM

Re: How to pass parameters from the markup to the button onclick event code?
 
Mark Rae [MVP] wrote:
> "AAaron123" <aaaron123@roadrunner.com> wrote in message
> news:OpS1P7eIKHA.3928@TK2MSFTNGP04.phx.gbl...
>
>> I can't get past getting this correct

>
> What does that mean...?


I don't know :-)

Guess I meant I couldn't find the correct syntax.

Seems like it should be simple for the Buton element to refer to the Hidden
element.
The lines are adjacent.
But getElementById does not return the element.
OnClientClick="document.getElementById('<%=HiddenF ield1.ClientID%>' ).value='66';"

Is it ok to use the CommandName and CommandArgument to pass data to the
click event?

I tried it and it appears to work OK.

Very simple to program.

Any reason not to do that?


Thanks




AAaron123 08-21-2009 12:37 PM

Re: How to pass parameters from the markup to the button onclick event code?
 
Mark Rae [MVP] wrote:
> "AAaron123" <aaaron123@roadrunner.com> wrote in message
> news:uk8ew3fIKHA.4708@TK2MSFTNGP03.phx.gbl...
>
>> Is it OK to use the CommandName and CommandArgument to pass data to
>> the Click event?

>
> The CommandName and CommandArgument are available to the Command
> event - I guess that's what you meant...


In a .aspx I have"
<asp:Button CssClass="ButtonAction" runat="server" Text="Show These Photos"
CommandName='<%# Eval("Caption") %>'

CommandArgument='<%# Eval("AlbumID") %>'
OnClick="ShowThesePhotos_Click"></asp:Button>



In the .aspx.vb I have:

LabelAlbumCaption.Text = "<b>" & DirectCast(sender,
System.Web.UI.WebControls.Button).CommandName & "</b>"

Also similar for CommandArgument.



And it works for me.

Which is why I asked if it is K to take advantage of that?

What do you think?



Thanks






AAaron123 08-21-2009 01:23 PM

Re: How to pass parameters from the markup to the button onclick event code?
 
I missed a line when I cut/paste.
I fixed it below.

AAaron123 wrote:
> Mark Rae [MVP] wrote:
>> "AAaron123" <aaaron123@roadrunner.com> wrote in message
>> news:uk8ew3fIKHA.4708@TK2MSFTNGP03.phx.gbl...
>>
>>> Is it OK to use the CommandName and CommandArgument to pass data to
>>> the Click event?

>>
>> The CommandName and CommandArgument are available to the Command
>> event - I guess that's what you meant...

>
> In a .aspx I have"
> <asp:Button CssClass="ButtonAction" runat="server" Text="Show These
> Photos" CommandName='<%# Eval("Caption") %>'
>
> CommandArgument='<%# Eval("AlbumID") %>'
> OnClick="ShowThesePhotos_Click"></asp:Button>
>
>
>
> In the .aspx.vb I have:
>


Protected Sub ShowThesePhotos_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

LabelAlbumCaption.Text = "<b>" & DirectCast(sender,
System.Web.UI.WebControls.Button).CommandName & "</b>"


....
> Also similar for CommandArgument.
>
>
>
> And it works for me.
>
> Which is why I asked if it is K to take advantage of that?
>
> What do you think?
>
>
>
> Thanks




AAaron123 08-21-2009 02:03 PM

Re: How to pass parameters from the markup to the button onclick event code?
 
Mark Rae [MVP] wrote:
> "AAaron123" <aaaron123@roadrunner.com> wrote in message
> news:%23GmEewlIKHA.1988@TK2MSFTNGP03.phx.gbl...
>
>>>> Is it OK to use the CommandName and CommandArgument to pass data to
>>>> the Click event?
>>>
>>> The CommandName and CommandArgument are available to the Command
>>> event - I guess that's what you meant...

>>
>> Which is why I asked if it is K to take advantage of that?
>>
>> What do you think?

>
> I think exactly the same as I thought a few hours ago, namely that the
> CommandName and CommandArgument properties are designed to work with
> the Command event:
>
> http://msdn.microsoft.com/en-us/libr...mmandname.aspx
> http://msdn.microsoft.com/en-us/libr...dargument.aspx
> http://msdn.microsoft.com/en-us/libr...on_events.aspx
>
> Obviously, you're free to do whatever you like...


A screwdriver is designed to drive screws but have you never opened a can of
paint with one? :-)
Just kidding. I now reaslise you were telling me you wouldn't do it. So I
won't either.

But as I read there appears to be no reason that I can't use the Command
event as freely as I use the Click event.
That's true isn't it?

Thanks a lot


AAaron123 08-21-2009 05:50 PM

Re: How to pass parameters from the markup to the button onclick event code?
 
Mark Rae [MVP] wrote:
> "AAaron123" <aaaron123@roadrunner.com> wrote in message
> news:ehE0xgmIKHA.5956@TK2MSFTNGP03.phx.gbl...
>
>> But as I read there appears to be no reason that I can't use the
>> Command event as freely as I use the Click event.
>> That's true isn't it?

>
> Yes but, as with all things, there's no need to use a sledgehammer to
> crack a nut.
>
> If you need nothing more than the properties and methods of the Click
> event, then use it.
>
> The Command event provides (essentially) all of the same basic
> methods as the Click event, but includes extra ones to extend the
> functionality.
> E.g. a typical use for this is when you have several buttons which
> perform very similar functions. You would then use the Command event
> rather than the Click event and use the CommandName and/or
> CommandArgument properties to branch the server-side code. In this
> way, you have several buttons but only one server_side Button_Command
> event. This helps to reduce repetition of code, the benefits of which
> are obvious...
> Whereas you could achieve similar functionality in any number of ways
> with the Click event (e.g. ((Button)sender).Text etc), the Command
> event is specifically designed for this...


The bottom line is, I tried to add data to a <asp:HiddenField/> control by
using the OnClientClick method of a button element but could not figure out
how to. I know it can be done but I can't do it. The button is in a <div>
which is in a <ItemTemplate> which is in a <asp:GridView>. I may have
missed one or two containers.

I either drop the feature or find another way to pass the data to an event
handler.

With your help I'll at least get the job done. Maybe not the best approach-
but working.

Thanks a lot



All times are GMT. The time now is 04:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.