asp.net – 在web用户控件中传递int数组作为参数
发布时间:2020-05-23 11:39:41 所属栏目:asp.Net 来源:互联网
导读:我有一个int数组作为Web用户控件的属性。如果可能,我想使用以下语法内联设置该属性: uc1:mycontrol runat=server myintarray=1,2,3 / 这将在运行时失败,因为它会期待一个实际的int数组,而是传递一个字符串。我可以使myintarray一个字符串,并在setter中解
|
我有一个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; }
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 为什么我的超级简单的ASP.NET Web API(
- asp.net-mvc – 所选值不适用于SelectList
- 在ASP.NET应用程序中托管的WCF服务中使用Autofac作为DI
- 使用什么方法将ASP.Net应用程序部署到野外?
- asp.net-mvc – MVC应用程序部署; System.Data.SqlClient.S
- asp.net-mvc – 向MVC 3添加基于声明的授权
- 是否有可能在asp经典和asp.net之间共享会话状态
- asp.net-mvc – 如何使用activedirectorymembershipprovide
- asp.net – 如何使用AntiXss Library正确清理内容?
- asp.net-mvc – SessionSecurityTokenHandler尝试使用DPAPI
推荐文章
站长推荐
- 为什么在使用当前同步上下文启动任务时,不设置AS
- asp.net – SimpleMembershipProvider不工作
- asp.net – 根据自己的主机Web API Windows服务验
- 文件上传 – 以经典的asp上传文件
- 在ASP.NET WebForms中使用jQuery调用’WebMethod
- asp.net-mvc-3 – 将字符串数组绑定到MVC Razor中
- asp.net – 可以在web.config中添加响应http头吗
- asp.net-mvc – Parallel Blob上传间歇性抛出404
- 从Asp.Net MVC 6 API返回JSON错误
- 如何限制文件夹访问在asp.net
热点阅读
