asp.net – 在web用户控件中传递int数组作为参数
发布时间:2020-12-15 18:47:16 所属栏目:asp.Net 来源:网络整理
导读:我有一个int数组作为Web用户控件的属性。如果可能,我想使用以下语法内联设置该属性: uc1:mycontrol runat="server" myintarray="1,2,3" / 这将在运行时失败,因为它会期待一个实际的int数组,而是传递一个字符串。我可以使myintarray一个字符串,并在sette
我有一个int数组作为Web用户控件的属性。如果可能,我想使用以下语法内联设置该属性:
<uc1:mycontrol runat="server" myintarray="1,2,3" /> 这将在运行时失败,因为它会期待一个实际的int数组,而是传递一个字符串。我可以使myintarray一个字符串,并在setter中解析,但我想知道是否有一个更优雅的解决方案。 解决方法
实施类型转换器,这里是一个,警告:快速和脏,不用于生产使用等:
public class IntArrayConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context,Type sourceType) { return sourceType == typeof(string); } public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context,System.Globalization.CultureInfo culture,object value) { string val = value as string; string[] vals = val.Split(','); System.Collections.Generic.List<int> ints = new System.Collections.Generic.List<int>(); foreach (string s in vals) ints.Add(Convert.ToInt32(s)); return ints.ToArray(); } } 并标记您的控件的属性: private int[] ints; [TypeConverter(typeof(IntsConverter))] public int[] Ints { get { return this.ints; } set { this.ints = value; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 获取Uploadify以使用asp.net-mvc
- ASP.NET Core中的OWASP Top 10 十大风险-SQL注入
- ASP.NET路由:令牌之间的字面子段和来自文字子段的字符的路
- asp.net-mvc – 如何持久保存用户选择(例如:主题选择)
- asp.net-mvc – Razor ViewEngine HTML.Checkbox方法创建一
- 如何在ASP.NET中自定义发布?
- asp.net – 如何调用一个特定的UpdatePanel加载后的客户端J
- 深入理解C# 3.x的新特性(1): Anonymous Type
- asp.net-mvc – 使用模型更改更新scaffolded控制器和视图
- 什么是ASP.NET vNext?
推荐文章
站长推荐
- asp.net – HTTP PUT在IIS 8.5上失败
- 如何阻止IIS 8停止空闲的ASP.NET应用程序?
- ASP.NET MVC是一个大的企业项目的不好的选择吗?
- asp.net-mvc – 将多个参数传递给控制器???
- asp.net-mvc-2 – 引用用TextBoxFor()创建的控件
- asp.net – 计算Twitter上特定单词的结果数
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MV
- asp.net – 这是什么错误?
- asp.net-mvc-4 – ASP .Net MVC 4 Authorize和Al
- asp.net-mvc – ASP MVC友好的URL和相对路径图像
热点阅读