我在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;
}
}
}
答案 0 :(得分:0)
我想你会想要做一些像
这样的事情首页:
<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;
}
}
}
希望这会有所帮助
答案 1 :(得分:0)
我能够通过将验证逻辑移出ServerValidate事件并进入更新按钮单击事件来处理此问题。
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;
}
}
}
}