在我的页面加载方法中,我想将模板字段中的文本框设置为一个值。
这是我当前的源代码,显示了我的模板字段文本框项目“txtQuan”:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTotalRate" Text="Qty:" runat="server" />
<asp:TextBox ID="txtQuan" Height="15" Width="30" runat="server" />
<asp:Button ID="addButton" CommandName="cmdUpdate" Text="Update Qty" OnClick="addItemsToCart_Click" runat="server" />
</ItemTemplate>
</asp:TemplateField>
这就是我尝试设置 TextBox 值的方式:
string cartQty = Qty.ToString();
((TextBox)(FindControl("txtQuan"))).Text = cartQty;
我目前收到“nullRefernceException 错误”。
最佳答案
使用 RowDataBound 事件来做到这一点。你可以在网上查一下。该事件处理程序的参数使您可以轻松访问每一行。您还可以使用 var rows = myGridView.Rows
var rows = myGridView.Rows;
foreach(GridViewRow row in rows)
{
TextBox t = (TextBox) row.FindControl("txtQuan");
t.Text = "Some Value";
}
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
TextBox t = (TextBox) e.Row.FindControl("txtQuan");
t.Text = "Some Value";
}
}
关于c# - 在 GridView 模板字段中设置文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696551/