【发布时间】:2014-03-02 01:38:15
【问题描述】:
我有一个 GridView,我需要能够以编程方式更改编辑模板中 TextBox 的值。当我尝试在 onRowDataBound 期间访问它时,我得到了这个:
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
我认为在 onRowDataBound 方法中,编辑模板控件不可访问。但是,当我尝试在 onRowEditing 方法中编辑 TextBox 的值时,GridViewEditEventArgs 对象没有 .Row 选项,因此您似乎也无法在 onRowEditing 方法中访问编辑模板中的控件。
任何想法如何在 GridView 的编辑模板中以编程方式更改 TextBox 的值?
ASP.NET WebForms、.NET 4.0、C#
====
编辑#1:这是我目前拥有的。 txtMiles 对象在 RowEditing 中最终为空。
<asp:TemplateField HeaderText="Miles Frequency">
<ItemTemplate>
<asp:Label ID="lblFreqMiles" runat="server" Text='<%# Eval("FrequencyMiles") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtFreqMiles" runat="server" Text='<%# Eval("FrequencyMiles")%>' Width="50px"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="req1" ControlToValidate="txtFreqMiles" ErrorMessage="*" />
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtFreqMiles" ErrorMessage="Value must be a whole number." />
</EditItemTemplate>
</asp:TemplateField>
protected void gvMaint_RowEditing(object sender, GridViewEditEventArgs e)
{
//format Miles Frequency column
GridViewRow row = grvMaint.Rows[e.NewEditIndex];
TextBox txtMiles = (TextBox)row.FindControl("txtFreqMiles");
if (txtMiles.Text == "999999")
{
//do stuff
}
grvMaint.EditIndex = e.NewEditIndex;
populateMaintGrid();
}
【问题讨论】:
标签: c# asp.net .net gridview webforms