【问题标题】:Error on Accessing Gridview EditItemTemplate访问 Gridview EditItemTemplate 时出错
【发布时间】:2017-11-17 01:18:19
【问题描述】:

以下是我的错误信息:

System.NullReferenceException: '对象引用未设置为 对象的实例。'

DueDateTB 为空。

当我开始更新 Gridview 中的日期列时,会在执行期间发生这种情况。以下是我的前端和后端代码。

背面:

protected void PaymentsDueGV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  TextBox DueDateTB = (TextBox)((GridView)sender).FindControl("DueDateTB");
  string DueDate = DueDateTB.Text;
  DueDateTB.Text= (Convert.ToDateTime(DueDateTB.Text)).ToString("dddd, MMMM dd, yyyy h:mm tt");
  PaymentsDueDS.UpdateParameters["DueDate"].DefaultValue = DueDateTB.Text;
}

正面:

<asp:TemplateField HeaderText="Due Date" SortExpression="CreatedDate" ItemStyle-HorizontalAlign="Center">
  <EditItemTemplate>
    <asp:TextBox runat="server" Text='<%# Convert.ToDateTime(Eval("DueDate")).ToLocalTime().ToString("d") %>' ID="DueDateTB" CausesValidation="True" Columns="5" MaxLength="10"></asp:TextBox>
      <asp:RequiredFieldValidator ID="DueDateTBRValidator" runat="server" ErrorMessage="*"
           ForeColor="Black" ControlToValidate="DueDateTB">
      </asp:RequiredFieldValidator>
  <asp:CalendarExtender ID="DueDateCE" runat="server" Enabled="True" TargetControlID="DueDateTB" Format="MM/dd/yyyy"></asp:CalendarExtender>
  </EditItemTemplate>
  <ItemTemplate>
      <asp:Label ID="lblLocalTime" runat="server" Text='<%# Convert.ToDateTime(Eval("DueDate")).ToLocalTime().ToString("d") %>'></asp:Label>
  </ItemTemplate>
  <HeaderStyle CssClass="GridCells"></HeaderStyle>
  <ItemStyle CssClass="GridCells"></ItemStyle>
  <FooterStyle CssClass="GridCells"></FooterStyle>
</asp:TemplateField>

【问题讨论】:

    标签: c# asp.net gridview edititemtemplate


    【解决方案1】:

    您必须将sender 对象转换为GridViewRow 而不是GridView

    TextBox DueDateTB = (TextBox)((GridViewRow)sender).FindControl("DueDateTB");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多