繁体   English   中英

更改GridView单元格文本OnEditing“输入字符串的格式不正确。” C#ASP.NET

[英]Changing GridView Cell Text OnEditing “Input string was not in a correct format.” C# ASP.NET

我试图在绑定到SQL表的ASP.NET网站上显示一个相当简单的每周计划。 我遇到的问题是,星期几的值以整数形式(1到7)保存在SQL表中。 我想将整数显示为用户的实际星期几名称,但可以使用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();
}

该错误发生在服务器端,由于使用asp.net ajax(可能是更新面板),因此出现了JavaScript错误。 在服务器上调试,它将捕获您的错误。 猜测是您试图解析空字符串或其他无效值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM