在gridview行中查找控件 [英] Finding a control inside a gridview row
问题描述
大家好,
最近2个小时,我一直在寻找解决方案,但没有成功.我有一个gridview,并且在此视图中有一个templatefield.我正在尝试访问此网格内的文本框.这是设计器中用于gridview的代码:
Hello all,
I have been trying to find a solution to it for the last 2 hours but I am not successful. I have a gridview and inside this view I have a templatefield. I am trying to access a textbox inside this grid. Here is the code in the designer for gridview:
<asp:GridView ID="gvExcParts" runat="server" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" CellPadding="4" CellSpacing="2"
UseAccessibleHeader="False" Width="950px" onrowdatabound="gvSelectedItems_RowDataBound" Visible="False" OnRowEditing="gvExcParts_RowEditing"
OnRowCancelingEdit="gvExcItems_RowCancelingEdit" OnRowUpdating="gvExcItems_RowUpdating" OnRowDeleting="gvExcItems_RowDeleting"
onrowcommand="gvExcItems_RowCommand" AllowSorting="true" AutoGenerateEditButton="true">
<RowStyle BackColor="#EEEEEE" BorderColor="White" BorderStyle="Solid"
BorderWidth="2px" />
<Columns>
<asp:TemplateField HeaderText="Part Quantity" >
<ItemTemplate > <%#Eval("RMAPartQty")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtRMAPartQuantity" runat="server" width="60px" Text='<%#Eval("RMAPartQty")%>'></asp:TextBox>
<asp:FilteredTextBoxExtender ID="ftbeQtyEdit" runat="server" TargetControlID="txtRMAPartQuantity" ValidChars="1234567890" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#CCCCCC" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" Font-Bold="True" />
<EditRowStyle BackColor="#EEEEEE" />
<AlternatingRowStyle BackColor="#EFEFEF" />
</asp:GridView>
我正在尝试读取"txtRMAPartQuantity"的值.我的代码是:
I am trying to read the value of "txtRMAPartQuantity". My code for it is:
for (int i = 0; i < gvExcParts.Rows.Count; i++)
{
string t1 = gvExcParts.Rows[i].Cells[1].Text;
TextBox txtQtyval1 = (TextBox)gvExcParts.Rows[i].Cells[1].FindControl("txtRMAPartQuantity");
TextBox txtQtyval2 = gvExcParts.Rows[i].FindControl("txtRMAPartQuantity") as TextBox;
}
在我的gvExcItems_RowUpdating
事件上,我使用相同的FindControl()
方法,它成功返回了值.但是在我的页面加载中,无论我尝试什么,该值始终为null.我做错了什么,有人可以帮忙吗?
On my gvExcItems_RowUpdating
event I am using the same FindControl()
method and it successfully returns the value. But in my page load no matter what I try the value is always null. What I am doing wrong can someone help me?
推荐答案
请确保在将其绑定到数据后,遍历gridview行.
Make sure you iterate through your gridview rows after you bind it to data.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
gvExcParts.DataSource = yourDataSource;
gvExcParts.DataBind();
for (int i = 0; i < gvExcParts.Rows.Count; i++)
{
TextBox text = (TextBox)gvExcParts.Rows[i].FindControl("txtRMAPartQuantity");
}
}
}
检查这些博客
访问GridView控件内的不同控件 [ http://stackoverflow.com/questions/1965835/find-control-inside-grid-row [^ ]
--NDK
check these blogs
Accessing the different controls inside a GridView control[^]
http://stackoverflow.com/questions/1965835/find-control-inside-grid-row[^]
--NDK
尝试此代码...
这里h是行号
int h = 0;
TextBox nt =(TextBox)Gridview1.Rows [h] .Cells [1] .FindControl("txt_Product_Name");
字符串myVal = nt.Text;
通过使用FindControl方法,您可以选择网格视图内任何控件的值.
享受:)
try this code ...
here h is row number
int h=0;
TextBox nt = (TextBox)Gridview1.Rows[h].Cells[1].FindControl("txt_Product_Name");
string myVal= nt.Text ;
by using FindControl method u can pick value of any control inside a grid view.
enjoy :)
这篇关于在gridview行中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!