Home > DeveloperSection > Forums > If statement on dropDownList listItem
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    November-20-2014 10:42 PM

 ASP.Net ASP.Net  Dropdown 
Ratings:


 1 Reply(s)
 680  View(s)
Rate this:

I need to add a condition to a DropDownList where a method can be executed by button click only if the user has selected a value different than the listItem (default value).

 

<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>

How can I structure an if condition to validate that the selected value is not the ListItem (default value)?



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    November-20-2014 11:49 PM

You can use asp.net delivered validation controls

<asp:RequiredFieldValidator id="rfv1"

                    ControlToValidate="DropDownList1"

                    Display="Static"

                    ErrorMessage="* Select a value"

                    InitialValue="DefaultValueHere"

                    runat="server"

                    ValidationGroup="V1"/>

Then edit your button markup to use ValidationGroup

 

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

In your codebehind button click code add this

 

protected void button1_onlick(Object sender, EventArgs e)

{

     If(Page.IsValid)

     {

       // your existing code here

     }

}


Don't want to miss updates? Please click the below button!

Follow MindStick