简体   繁体   中英

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:

<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 . That means, that you can access only controls with runat="server" through e.Row.FindControl . Default HTML Tags without runat="server" cannot be accessed.

Also have a look at the following SO Post " How to access span id in code behind ".

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM