简体   繁体   English

如何从后面的代码访问和设置跨度控件的属性?

[英]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.

相关问题 如何从代码后面找到span并为其添加用户控件? - How can I find span from code behind and add user control to it? 如何在后面的代码中访问扩展 Panel 的自定义控件的公共属性? - How can I access the public properties of a custom control, which extends Panel, in code behind? 我可以从(ResourceDictionary)后面的代码访问命名控件吗? - Can I get access from code behind (of a ResourceDictionary) to a named control? 如何从代码隐藏中访问用于填充XAML类的DataContext的属性/方法? - How can I access properties/methods of the DataContext used to populate a XAML class from the code-behind? 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 我如何使用反射从文件后面的asp.net代码访问服务器端控件? - How can i access a server side control from asp.net code behind file using reflection? 从 c# 中的代码访问用户控件属性和值 - Access user control properties and values from code behind in c# 使用WPF自定义控件,如何通过后面的代码为自定义控件提供名称以访问它? - Using a WPF Custom Control, How can I give my custom control a name to access it via the code behind? 如何从堆栈面板中的控件后面的代码访问? - How can i access from code behind, controls that are in a stack panel? 如何从后面的代码访问aspx网格? - How can I access the aspx grid from code behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM