循环遍历GridView行并验证ItemTemplate中包含的每个TextBox

本文关键字:包含 TextBox ItemTemplate GridView 遍历 验证 循环 | 更新日期: 2023-09-27 18:08:34

我在gridview中有一个数据绑定文本框,使用户能够轻松快速地批量更新行。我已经尝试使用CustomValidator来验证每个文本框对一个SQL列,但它的行为方式不像我需要它。CustomValidator代码在TextChanged事件中正常工作,但是在ServerValidate事件中行为不正常(我理解为什么)。如果我将代码留在TextChanged事件处理程序中,它仍然允许在点击我创建的Update按钮时修改数据。如何针对SQL数据单独有效地验证每个TextBox ?

    <ItemTemplate>
<asp:TextBox ID="Account" runat="server" AutoPostBack="true" OnTextChanged="Account_TextChanged" Text='<%# Bind("Account") %>'></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" CssClass="CustomValidator" ValidateEmptyText="false" SetFocusOnError="True" Display="Dynamic" ControlToValidate="Account" OnServerValidate="Validate_ServerValidate" ErrorMessage="Custom Validator"></asp:CustomValidator>
</ItemTemplate>
foreach (GridViewRow row in GridView2.Rows)
{
    TextBox Account = row.FindControl("Account") as TextBox;
    CustomValidator validator = row.FindControl("CustomValidator1") as CustomValidator;
    string sAccount = Account.Text;
    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT Account Table WHERE Account = @Account", conn))
        {
                    da.SelectCommand.Parameters.Add("@Account", SqlDbType.VarChar);
                    da.SelectCommand.Parameters["@Account"].Value = sAccount;
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        validator.IsValid = true;
                    }
                    else
                    {
                        validator.IsValid = false;
                    }
                }
            }

循环遍历GridView行并验证ItemTemplate中包含的每个TextBox

我想你会想做一些像

首页

:

    <asp:TextBox runat="server" ID="txtValidateMe"></asp:TextBox>
    <asp:CustomValidator runat="server" id="validateTheTextBox" OnServerValidate="validateTheTextBox_OnServerValidate" ControlToValidate="txtValidateMe"/>
服务器端

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            //do something
        }
    }
    protected void validateTheTextBox_OnServerValidate(object source, ServerValidateEventArgs args)
    {
        using (var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
        using (var da = new SqlDataAdapter("SELECT Account Table WHERE Account = @Account", conn))
        {
            da.SelectCommand.Parameters.Add("@Account", SqlDbType.VarChar);
            da.SelectCommand.Parameters["@Account"].Value = args.Value;
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }

希望能有所帮助

我可以通过将验证逻辑从ServerValidate事件移到update buttonclick事件中来处理这个问题。

protected void update_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in SomeGrid.Rows)
                {
                    TextBox SomeTextBox = row.FindControl("SomeTextBox") as TextBox;
                    CustomValidator validator = row.FindControl("SomeValidator") as CustomValidator;
                    string Account = SomeTextBox.Text;
                    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                    using (SqlDataAdapter da = new SqlDataAdapter("SELECT Account FROM Account WHERE Account = @Account", conn))
                    {
                        da.SelectCommand.Parameters.Add("@Account", SqlDbType.VarChar);
                        da.SelectCommand.Parameters["@Account"].Value = Account;
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            validator.IsValid = true;
                        }
                        else
                        {
                            validator.IsValid = false;
                        }
                    }
                }
            }