如果在dropDownList listItem上声明

时间:2013-11-29 21:10:53

标签: c# asp.net

我需要向DropDownList添加一个条件,只有当用户选择了不同于listItem(默认值)的值时,才能通过按钮单击执行方法。

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
     DataSourceID="SqlDataSource5" 
     DataTextField="proj_name" DataValueField="proj_name">
        <asp:ListItem Text="Select a project to clone" Value="" />
</asp:DropDownList>

如何构造if条件以验证所选值是否不是ListItem(默认值)?

2 个答案:

答案 0 :(得分:3)

您可以使用asp.net提供的验证控件

例如:

<asp:RequiredFieldValidator id="rfv1"
                    ControlToValidate="DropDownList1"
                    Display="Static"
                    ErrorMessage="* Select a value"
                    InitialValue="DefaultValueHere"
                    runat="server"
                    ValidationGroup="V1"/> 

然后编辑按钮标记以使用ValidationGroup

<asp:Button Id="button1" ValidationGroup="V1" .../>

在您的代码隐藏按钮中,单击代码添加此

protected void button1_onlick(Object sender, EventArgs e)
{
     If(Page.IsValid)
     {

       // your existing code here
     }
}

答案 1 :(得分:0)

参见下面的示例代码

if (DropDownList1.SelectValue == "")
{
// Write your code here
}

你也可以:

if (DropDownList1.Text == "Select a project to clone")
{
// Write your code here
}
相关问题