Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#表达式定义服务器标记属性值_C#_Asp.net_Web Controls - Fatal编程技术网

使用c#表达式定义服务器标记属性值

使用c#表达式定义服务器标记属性值,c#,asp.net,web-controls,C#,Asp.net,Web Controls,是否可以从c#表达式设置服务器标记的属性,例如 <asp:TextBox Width='<%= [some c# expression] %>'/> ? 我想这会很简单,但我无法运行这样的表达式 谢谢你的帮助 Ryan是的,这是可能的。您需要确保控件在服务器端运行(runat=“server”),但具体取决于您试图在表达式中计算的内容 只要表达式返回字符串,就可以了 <asp:TextBox id="txt" runat="server" Width='&l

是否可以从c#表达式设置服务器标记的属性,例如

<asp:TextBox Width='<%= [some c# expression] %>'/> 

?

我想这会很简单,但我无法运行这样的表达式

谢谢你的帮助


Ryan是的,这是可能的。您需要确保控件在服务器端运行(
runat=“server”
),但具体取决于您试图在表达式中计算的内容

只要表达式返回字符串,就可以了

<asp:TextBox id="txt" runat="server" Width='<%= (10 * 10).ToString() %>px'/> 

请参阅和以获取参考。

注意绑定到的类型

比如说

<asp:TextBox runat=server ID=C_TB_MyTB 
        Text=<%# MyText %> 
        Width=<%# MyWidth %>
    ></asp:TextBox>
不起作用,然而:

protected int MyWidth=300;
protected string MyText = "Bla bla bla...";
可以


很好的代码,

我还想补充一点,
是一个
响应,只是为了更好地理解这很奇怪。我尝试了您的示例:但出现错误:无法从“Width”属性的字符串表示形式“”创建“System.Web.UI.WebControl.Unit”类型的对象。(文本框在数据绑定的网格中)我还尝试了一个简单的表达式和。知道我做错了什么吗?谢谢。@Ryan-
Width
是一个
单位
,所以需要一个数字+一个单位。我的例子应该是
Width='px'
Hi@Oded,它仍然抱怨不是有效的表示。我把它归结为最简单的形式:-这个标签没有显示任何东西!(如果我将表达式替换为文字,它将显示)。感谢您的持续帮助。@Ryan-只需使用
protected string MyWidth="300";
protected string MyText = "Bla bla bla...";
protected int MyWidth=300;
protected string MyText = "Bla bla bla...";