正在编辑中更改GridView单元格文本“输入字符串格式不正确”.c# ASP.NET

本文关键字:格式 字符串 输入 不正确 NET ASP 文本 编辑 单元格 GridView | 更新日期: 2023-09-27 18:14:36

我试图在ASP上显示一个相当简单的每周时间表。. NET网站,绑定到一个SQL表。我遇到的问题是,我的星期值在SQL表中作为整数(1到7)保存。我想将整数显示给用户作为实际的星期名称,使用OnRowDataBound事件即可找到。但是,当我将工作日更改回整数进行编辑时,我得到一个JScript错误,即"输入字符串格式不正确"。

起初我认为这可能是一个问题与其他两个细胞,然后我搞乱,但据我所知,情况并非如此。不幸的是,JScript错误没有给我足够的信息来真正调试这个问题,我知道OnEditing事件成功地将两个边界字段的文本更改回一个数字

所以我的问题是,解决这个问题的最好方法是什么?是否有更好的方法向用户显示星期名称,然后在他们想要编辑一行时将这些名称更改为整数?有没有一种方法可以改变编辑模板使用下拉列表之类的东西?

<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="false"
    AutoGenerateDeleteButton="true"
    AutoGenerateEditButton="true"
    CssClass="view"
    DataKeyNames="TimeID"
    DataSourceID="SqlDataSource1"
    OnRowDataBound="GridView1_RowDataBound"
    OnRowEditing="GridView1_RowEditing"
    Width="100%">
    <Columns>
        <asp:BoundField DataField="TimeID" HeaderText="TimeID" ReadOnly="true" Visible="false" />
        <asp:BoundField DataField="WeekDayStart" HeaderText="Week Day Start" />
        <asp:BoundField DataField="WeekDayEnd" HeaderText="Week Day End" />
        <asp:BoundField DataField="StartTime" HeaderText="Start Time" />
        <asp:BoundField DataField="EndTime" HeaderText="End Time" />
    </Columns>
</asp:GridView>
// OnRowDataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Converts the text of WeekDayStart and WeekDayEnd to be actual week days
    if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
    {
        e.Row.Cells[2].Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[Convert.ToInt32(e.Row.Cells[2].Text) - 1];
        e.Row.Cells[3].Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[Convert.ToInt32(e.Row.Cells[3].Text) - 1];
    }
}

// OnRowEditing
// CURRENTLY THROWING ERROR WHEN EDIT BUTTON IS CLICKED (JScript runtime error)
// "Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format."
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.Rows[e.NewEditIndex].Cells[2].Text = (Array.IndexOf(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames,
        GridView1.Rows[e.NewEditIndex].Cells[2].Text) + 1).ToString();
    GridView1.Rows[e.NewEditIndex].Cells[3].Text = (Array.IndexOf(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames,
        GridView1.Rows[e.NewEditIndex].Cells[3].Text) + 1).ToString();
}

正在编辑中更改GridView单元格文本“输入字符串格式不正确”.c# ASP.NET

错误发生在服务器端,你得到一个javascript错误,因为你正在使用asp.net ajax(一个更新面板可能)。在服务器上进行调试,它将捕获您的错误。您可能正在尝试解析空字符串或其他无效值。