简体   繁体   中英

Passing a parameter as an event argument in JS to code behind

I was using an asp:Button on each row to pass 'ID' to code behind 'lvDataStores_OnButtonClick()'.

Instead of clicking the button I want to make the entire row clickable. I have done this using

<tr runat="server" class="altItemTableData" onclick='<%#"doPostBack("+Eval("ID")+")" %>'>

From JS I then want to press the 'btn_ViewMap' and call code lvDataStores_OnButtonClick() while passing the argument 'MapId'...The pluming of this works as I can call code behind ' protected void lvDataStores_OnButtonClick(Object sender, CommandEventArgs e) '

But I am unsure how to pass mapId as an argument in the $$("btn_ViewMap")[0].click(); . ATM I am using something like:

<script type="text/javascript">
     function doPostBack(Mapid) 
     {
        alert("MAP ID is " + Mapid)
        $$("btn_ViewMap")[0].click();
     }

<asp:Button ID="btn_ViewMap" runat="server" visible="false" CommandName="View" CommandArgument='<%#Eval("ID")%>' OnCommand="lvDataStores_OnButtonClick" />

CommandArgument is completely 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 and pass your custom value as second parameter:

<script type="text/javascript">
    $("a").click(function () {
        var val = $(this).attr('id').toString();
        __doPostBack("<%= btn1.UniqueID %>", val);
    });
</script>

And you can get passed argument in server click handler from the Request.Form collection:

protected void button_click(object sender, EventArgs e)
{
    var argument = Request.Form["__EVENTARGUMENT"];
}

If script above won't work then maybe __doPostBack function not defined on page. In this case add this code to Page_PreRender method: ClientScript.GetPostBackEventReference(btn1, string.Empty); this will force page to define __doPostBack method on page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM