Skip to content Skip to sidebar Skip to footer

How To Assign Asp.net Hidden Field Value To Javascript Variable?

Following is the code snippets taken from http://pietschsoft.com/post/2011/09/09/Tag-Editor-Field-using-jQuery-similar-to-StackOverflow.aspx // pre-selected tags values: [ 'ja

Solution 1:

You can create a public property and use it in your HTML like the following...

C# (Added per/comments)

publicstring Choices { get; set; }

protectedvoidPage_Load(object sender, EventArgs e)
{
    string[] choices = newstring[] { "'Choice 1'", "'Choice 2'", "'Choice 3'" };
    Choices = String.Join(",", choices);
}

JavaScript

<scripttype="text/javascript">var values = [<%= Choices %>];
</script>

NOTE: I put single quotes around the values since JavaScript requires the them to recognize the value as part of a string array ( Valid = ['value','value'] / Invalid = [value,value] ).

Solution 2:

hidden:

values: [
        $('hidden1').val(),
        $('hidden2').val(),
        $('hidden3').val()];

or c# (mvc):

values: [
        '@model.var1',
        '@model.var2',
        '@model.var3'];

Post a Comment for "How To Assign Asp.net Hidden Field Value To Javascript Variable?"