【问题标题】:How to display the Error Message in Asp.Net Page using C#?如何使用 C# 在 Asp.Net 页面中显示错误消息?
【发布时间】:2012-09-03 01:46:00
【问题描述】:

我的 aspx 页面中有几个下拉框,我也有搜索按钮。

如果用户在没有选择任何下拉框的情况下单击“搜索”按钮,我想显示“请选择任何值”之类的消息

我的页面是动态创建的,所有的控件都是动态创建的。而且我不想使用任何静态 aspx 页面控件(如(文本框、标签等))显示错误消息

我想显示我在我的 aspx.cs 文件中引用的其他一些 .cs 文件的错误消息。

我尝试过如下方法:但是它不起作用。

if (first!= "-- Select The Item --" || second != "-- Select The Item --")
            {
                //Do Something
            }
            else
            {

                throw new Exception("Please select any Filter Type");
            }

【问题讨论】:

    标签: c#


    【解决方案1】:

    您可以使用RequiredFieldValidator 请查看以下链接:

    How to add a RequiredFieldValidator to DropDownList control?

    【讨论】:

    • 我需要说例如:string a = "hi" if(a !=""){//do} else {show some message in the page} 但我正在检查这些东西我的通用 .cs 文件,不在 aspx.cs 文件中
    【解决方案2】:

    假设dd1dd2DropDownList 控件ID,您可以检查SelectedIndex 属性:

    if (dd1.SelectedIndex != 0 || dd2.SelectedIndex != 0)
    {
        //Do Something
    }
    else
    {
    
        throw new Exception("Please select any Filter Type");
    }
    

    【讨论】:

    • 我需要说例如:string a = "hi" if(a !=""){//do} else {show some message in the page} 但我正在检查这些东西我的通用 .cs 文件,不在 aspx.cs 文件中
    【解决方案3】:

    那么 javascript 可以用来验证下拉列表....看看下面的链接 How to validate dynamically created control?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      相关资源
      最近更新 更多