简体   繁体   中英

How to pass a variable value from the code behind to an external JavaScript file (jQuery file)?

Suppose I have this:

<asp:LinkButton runat="server" CssClass="lFilename" ID="grdlinkFilename" Text='<%#Eval("FILEPATH")%>' CommandArgument='<%#Eval("FILEPATH")%>'  OnCommand="grdlinkFilename_click"> </asp:LinkButton>

How can I pass the <%#Eval("FILEPATH")%> to an external JavaScript (jQuery) file?

You can take help of html tage data-id to do this.

 <asp:LinkButton runat="server" CssClass="lFilename"
 ID="grdlinkFilename" Text='<%#Eval("FILEPATH")%>'
 CommandArgument='<%#Eval("FILEPATH")%>' 
 OnCommand="grdlinkFilename_click" data-id='<%#Eval("FILEPATH")%>' >
 </asp:LinkButton>

and in Jquery you can get that value as

var result = $("#grdlinkFilename").attr("data-id");

Is this what it is you need ?

HTML

   var valueLink = document.getElementById('grdlinkFilename').innerText

ASP

   var valueLink = document.getElementById('<%=grdlinkFilename.ClientID %>').innerText

You can pass it using different state management available in ASP.NET and out of it. For ASP.NET "specific" state management , can have a look on good description on :

A Beginner's Tutorial on ASP.NET State Management

Or, simply assign value to some UI element with display:none ,and read it's value from javascript .

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