What do (TextBox) and Controls[0] mean in this code?

MikeR 0 Reputation points
2023-03-08T10:10:24.1166667+00:00

Hi, now at school we are learning how to use GridView. We have a table with all the information of all the users. When we want to update a row, we need to store name, last name and other data in separate variables. Our teacher does it this way:

GridViewRow changedRow = GridView1.Rows[e.RowIndex];
string firstName = ((TextBox)changedRow.Cells[1].Controls[0]).Text;

I don't understand what are (TextBox) and Controls[0]? What do they do? What are they used for?

Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 47,441 Reputation points
    2023-03-08T10:23:49.3933333+00:00

    (TextBox)

    That's a "type boxing", means forcing to convert to a specific data type, see Boxing and Unboxing (C# Programming Guide)

    Controls[0]

    That's simply accessing the first (zero-based) element of an array, see Array Class


  2. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-03-08T17:04:30.5466667+00:00

    (TextBox) is a cast (not an box/unbox).

    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions

    as you might guess Cells is an army of cells of a grid row. .Controls is a collection of Controls in the cell. the author assumes the first control in the second cell is a TextBox (which inherits from Control). The cast allows accessing unique TextBox properties like .Text. if the control is not a TextBox, the cast will throw a runtime error.

    box/unbox is for converting value types to objects and back. most commonly used for numeric types. this is a feature f most typed languages. you either pass the actual value via the stack, or you pass a value pointer allocated on the heap.

    0 comments No comments

  3. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-03-09T05:30:58.66+00:00

    Hi @MikeR

    First of all

    GridViewRow changedRow = GridView1.Rows[e.RowIndex];

    Declare a variable of type GridViewRow whose value is the "e.RowIndex+1" row of GridView1.

    string firstName = ((TextBox)changedRow.Cells[1].Controls[0]).Text;

    Declare a string variable firstname. Strongly convert this GridViewRow variable to a TextBox variable via '(TextBox)changedRow'. (Provided you can convert this control to a TextBox control.)

    ‘Cells[1]. Controls[0]' represents the first Control of the second Row.

    ForExample

    Code:

    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="D1">
                        <ItemTemplate>
                            <asp:TextBox ID="D1" runat="server" Text='Test1'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="D2">
                        <ItemTemplate>
                            <asp:TextBox ID="D2" runat="server" Text='Test2'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
    </asp:GridView>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
     GridViewRow a = GridView1.Rows[0];
                string firstname = ((TextBox)a.Cells[0].Controls[1]).Text;
                TextBox1.Text = firstname;
    

    Result:

    Test1

    Test2

    Best regards,
    Qi You


    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 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.