admin管理员组文章数量:1420073
I have got an enumeration in C# ie something like Category.cs.
In a dropdownlist we are binding values.
So if the user selects some specific value in dropdown it will hide one div.
So i want to get the enumeration value in javascript ie want to pare the enumeration value with one selected value in javascript.
Mahesh
I have got an enumeration in C# ie something like Category.cs.
In a dropdownlist we are binding values.
So if the user selects some specific value in dropdown it will hide one div.
So i want to get the enumeration value in javascript ie want to pare the enumeration value with one selected value in javascript.
Mahesh
Share Improve this question edited Apr 4, 2011 at 11:21 user447356 asked Apr 4, 2011 at 10:33 Mahesh KPMahesh KP 6,45613 gold badges53 silver badges71 bronze badges2 Answers
Reset to default 2Suppose you have such enum
with numeric values:
public enum Colors
{
Yellow = 1,
Red,
Blue,
Green,
Purple
}
First of all, in the code behind (Page_Load event) register JavaScript code that will build client side structure that hold the same data:
string strJS = string.Format("var arrColors = {{{0}}}; ",
string.Join(", ", Enum.GetNames(typeof(Colors)).ToList().ConvertAll(key =>
{
return string.Format("{0}: {1}", key, (int)((Colors)Enum.Parse(typeof(Colors), key)));
}).ToArray()));
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "enum", strJS, true);
Now arrColors
is JS variable with both keys and values of your enum
.
To use it, have such code for example:
<script type="text/javascript">
function SelectionChanged(oDDL) {
var selectedValue = oDDL.value;
var enumValue = arrColors[selectedValue] || "N/A";
alert("enum value for '" + selectedValue + "' is: " + enumValue);
}
</script>
And the drop down should look like this:
<select onchange="SelectionChanged(this);">
<option>Select..</option>
<option value="Yellow">Yellow</option>
<option value="Green">Green</option>
</select>
System.Enum.GetNames(typeof(yourenumerationtype))
- returns an array of strings, which represents enumeration items' names
本文标签: How to use C enumeration values in JavaScriptStack Overflow
版权声明:本文标题:How to use C# enumeration values in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745332685a2653890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论