编辑gridview行时捕获并报告错误

时间:2018-11-18 21:11:39

标签: c# asp.net gridview

我正在尝试捕获错误并将其显示在gridview中,但是遇到了麻烦。这是我的代码。

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvDailyGPS.Rows[e.RowIndex];

TextBox txtEditTime = row.FindControl("txtEditTime") as TextBox;
DropDownList ddlEventDateOnly = row.FindControl("ddlEventDateOnly") as DropDownList;

DateTime EDT = DateTime.Parse(ddlEventDateOnly.SelectedValue);
try
{
    EDT = DateTime.Parse(ddlEventDateOnly.SelectedValue + " " + txtEditTime.Text);
}
catch (Exception)
{


    Label lblErr = row.FindControl("lblErr") as Label;

    lblErr.Text = "There was an error with Date or Time";
    gvDailyGPS.EditIndex = -1;
    BindGrid();

}

我尝试了许多不同的方法,并将lblErr标记添加到“编辑”和“项目模板”中均无济于事。如何返回编辑模式,将文本添加到lblErr并显示?这是我的aspx:

<ItemTemplate>
    <asp:Label ID="lblEventTime" runat="server" Text='<%# Bind("EventDateTime") %>'></asp:Label>
    <br />
    <asp:Label ID="lblErr" runat="server" Font-Bold="True" ForeColor="Red" Text=""></asp:Label>
</ItemTemplate>
<EditItemTemplate>
    <asp:Label ID="lblThisEventDate" runat="server" Text='<%# Bind("EventDateTime",  "{0:M/dd/yyyy}") %>' Visible="false"></asp:Label>
    <asp:DropDownList ID="ddlEventDateOnly" runat="server" />
    <asp:TextBox ID="txtEditTime" Text='<%# Bind("EventDateTime", "{0:hh:mm tt}") %>' Width="95px" runat="server" />
    <asp:Label ID="lblErr" runat="server" Font-Bold="True" ForeColor="Red" Text=""></asp:Label>
</EditItemTemplate>

0 个答案:

没有答案