locked
How to display "select value" by defaul in dropdownlist? RRS feed

  • Question

  • User319971903 posted

    In my application my dropdownlist is bounded with sqldatasource.

    By default it was displaying the first row of the select statement.

    What i need is to specify some text like "select value" in the dropdownlist initially. 

    Can anyone give me a code for this?

    Tuesday, July 26, 2011 10:18 AM

Answers

  • User77042963 posted

    You can use UNION ALL to add the first item in your SELECT query or you can add the item in your markup.

         <asp:DropDownList ID="DropDownList1" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="Col1" 
                DataValueField="Col1" AppendDataBoundItems="true">
                <asp:ListItem Value="">--Select One--</asp:ListItem>
            </asp:DropDownList>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 26, 2011 11:36 AM
  • User77042963 posted
     <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"  
                DataSourceID="SqlDataSource1" DataTextField="Col1"  OnSelectedIndexChanged="aa" 
                DataValueField="Col1" AppendDataBoundItems="true"> 
                <asp:ListItem Value="">--Select One--</asp:ListItem> 
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  InitialValue=""
            ErrorMessage="RequiredFieldValidator" ControlToValidate="DropDownList2"></asp:RequiredFieldValidator>
           
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 26, 2011 2:38 PM

All replies

  • User77042963 posted

    You can use UNION ALL to add the first item in your SELECT query or you can add the item in your markup.

         <asp:DropDownList ID="DropDownList1" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="Col1" 
                DataValueField="Col1" AppendDataBoundItems="true">
                <asp:ListItem Value="">--Select One--</asp:ListItem>
            </asp:DropDownList>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 26, 2011 11:36 AM
  • User319971903 posted

     Actually what i am doing is whenver i select item in dropdownlist i enables autopostback and based on selected values i retrieve row and then display that by using label. So when i use the above technique u specified it was working fine but when we select "select one" it will be a problem since there will be no row's with the specified value.Can u tell me how to modify this? 

    here is the code that i am using for displaying text with label:

     Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
    Dim cs As String
    cs = "Data Source=192.168.2.4\sqlexpress;Initial Catalog=test;Integrated Security=True"
    Dim oSQLConn As SqlConnection = New SqlConnection(cs)
    oSQLConn.Open()
    Dim query, time As String
    query = "select recent_xmlgen_timestamp from datasource where Doctor_id='" + DropDownList1.SelectedValue + "'"
    Dim ds As New DataSet
    Dim da As New SqlDataAdapter(query, oSQLConn)
    da.Fill(ds, "test")
    time = CType(ds.Tables("test").Rows(0).Item(0), String) // this will give error if i select default text in dropdownlist.
    Label7.Text = "Recent XML generated timestamp:"
    Label7.Text += time
    End Sub
    Tuesday, July 26, 2011 11:48 AM
  • User77042963 posted
     <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"  
                DataSourceID="SqlDataSource1" DataTextField="Col1"  OnSelectedIndexChanged="aa" 
                DataValueField="Col1" AppendDataBoundItems="true"> 
                <asp:ListItem Value="">--Select One--</asp:ListItem> 
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  InitialValue=""
            ErrorMessage="RequiredFieldValidator" ControlToValidate="DropDownList2"></asp:RequiredFieldValidator>
           
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 26, 2011 2:38 PM
  • User319971903 posted

    Thanks it works!!!!

    Tuesday, July 26, 2011 3:14 PM