how can i get the button's command argument?
Asked Answered
S

4

5

Is it possible to get a button's command argument in javascript. I don't want to manipulate it, i just want to read it.

If possible - how?

Scincoid answered 14/10, 2009 at 17:54 Comment(3)
Effectively you want to know the javascript you're that is executing?Sarmentose
Huh? I want to be able to get the command argument for a button given a client id.Scincoid
"command argument" and "client id" aren't really javascript terms; do you mean "onclick" and "id"?Eschew
P
19

Make your own attribute and get it with standard JavaScript:

Markup:

<asp:LinkButton ID="LinkButton1" cmdname="test" CommandName="test" CommandArgument="1" 
    runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

Script:

//however you choose to get the element, jQuery...etc
document.getElementById('<%=LinkButton1.ClientID %>').cmdname
Provincialism answered 14/10, 2009 at 18:11 Comment(2)
you are a beast!! I didn't know you can make up your own property. I've been at this for hours now!!! thanks rick!! if i could give you more points i would.Scincoid
To use correct HTML5 (though it works fine in older HTML too!) you should prefix your custom attribute with data-. So you'd have data-cmdname. See tinyurl.com/6uycchjFontana
S
1

You can put command argument into some hidden field, then retrieve the value of the hidden field in javascript after page loads such as $(document).ready() in JQuery.

Swivel answered 14/10, 2009 at 17:57 Comment(1)
This is the only way. The command argument exists only on the server side as far as I know.Liegeman
W
0

The short answer is NO if you are ONLY using javascript. But you could retrieve it with an ajax call to the server or using a hidden field like J.W. suggested. This property is not even accessible from themes. For more info read the documentation in msdn

Whereon answered 14/10, 2009 at 18:6 Comment(0)
L
0

JS :
------
function get_id(obj) {
    var my_eval = obj.getAttribute("name");
    alert(my_eval);
}
<input type="button" id="btn" value="My Btn" onclick="get_id(this);" name='<%# Eval("item_id") %>' />
Lumberyard answered 31/7 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.