开发者

How to retrieve a value from jquery in C# user control?

开发者 https://www.devze.com 2023-01-15 02:06 出处:网络
I have a visitorID variable in ToDo function in external javascript. I want to assign its\' value in a user control. Front End Code:

I have a visitorID variable in ToDo function in external javascript.

I want to assign its' value in a user control. Front End Code:

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

<script type="text/javascript">

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

</script>

In the back end it says, that hidVisitorID.Value is null (or -1 in this case). How do I assign value 开发者_Go百科from jquery variable to hidVisitorID ?


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 click the button, it'll post back.

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

Ensure your jQuery reference is ABOVE your other .js reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号