Validate GridView_RowUpdating textfields ASP.NET

本文关键字:ASP NET textfields RowUpdating GridView Validate | 更新日期: 2023-09-27 18:01:20

当我编辑一些信息时,我想对我的Gridview进行验证,但我不知道如何。我没有使用简单的方法,我是通过代码(c#)来做的,我不知道如何在我的文本字段上添加验证…

下面是我的一些代码:

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();
}
我希望你能帮助我,谢谢。

Validate GridView_RowUpdating textfields ASP.NET

你应该首先把文本框的内容或其他元素,正在处理的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
    }

但是有一个问题,如果来自TextBox的值不能转换为整数,你的代码将会产生一个错误。为了防止这种情况,你可以使用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