在Asp.net WebForm中的UpdatePanel内的模板TextBox feild

时间:2015-12-16 08:12:08

标签: c# asp.net

我有一个数据绑定网格视图,我在

中有一个TextBox模板字段

标记如下所示

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

现在我需要在回发事件中获取Textbox的行索引我这样做了

 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)
     {   
     }        
}

任何人都可以建议您获取currentRowrowindex吗?

1 个答案:

答案 0 :(得分:0)

很抱歉,到目前为止,我还没有更新我遇到此问题的解决方案。让它帮助有相同查询的人

我创建了一个循环槽的类,直到它到达Gridviewrow作为命名容器

 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;
        }
    }

然后在我的Click事件或更改甚至我调用函数

protected void txt_consumption_TextChanged(object sender, EventArgs e)
 {                
     try
     {
  TextBox txtcons = (TextBox )sender;
   GridViewRow currentRow = txtcons.ClosestContainer<GridViewRow>();

         int rowindex = 0;
         rowindex = currentRow.RowIndex;
         calculateperdozen(currentRow);    
     }
     catch (Exception)
     {   
     }        
}