在 Web 用户控件中传递 int 数组作为参数
我有一个 int 数组作为 Web 用户控件的属性。 如果可能的话,我想使用以下语法来内联设置该属性:
<uc1:mycontrol runat="server" myintarray="1,2,3" />
这将在运行时失败,因为它将期望一个实际的 int 数组,但正在传递一个字符串。 我可以将 myintarray 制作为字符串并在 setter 中解析它,但我想知道是否有更优雅的解决方案。
I have an int array as a property of a Web User Control. I'd like to set that property inline if possible using the following syntax:
<uc1:mycontrol runat="server" myintarray="1,2,3" />
This will fail at runtime because it will be expecting an actual int array, but a string is being passed instead. I can make myintarray
a string and parse it in the setter, but I was wondering if there was a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以向 aspx 内的页面事件添加如下内容:
You could add to the page events inside the aspx something like this:
您可以实现一个类型转换器类,在 int 数组和字符串数据类型之间进行转换。
然后使用 TypeConverterAttribute 修饰 int 数组属性,指定您实现的类。 然后,Visual Studio 将使用您的类型转换器对您的属性进行类型转换。
You can implement a type converter class that converts between int array and string data types.
Then decorate your int array property with the TypeConverterAttribute, specifying the class that you implemented. Visual Studio will then use your type converter for type conversions on your property.
如果您在父控件之一上使用数据绑定,则可以使用数据绑定表达式:
使用自定义表达式生成器,您可以执行类似的操作。 表达式生成器:
用法:
If you use DataBinding on one of the parent Controls, you can use a DataBinding Expression:
With a custom expression builder, you can do something similar. The expression builder:
Usage:
@mathieu,非常感谢您的代码。 为了编译我对其进行了一些修改:
@mathieu, thanks so much for your code. I modified it somewhat in order to compile:
在我看来,逻辑且更可扩展的方法是从
asp:
列表控件中获取页面:Seems to me that the logical—and more extensible—approach is to take a page from the
asp:
list controls:很棒的片段@mathieu。 我需要使用它来转换 long,但我没有制作 LongArrayConverter,而是编写了一个使用泛型的版本。
此版本应该适用于任何可从字符串转换的类型。
Great snippet @mathieu. I needed to use this for converting longs, but rather than making a LongArrayConverter, I wrote up a version that uses Generics.
This version should work with any type that is convertible from string.
您是否尝试过研究类型转换器? 此页面看起来值得一看: http://www.codeguru.com/ columns/VB/article.php/c6529/
另外,Spring.Net似乎有一个StringArrayConverter(http://www.springframework.net/doc-latest/reference/html/objects-misc.html - 第 6.4 节),如果您可以将其提供给ASP.net 通过使用 TypeConverter 属性装饰属性,可能会起作用。
Have you tried looking into Type Converters? This page looks worth a look: http://www.codeguru.com/columns/VB/article.php/c6529/
Also, Spring.Net seems to have a StringArrayConverter (http://www.springframework.net/doc-latest/reference/html/objects-misc.html - section 6.4) which, if you can feed it to ASP.net by decorating the property with a TypeConverter attribute, might work..
你也可以做这样的事情:
这将导致:
You could also do something like this:
which will result in:
要添加组成列表的子元素,您需要以某种方式设置控件:
上面的操作是子元素所在的 cproperty 的名称。我使用 ArrayList,因为我没有用它测试任何其他内容。 :
当你的控件初始化时,它将具有子元素的值。 您可以制作一个仅包含整数的迷你课程。
To add child elements that make your list you need to have your control setup a certain way:
The Actions above is the name of the cproperty the child elements will be in. I use an ArrayList, as I have not testing anything else with it.:
when your contorl is initialized it will have the values of the child elements. Those you can make a mini class that just holds ints.
执行 Bill 所说的列表操作,只需在用户控件上创建一个 List 属性即可。 然后你就可以按照 Bill 的描述来实现它。
Do do what Bill was talking about with the list you just need to create a List property on your user control. Then you can implement it as Bill described.
实现一个类型转换器,这里是一个,警告:快速&肮脏,不适用于生产用途等:
并标记控件的属性:
Implement a type converter, here is one, warning : quick&dirty, not for production use, etc :
and tag the property of your control :