views:

37

answers:

1

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 from jquery variable to hidVisitorID ?

A: 

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.

p.campbell
this will work. But I cant get my variable from ToDo.js to work instead of "foobar". I put .val(ToDo.variableName) and it doesnt recognize ToDo
Stewie Griffin
@Display: check out the update on the answer here. `Alert()` and placing of the various .js script tags.
p.campbell
I have a feeling, that I should refactor my javascript to make it work.. as some values are assigned loading events later on. Anyway, thanks for your answer.
Stewie Griffin