Search code examples
asp.netgridviewpaneltemplatefield

Get value of TextBox in Panel in <table> in TemplateField in GridView


I have a GridView with a TemplateField. Inside the ItemTemplate there are 2 TextBox's and then a <table> with an Panel. In that Panel there's another TextBox.
How do I get the value of that last TextBox?
I have the index of the gridview's row, but I don't know how to get on from there.
(Data from DB is bound at page load).

<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:GridView ID="grv_Four_Rows" runat="server" AutoGenerateColumns="False"
    ShowHeader="False" CellPadding="3" CssClass="myGrid" DataKeyNames="Test1_First_Name">
    <RowStyle BackColor="#b5c7de" />
    <AlternatingRowStyle BackColor="#d1e0e0" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txbFirstName" runat="server" CssClass="myTextBox" ReadOnly="false" Text='<%# Eval("Test1_First_Name")%>'></asp:TextBox>
                <asp:TextBox ID="txbLastName" runat="server" CssClass="myTextBox" ReadOnly="true" Text='<%# Eval("Test1_Last_Name")%>'></asp:TextBox>
                <table style="width: 350px;">
                    <asp:Panel ID="upp_2nd_row" runat="server" Visible="true">
                         <td style="float: left">
                             <a style="color: red; font-weight: bold;">Address: </a>
                             <asp:TextBox ID="txbAddress" Width="200px" Font-Bold="true" runat="server" Text='<%# Eval("Test1_Address")%>' />&nbsp;&nbsp;
                         </td>
                         <td style="float: right">
                             <asp:Button ID="btn_Edit_Details" runat="server" Text="Go" Font-Bold="true"
                                    OnClick="my_Update_details" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
                         </td>
                    </asp:Panel>
                </table>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<asp:Label ID="lblMessage1" runat="server" BackColor="Yellow" Font-Bold="true"></asp:Label>

C# Code behind:

    protected void my_Update_details(object sender, EventArgs e)
    {
        string my_row_index = (sender as Button).CommandArgument;
        lblMessage1.Text = "Row Index: " + my_row_index;
    }

When I click the "Go" button in any row, the my_Update_details event is fired. All it does is, it displays the gridview's current row's index, and the value is correct.
But in the my_Update_details I also want to pick the values of the textbox's, and that's where I'm stuck (user is supposed to change their values and the 'Go' button should update the DB). In the following example I clicked the button in the 4th row and it correctly displays the index as 3 :
enter image description here


Solution

  • So this is how it works: first I rack my brains for a couple of days, then I post a question in SO, then - after 2 minutes I find a solution...
    All I did was add a line in the my_Update_details method (the second line) :

        protected void my_Update_details(object sender, EventArgs e)
        {
            string my_row_index = (sender as Button).CommandArgument;
            TextBox AAAAAA = (TextBox)grv_Four_Rows.Rows[Convert.ToInt32(my_row_index)].Cells[0].FindControl("txbAddress");
            lblMessage1.Text = "Address: " + AAAAAA.Text;
            lblMessage2.Text = "Row Index: " + my_row_index;
        }
    

    and now I get:
    enter image description here

    which is exctly what I needed : to get the value that's in the txbAddress textbox.
    Hope it will help someone in the future....