How to assign ASP.NET hidden field value to JavaScript variable?

隐身守侯 提交于 2019-12-10 21:48:37

问题


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: [
    'javascript',
    'css',
    'jquery'];  

I want to assign values with some hidden field or C# variable, please help as I don't have expertise with JavaScript/jQuery.


回答1:


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

C# (Added per/comments)

public string Choices { get; set; }

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

JavaScript

<script type="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] ).




回答2:


hidden:

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

or c# (mvc):

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


来源:https://stackoverflow.com/questions/8992531/how-to-assign-asp-net-hidden-field-value-to-javascript-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!