我的网页上有几个验证器和验证摘要。这些是在加载页面时设置的,因为它们只是普通表达式和必需字段之类的东西。
我还有一些其他情况,当用户填写文本框时,系统必须保存检查它是否有效。我想在摘要中显示另一条消息以及最初创建的验证器,以显示该条目无效。
我尝试过以下操作,但是在保存时没有显示任何内容。在保存按钮单击开始时调用此方法,并且应该向验证摘要添加消息并返回isvalid = false。
protected bool ValidProductCode()
{
bool lValid = false;
if (!String.IsNullOrEmpty(txtProductCode.Text))
{
try
{
using (SqlConnection conn = new SqlConnection(GetConnection.GetConnectionString()))
{
DataSet ds = new DataSet();
SqlCommand sqlComm = new SqlCommand("PL_ProductCodes_FillScreen", conn);
sqlComm.Parameters.AddWithValue("@SiteID", ddlSiteId.SelectedValue);
sqlComm.Parameters.AddWithValue("@ProductCode", txtProductCode.Text);
sqlComm.Parameters.AddWithValue("@IsOntext", 1);
sqlComm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlComm;
da.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
lValid = true;
}
}
}
catch (SqlException ex)
{
ExceptionHandling.SQLException(ex, constPageID, constIsSiteSpecific);
}
}
CustomValidator err = new CustomValidator();
err.IsValid = false;
err.ErrorMessage = "The product code entered is invalid";
Page.Validators.Add(err);
return lValid;
}
答案 0 :(得分:0)
代码隐藏:
protected void ValidateProductCode(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(args.Value))
{
args.IsValid = true;
}
else
{
try
{
using (SqlConnection conn = new SqlConnection(GetConnection.GetConnectionString()))
using (SqlCommand sqlComm = new SqlCommand("PL_ProductCodes_FillScreen", conn))
{
sqlComm.Parameters.AddWithValue("@SiteID", ddlSiteId.SelectedValue);
sqlComm.Parameters.AddWithValue("@ProductCode", txtProductCode.Text);
sqlComm.Parameters.AddWithValue("@IsOntext", 1);
sqlComm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(sqlComm);
DataSet ds = new DataSet();
da.Fill(ds);
args.IsValid = ds.Tables[0].Rows.Count != 0;
}
}
catch (SqlException ex)
{
ExceptionHandling.SQLException(ex, constPageID, constIsSiteSpecific);
args.IsValid = false;
}
}
}
标记:
<asp:CustomValidator ID="validProductCode" runat="server"
ControlToValidate="txtProductCode"
OnServerValidate="ValidateProductCode"
ErrorMessage="The product code entered is invalid"
Display="Dynamic"
/>