简体   繁体   English

Asp.net WebForm中UpdatePanel内的模板TextBox字段

[英]Template TextBox feild inside UpdatePanel in Asp.net WebForm

I have a databound gridview and I have a TextBox template field inside that 我有一个数据绑定的gridview,里面有一个TextBox模板字段

The markup look like the below 标记如下所示

<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 现在我需要在回发事件中获取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)
     {   
     }        
}

Can anyone suggest be a way to get the currentRow or rowindex ? 任何人都可以建议一种获取currentRowrowindex吗?

Sorry that I had not updated the solution I got for this problem till now. 抱歉,到目前为止,我还没有更新解决此问题的解决方案。 Let it help somebody who have the same query 让它帮助有相同查询的人

I created a class which will loop trough till it reaches the Gridviewrow as naming container 我创建了一个类,它将遍历槽直到它到达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;
        }
    }

Then in my Click event or Change even I called the function 然后在我的Click事件或Change中,甚至我调用了该函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM