简体   繁体   English

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

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

I am trying to display a fairly simple weekly schedule on an ASP.NET website that ties into an SQL table. 我试图在绑定到SQL表的ASP.NET网站上显示一个相当简单的每周计划。 The problem I am having is I the week day values are held in the SQL table as integers (1 through 7). 我遇到的问题是,星期几的值以整数形式(1到7)保存在SQL表中。 I wanted to display the integers to the user as actual week day names, which works just find using the OnRowDataBound event. 我想将整数显示为用户的实际星期几名称,但可以使用OnRowDataBound事件找到该整数。 But, when I change the weekdays back to integers for editing I am getting a JScript error that the "Input string was not in the correct format". 但是,当我将工作日改回整数以进行编辑时,出现了JScript错误,即“输入字符串的格式不正确”。

At first I thought it may be an issue with one of the cells other then the two I am messing with, but as far as I can tell that is not the case. 起初,我以为可能是其中一个单元而不是我要弄乱的两个单元的问题,但据我所知并非如此。 Unfortunately the JScript error is not giving me enough information to really debug this problem, and I know that the OnEditing event is successfully changing the text of the two boundfields back to a number 不幸的是,JScript错误没有给我足够的信息来真正调试此问题,并且我知道OnEditing事件已成功将两个边界域的文本改回了一个数字。

So my question is, what is the best way to fix this? 所以我的问题是,解决此问题的最佳方法是什么? Is there a better way to display week day names to the user then change those names to integers when they want to edit a row? 有没有更好的方法向用户显示星期几的名称,然后在他们要编辑行时将这些名称更改为整数? Is there a way I can change the edit template to use something like a drop down list or something like that? 有没有一种方法可以更改编辑模板以使用诸如下拉列表之类的东西?

<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();
}

The error is happening server side, you are getting a javascript error because you are using asp.net ajax (an update panel maybe). 该错误发生在服务器端,由于使用asp.net ajax(可能是更新面板),因此出现了JavaScript错误。 Debug on the server and it will catch your error. 在服务器上调试,它将捕获您的错误。 At a guess you are trying to parse an empty string or some other invalid value. 猜测是您试图解析空字符串或其他无效值。

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

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