Search code examples
javascriptjquerysharepoint-2013sharepoint-jsom

How can I get value ID from ascx code behind


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.


Solution

  • 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