在Asp.net WebForm中的UpdatePanel内的模板TextBox feild [英] Template TextBox feild inside UpdatePanel in Asp.net WebForm
问题描述
我有一个数据绑定网格视图,里面有一个文本框模板字段
标记如下所示
I have a databound gridview and I have a textbox template field inside that
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
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
推荐答案
> * < / asp:RegularExpressionValidator >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< / asp:GridView >
< / ContentTemplate >
< / asp:UpdatePanel >
">*</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
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
你好,
试试这个
GridViewRow currentRow =(GridViewRow)txtcons.Parent.Parent.Parent.Parent;
谢谢,
希望这会有所帮助!
Hello,
Try this
GridViewRow currentRow = (GridViewRow)txtcons.Parent.Parent.Parent.Parent;
Thanks,
Hope this helps !!!
依赖于标记的确切结构是一种非常脆弱的方法。一旦触摸标记,您将不得不返回代码来更新您遍历的.Parent
节点的数量。
更好的方法是走向控制树,寻找指定类型的容器:
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:
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;
}
}
然后您可以使用此方法查找特定类型的最近容器,记住要检查 null
如果找不到指定类型的容器:
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:
protected void txt_consumption_TextChanged(object sender, EventArgs e)
{
TextBox txtcons = (TextBox)sender;
GridViewRow currentRow = txtcons.ClosestContainer<GridViewRow>();
if (currentRow != null)
{
calculateperdozen(currentRow);
}
}
这篇关于在Asp.net WebForm中的UpdatePanel内的模板TextBox feild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!