2016-08-09 27 views
1

我想在我編輯一些信息時對Gridview進行驗證,但我不知道如何。我不使用簡單的方法,我的代碼(C#)做這件事,我不知道如何在我的文本框添加驗證...驗證GridView_RowUpdating textfields ASP.NET

下面是我的一些代碼:

protected void gvAdmins_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    DataTable dt = (DataTable)Session["TaskTableA"]; 

    GridViewRow row = gvAdmins.Rows[e.RowIndex]; 

    dt.Rows[row.DataItemIndex]["ID"] = ((TextBox)(row.Cells[1].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["H:"] = ((TextBox)(row.Cells[4].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["M:"] = ((TextBox)(row.Cells[5].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex][6] = ((TextBox)(row.Cells[7].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex][7] = ((TextBox)(row.Cells[8].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex][8] = ((TextBox)(row.Cells[9].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex][9] = ((TextBox)(row.Cells[10].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex][10] = ((TextBox)(row.Cells[11].Controls[0])).Text; 

    gvAdmins.EditIndex = -1; 

    int id = Convert.ToInt32(Request.QueryString["id"]); 
    string idCar = ((TextBox)(row.Cells[1].Controls[0])).Text; 
    int hom = Convert.ToInt32(((TextBox)(row.Cells[4].Controls[0])).Text); 
    int mu = Convert.ToInt32(((TextBox)(row.Cells[5].Controls[0])).Text); 
    int t1 = 0; 
    int t2 = 0; 
    int t3 = 0; 
    int t4 = 0; 
    int t5 = 0; 
    t1 = Convert.ToInt32(((TextBox)(row.Cells[7].Controls[0])).Text); 
    t2 = Convert.ToInt32(((TextBox)(row.Cells[8].Controls[0])).Text); 
    t3 = Convert.ToInt32(((TextBox)(row.Cells[9].Controls[0])).Text); 
    t4 = Convert.ToInt32(((TextBox)(row.Cells[10].Controls[0])).Text); 
    t5 = Convert.ToInt32(((TextBox)(row.Cells[11].Controls[0])).Text); 

    int total = hom + mu; 
    int totalT = t1 + t2 + t3 + t4 + t5; 

    string comandoIF = //MY SQL Command; 

    conn.IAE(comandoIF); 

    dt.Rows[row.DataItemIndex]["Total:"] = total.ToString(); 

    BindDataA(); 
} 

我希望你能幫助我,謝謝。

回答

0

您應該首先將文本框或正在gvAdmins_RowUpdating中處理的其他元素的內容放入變量並驗證它們。

對於你可以做這樣的字符串:

string H = ((TextBox)(row.Cells[4].Controls[0])).Text; 
    if (H == "correctValue") 
    { 
     dt.Rows[row.DataItemIndex]["H:"] = H; 
    } 
    else 
    { 
     //the input was not correct 
    } 

對於需要轉換像整數,日期,布爾等你做這樣的事的值。您必須從TextBox(始終是一個字符串)轉換值。

int ID = Convert.ToInt32(((TextBox)(row.Cells[1].Controls[0])).Text); 
    if (ID > 500 && ID < 600) 
    { 
     dt.Rows[row.DataItemIndex]["ID"] = ID; 
    } 
    else 
    { 
     //the input was not correct 
    } 

但是有一個問題,如果從文本框中的值不能轉換成整數代碼將特羅錯誤。爲了防止這種情況,你可以使用try-catch塊:

int ID = -1; 
    //try the conversion in a try-catch block. If conversion fails your site won't trow an error 
    try 
    { 
     ID = Convert.ToInt32(((TextBox)(row.Cells[1].Controls[0])).Text); 
    } 
    catch 
    { 
     //error converting to integer, but will continue 
    } 

    //after that do the validating 
    if (ID > 500 && ID < 600) 
    { 
     dt.Rows[row.DataItemIndex]["ID"] = ID; 
    } 
    else 
    { 
     //the input was not correct 
    } 

注意上面的例子是非常基本的。如果你願意,你可以使驗證非常複雜和嚴格。你應該在獲得數據庫中可用的驗證數據和用戶體驗等之間找到平衡點。

我也推薦你也使用驗證器形式的客戶端驗證。這些鏈接可以讓你開始說:

https://msdn.microsoft.com/en-us/library/ms972961.aspx

https://msdn.microsoft.com/en-us/library/a0z2h4sw.aspx

+0

好吧,我問別人,他告訴我,我可以使用腳本和CSS類把文本框的類型到數量,所以我沒有...這有點爲我工作,謝謝 OPMUANRK

+0

沒錯,那也可以。但請記住,舊版瀏覽器不會重構這些屬性並顯示正常的文本框。 – VDWWD