How to get value from jquery in c # user control?

I have a visitor variable in the ToDo function in external javascript.

I want to assign its value in a user control. Foreground Code:

<asp:HiddenField ID="hidVisitorID" runat="server" Value="-1"/>

<script type="text/javascript">

$j('#<%= hidVisitorID.ClientID %>').val(ToDo.visitorID);

</script>

At the opposite end, it says hidVisitorID.Value is null (or -1 in this case). How to assign value from jquery variable to hidVisitorID?

+3
source share
1 answer

Try this code:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="my_TO_DO.js"></script>

<script type="text/javascript">
    $(document).ready(function () {
        alert('my todo varname is: ' + ToDo.variableName);
        $('#<%= hidVisitorID.ClientID %>').val("foobar");
    });
</script>


 <asp:HiddenField ID="hidVisitorID" runat="server" Value="-1"/>

 <asp:Button Text="sub" runat="server" onclick="Click" />

When you press the button, it will return a message.

protected void Click(object sender, EventArgs e)
{
    string valFromHidden = hidVisitorID.Value;
   //valFromHidden is now foobar
}    

Make sure the jQuery link is above the previous .js link.

0
source

Source: https://habr.com/ru/post/1763807/


All Articles