How to pass parameters from the markup to the button onclick event code?

Discussion in 'ASP .Net' started by AAaron123, Aug 20, 2009.

  1. AAaron123

    AAaron123 Guest

    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, Aug 20, 2009
    #1
    1. Advertising

  2. AAaron123

    AAaron123 Guest

    Mark Rae [MVP] wrote:
    > "AAaron123" <> wrote in message
    > news:...
    >
    >> 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, Aug 20, 2009
    #2
    1. Advertising

  3. AAaron123

    AAaron123 Guest

    Mark Rae [MVP] wrote:
    > "AAaron123" <> wrote in message
    > news:%...
    >
    >> 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('<%=HiddenField1.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, Aug 21, 2009
    #3
  4. AAaron123

    AAaron123 Guest

    Mark Rae [MVP] wrote:
    > "AAaron123" <> wrote in message
    > news:...
    >
    >> 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('<%=HiddenField1.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, Aug 21, 2009
    #4
  5. AAaron123

    AAaron123 Guest

    Mark Rae [MVP] wrote:
    > "AAaron123" <> wrote in message
    > news:...
    >
    >> 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, Aug 21, 2009
    #5
  6. AAaron123

    AAaron123 Guest

    I missed a line when I cut/paste.
    I fixed it below.

    AAaron123 wrote:
    > Mark Rae [MVP] wrote:
    >> "AAaron123" <> wrote in message
    >> news:...
    >>
    >>> 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, Aug 21, 2009
    #6
  7. AAaron123

    AAaron123 Guest

    Mark Rae [MVP] wrote:
    > "AAaron123" <> wrote in message
    > news:%...
    >
    >>>> 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/library/system.web.ui.webcontrols.button.commandname.aspx
    > http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
    > http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button_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, Aug 21, 2009
    #7
  8. AAaron123

    AAaron123 Guest

    Mark Rae [MVP] wrote:
    > "AAaron123" <> wrote in message
    > news:...
    >
    >> 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
    AAaron123, Aug 21, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    1,884
    Guest
    Aug 18, 2007
  2. Bogdan
    Replies:
    2
    Views:
    1,717
    Eliyahu Goldin
    May 21, 2009
  3. Zalek Bloom

    How to pass 2 parameters with OnClick?

    Zalek Bloom, Nov 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    743
    Roland Hall
    Nov 5, 2003
  4. Replies:
    2
    Views:
    243
  5. SM
    Replies:
    2
    Views:
    141
Loading...

Share This Page