簡體   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