Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改GridView单元格文本OneEdit“;输入字符串的格式不正确。”;C#ASP.NET_C#_Asp.net_Sql_Gridview - Fatal编程技术网

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

更改GridView单元格文本OneEdit“;输入字符串的格式不正确。”;C#ASP.NET,c#,asp.net,sql,gridview,C#,Asp.net,Sql,Gridview,我试图在一个ASP.NET网站上显示一个相当简单的每周时间表,该网站与SQL表相关联。我遇到的问题是,星期日值作为整数(1到7)保存在SQL表中。我想向用户显示整数作为实际的周日名称,这只需使用OnRowDataBound事件查找即可。但是,当我将工作日改回整数进行编辑时,我得到了一个JScript错误,即“输入字符串的格式不正确” 起初,我认为这可能是一个问题,其中一个细胞,然后是我正在处理的两个细胞,但据我所知,情况并非如此。不幸的是,JScript错误没有给我足够的信息来真正调试这个问题,

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

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

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

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

//在线数据绑定
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
//将WeekDayStart和WeekDayEnd的文本转换为实际的周天数
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];
}
}
//在线编辑
//单击“编辑”按钮时当前引发错误(JScript运行时错误)
//“Sys.WebForms.PageRequestManagerServerErrorException:输入字符串的格式不正确。”
受保护的无效GridView1_行编辑(对象发送方,GridViewEditEventArgs e)
{
GridView1.Rows[e.NewEditIndex].Cells[2].Text=(Array.IndexOf(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames,
GridView1.Rows[e.NewEditIndex]。单元格[2]。文本)+1)。ToString();
GridView1.Rows[e.NewEditIndex].Cells[3].Text=(Array.IndexOf(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames,
GridView1.Rows[e.NewEditIndex]。单元格[3]。文本)+1)。ToString();
}

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

错误发生在服务器端,您会收到一个javascript错误,因为您使用的是asp.net ajax(可能是更新面板)。在服务器上调试,它将捕获您的错误。猜测您试图解析空字符串或其他无效值。@BenRobinson您是对的,我使用的是更新面板。我删除它是为了进行一些调试,当我进入编辑模式时,会调用RowDataBound,这就是我得到格式异常的地方。我刚刚在我的RowDataBound中添加了一个检查,以检查单元格2和单元格3是否不等于“”,并修复了它。谢谢你的帮助!你应该把这个建议作为答案贴出来,这样我就可以接受了:)。