[英]How can I access and set the properties of span control from code behind?
This I what I am able to do with a label control. 这就是我能够使用标签控件执行的操作。
Label mylabel = (Label)e.Row.FindControl("label1");
myLable.Text="";
I would like to do the same with a span and input control. 我想对跨度和输入控件执行相同的操作。 This is what i tried , but it doesn't work.
这是我尝试过的方法,但是不起作用。
var myspan = e.Row.FindControl("span1");
TextBox myinput=(TextBox)e.row.FindControl("Textbox1");
This is my aspx code: 这是我的aspx代码:
<asp:TemplateField HeaderText="abc" SortExpression="val1">
<ItemTemplate>
<span id='myspan<%#Eval("Sno")%>'>
<%#Eval("abc")%></span>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="100px" />
<HeaderStyle HorizontalAlign="Center" Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="abc2" SortExpression="val2">
<ItemTemplate>
<input id='mytextBox<%#Eval("Sno")%>' type="text"
onkeypress="return isNumberKey(event,this);" onchange="return RowUpdategrid(event,this,<%#Eval("Sno")%>,'<%#Eval("val3") %>');this.oldvalue = this.value;"
onfocus="this.oldvalue = this.value;" maxlength="12" class="GridText" style="width: 70px"
value='<%#Eval("CurrentYearLiquidatedPlan")%>' onpaste="return false" disabled="disabled" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" Width="105px" />
</asp:TemplateField>
You can only find server side controls through e.Row.FindControl
. 您只能通过
e.Row.FindControl
找到服务器端控件。 That means, that you can access only controls with runat="server"
through e.Row.FindControl
. 这意味着,您只能通过
e.Row.FindControl
访问带有runat="server"
e.Row.FindControl
。 Default HTML Tags without runat="server"
cannot be accessed. 没有
runat="server"
默认HTML标签无法访问。
Also have a look at the following SO Post " How to access span id in code behind ". 也可以看看下面的SO Post“ 如何在后面的代码中访问span ID ”。
<span id="expSpan" runat="server"></span>
When you want to format your output, you can use 当您要格式化输出时,可以使用
<span id='myspan<%#Eval("Sno")%>'><%#Eval("abc", "0:n3")%></span>
Please look at Standard Numeric Format Strings in MSDN for details. 请查看MSDN中的标准数字格式字符串以了解详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.