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

  • Thread starter Thread starter AAaron123
  • Start date Start date
A

AAaron123

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.
 
Mark said:
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.
 
Mark said:
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
 
Mark said:
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
 
Mark said:
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
 
I missed a line when I cut/paste.
I fixed it below.
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>"


....
 
Mark said:
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
 
Mark said:
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
 
Back
Top