0

ascx code behind

ascx

javascript file

I have 3 pics above. I don't know how to declare variable in JavaScript to get value from ascx and code behind.

Sang Tran
  • 47
  • 1
  • 7
  • If your "javascript file" is a .js, then be advised that you can't use `<%=` in a .js file. Only .ascx files are parsed for server-side variables. – freedomn-m Nov 25 '19 at 17:34

2 Answers2

0

ALL CREDIT GOES TO: https://stackoverflow.com/a/5392980/9357872

Add a class then get the value of the class:

<input type="hidden" id="hdnLibraryName" runat="server" CssClass="library"/>

Then:

var LibraryName = $(".library").val();
Evik Ghazarian
  • 1,803
  • 1
  • 9
  • 24
0

Using the code as below to get the hidden value using jQuery code.

var libraryName = $("input[id$='hdnLibraryName']").val();

Example:

.ascx code

<input type="hidden" id="hdnLibraryName" runat="server"/>

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var libraryName = $("input[id$='hdnLibraryName']").val();
        console.log(libraryName);
    });
</script> 

.ascx.cs code

protected void Page_Load(object sender, EventArgs e)
{
    this.hdnLibraryName.Value = "Library1126";
}

enter image description here

LZ_MSFT
  • 4,079
  • 1
  • 8
  • 9