Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a databound gridview and I have a textbox template field inside that

the markup look like the below

ASP.NET
  <asp:UpdatePanel ID="UpdatePanel1"   runat="server">
                <ContentTemplate>
                    <asp:GridView ID="tbl_costing" runat="server" AutoGenerateColumns="False" OnRowDataBound="tbl_costing_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1023px" ShowHeaderWhenEmpty="True">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>

  <asp:TemplateField HeaderText="Consumption" SortExpression="Consumption">
                                <ItemTemplate>
                                    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                                        <ContentTemplate>
                                            <asp:TextBox ID="txt_consumption" runat="server" Text='<%# Bind("Consumption") %>' AutoPostBack="True" OnTextChanged="txt_consumption_TextChanged"></asp:TextBox>
                                        </ContentTemplate>
                                    </asp:UpdatePanel>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_consumption" ErrorMessage="Required" ForeColor="#CC3300">*</asp:RequiredFieldValidator>

                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt_consumption" ErrorMessage="Enter valid Consumption" ForeColor="Red" ValidationExpression="^[\d.]+$">*</asp:RegularExpressionValidator>
                                </ItemTemplate>
                            </asp:TemplateField>
</Columns>
   </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>


Now I need to get the row index of the Textbox on post back event I did it like this

C#
protected void txt_consumption_TextChanged(object sender, EventArgs e)
        {

            try
            {
                TextBox txtcons = (TextBox)sender;
                GridViewRow currentRow = (GridViewRow)txtcons.Parent.Parent; // Error Here .....
                int rowindex = 0;
                rowindex = currentRow.RowIndex;
                calculateperdozen(currentRow);

            }
            catch (Exception)
            {


            }


        }



Can anyone suggest be a way to get the current Row or row index
Posted

Hello,

Try this
GridViewRow currentRow = (GridViewRow)txtcons.Parent.Parent.Parent.Parent;

Thanks,
Hope this helps !!!
 
Share this answer
 
Relying on the exact structure of the markup is a very brittle approach. As soon as you touch the markup, you'll have to go back through your code to update the number of .Parent nodes you traverse.

A better approach would be to walk up the control tree, looking for a container of a specified type:
C#
public static class ControlTreeExtensions
{
    public static TContainer ClosestContainer<TContainer>(this Control theControl) where TContainer : Control
    {
        if (theControl == null) throw new ArgumentNullException("theControl");
        
        Control current = theControl.NamingContainer;
        TContainer result = current as TContainer;
        while (current != null && result == null)
        {
            current = current.NamingContainer;
            result = current as TContainer;
        }
        
        return result;
    }
}

You can then use this method to find the closest container of a particular type, remembering to check for null if a container of the specified type is not found:
C#
protected void txt_consumption_TextChanged(object sender, EventArgs e)
{
    TextBox txtcons = (TextBox)sender;
    GridViewRow currentRow = txtcons.ClosestContainer<GridViewRow>();
    if (currentRow != null)
    {
        calculateperdozen(currentRow);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900