admin管理员组

文章数量:1432335

Unable to pass an argument from asp LinkButton to the javascript function I am calling it from OnClientClick.

This is what I have so far

<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Bind("Subject") %>' OnClientClick ="OpenViewMeeting(this); return false;" CommandName="Id" CommandArgument='<%# Bind("Id") %>' Width ="30%"/>

Javascript Function

<script type="text/javascript">
    function OpenViewMeeting(Ob) {
        var Id = Ob.CommandArgument;
        alert(Id);
        return false;
    }
    </script>

I can get the text of the button using "ob.text", so I know that "this" actually passes the LinkButton. However, I am trying to catch an argument from it and not the text. I need javascript for some further purpose, so I need to catch the argument there. Any help will be appreciated!

Unable to pass an argument from asp LinkButton to the javascript function I am calling it from OnClientClick.

This is what I have so far

<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Bind("Subject") %>' OnClientClick ="OpenViewMeeting(this); return false;" CommandName="Id" CommandArgument='<%# Bind("Id") %>' Width ="30%"/>

Javascript Function

<script type="text/javascript">
    function OpenViewMeeting(Ob) {
        var Id = Ob.CommandArgument;
        alert(Id);
        return false;
    }
    </script>

I can get the text of the button using "ob.text", so I know that "this" actually passes the LinkButton. However, I am trying to catch an argument from it and not the text. I need javascript for some further purpose, so I need to catch the argument there. Any help will be appreciated!

Share Improve this question asked Sep 2, 2015 at 17:14 umd3330umd3330 391 silver badge8 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

In code behind you can do this as well,

LinkButton2.Attributes.Add("onclick","javascript:return OpenViewMeeting("+LinkButton2.ID+")");

CommandArgument is strictly server side and does not show up in rendered html, how ever you could add an attribute named "CommandArgument" or whatever you wanted in the Page_PreRender and then pass it the bound CommandArgument value like this:

LinkButton lb = Repeater1.Controls[index of the reapeater item].FindControl("LinkButton2") as LinkButton;
lb.Attributes.Add("CommandArgument", lb.CommandArgument);

If CommandArgument becames an html attribute you can try something like this:

<script type="text/javascript">
function OpenViewMeeting(Ob) {
    var Id = Ob.getAttribute("CommandArgument");
    alert(Id);
    return false;
}
</script>

Otherwise inspect your html source and see how it looks and detect you attribute name.

As per This Question And Answer.

CommandArgument is pletely a server-side property and doesn't render any html attribute. So you can't change any button's attribute and fire click on it. The good news is that you can fire postback with client-side __doPostBack function using __doPostBack("<%= btn1.UniqueID %>", val);

please the the link.

The meaning of the parameter "this" in your java script function is as follows. it pass the DOM object of your rendered Link button. It means this parameter can get any HTML attribute in your link button. CommandArgument is a server property, any of these server properties is not understandable to Java script. i don't know what is your exact purpose with this id, any way just to get the ID of your link button (ID generated in HTML, not the Server ID , if you use a master page the server id and ID in rendered HTML will differ), you may just call as follows

<script>
        function OpenViewMeeting(thisObj) {
            alert(thisObj.id);
        }
 </script>

本文标签: cPass argument from link button to javascriptStack Overflow