【问题标题】:Using a validation summary to display label text使用验证摘要显示标签文本
【发布时间】:2013-08-08 23:51:05
【问题描述】:

我已经接管了一个通过 WCF 服务进行调用的站点的维护工作。对于客户端的某些验证器,有一个验证摘要显示在消息框中。如果我的 Web 服务调用出现任何异常,我也有一个标签。调整布局后,标签位于不方便的位置。我想知道如果后面的代码中发生异常,是否有办法在验证摘要中显示标签文本。

感谢任何建议。

cs文件中的示例:

bool ResultUserExistence = register.CheckUniquenessUserID(txtUserId.Text);

if (ResultUniquenessEmail == null)
{
    continue through code...
}

else
{
    lblException.Text = "Please choose a different user name. The current user name is already registered.";
}

验证摘要:

<asp:ValidationSummary ID="valSummary"
                    runat="server"
                    HeaderText="Please correct the following error(s):"
                    DisplayMode="List"
                    ForeColor="#FF9999"
                    ShowMessageBox="True"
                    ShowSummary="False" />

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    this answer 中所述,您可以创建一个自定义验证器,并在其上设置消息,这将使消息显示在您的验证摘要中。

    bool ResultUserExistence = register.CheckUniquenessUserID(txtUserId.Text);
    
    if (ResultUniquenessEmail == null)
    {
        continue through code...
    }
    
    else
    {
        var err As new CustomValidator()
        err.ValidationGroup = "UserUniqueness";
        err.IsValid = False;
        err.ErrorMessage = "Please choose a different user name. The current user name is already registered.";
        Page.Validators.Add(err);    
    
    }
    

    理想情况下,这将被分解为可重用的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2023-03-05
      相关资源
      最近更新 更多