If statement on dropDownList listItem

Total Post:101

 1349  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="" />

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

  1. Post:378

    Re: If statement on dropDownList listItem

    You can use asp.net delivered validation controls

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

    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)
           // your existing code here

      Modified On Apr-06-2018 11:51:51 PM