Why do asp.net controls not exist in current contexts in serverside (C#)

Donald Symmons 2,246 Reputation points
2023-11-02T04:10:09.9133333+00:00

I have label and textbox controls inside of a Gridview control, but none of them is recognized in the server side window. Why is that please, and how can I make the server side recognize these controls be it textbox or label or other controls?

 <asp:GridView ID="Gridview2" runat="server" Font-Size="10pt" HeaderStyle-Font-Size="10pt" CellPadding="6" GridLines="None" ShowFooter="True" AutoGenerateColumns="False" HeaderStyle-Font-Bold="false"
                        Style="width: 100%" FooterStyle-BackColor="#fdfdfd" HeaderStyle-ForeColor="#000000" Height="50px" HeaderStyle-Height="10px">
                        <Columns>
                            <asp:TemplateField HeaderText="Column1" ItemStyle-Width="40%" HeaderStyle-Font-Bold="false">
                                <ItemTemplate>
                                    <asp:TextBox ID="textBox1" runat="server" Class="form-control" Width="100%" Font-Size="10pt" Style="overflow: hidden; text-transform: capitalize; resize: none;" oninput="Resize(this)" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Column2" ItemStyle-Width="10%" HeaderStyle-Font-Bold="false">
                                <ItemTemplate>
                                    <asp:TextBox CssClass="form-control" ID="txtQuantity" Font-Size="10pt" runat="server" Width="100%" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Column3" ItemStyle-Width="25%" HeaderStyle-Font-Bold="false">
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="hero-lbl" Style="font-weight: 400;"></asp:Label>
                                    <asp:TextBox ID="TxtBx" runat="server" Font-Size="10pt" Width="90%" placeholder="0.00" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Column4" ItemStyle-Width="50%" HeaderStyle-Font-Bold="false">
                                <ItemTemplate>
                                    <asp:Label class="c-info" runat="server" ID="Infolabel" Style="font-weight: 400;"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
.NET
.NET
Microsoft Technologies based on the .NET software framework.
2,330 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
1,731 questions
ASP.NET Web Forms
ASP.NET Web Forms
A part of the ASP.NET web application framework that can be used to create ASP.NET web applications.
661 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 20,136 Reputation points Microsoft Vendor
    2023-11-02T05:24:45.5433333+00:00

    Hi @Donald Symmons,

    For nested controls, you need to use the FindControl method to find the corresponding control.

    FindControl("ControlId")

    foreach (GridViewRow row in Gridview2.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            Label lb1 = (Label)row.FindControl("textBox1");
            lb1.Text = "A";
            TextBox tb1 = (TextBox)row.FindControl("Infolabel");
            tb1.Text = "B";
        }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful