gpt4 book ai didi

c# - 如何在 ASCX 上设置控件的 Int 属性?

转载 作者:太空狗 更新时间:2023-10-29 20:02:01 25 4
gpt4 key购买 nike

我有一个 ASCX 包含

<my:Foo ID="Bar" runat="server" Value='' />

我想用 textbox1.Text 设置 Value,但是 Value 是 Int32。我正在寻找这样的东西:

<my:Foo ID="Bar" runat="server" Value='<%= Int32.Parse(textbox1.Text) %>' />

但是我明白了

Parser Error Message: Cannot create an object of type 'System.Int32' from its string representation '<%= Int32.Parse(textbox1.Text) %>' for the 'Value' property.

有没有办法在 ASCX 文件上执行此操作?我必须为此属性实现 TypeConverter 吗?

最佳答案

我不明白为什么你不能简单地使用文字而不是字符串表示:

<my:Foo ID="Bar" runat="server" Value="58" />

如果你想动态设置这个值,你需要在代码后面或代码块内这样做,例如在页面加载事件句柄中,因为你不能在服务器端控制:

// code behind, in the page_load event handler
Bar.Value = 58;

或者,在 <%%> 内, 在服务器端控件之外:

<% Bar.Value = 58; %>

关于c# - 如何在 ASCX 上设置控件的 Int 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3945158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com