List dropdown value in asp.net

I want to add a value to a drop-down list that cannot be selected, such as a name. Example: I have a drop-down list per month. The very first item should be a "select month", which should not be selected. And then from January to December. How can i do this?

And this is my current code.

string selectmonth = "select * from tblmonth"; SqlCommand scmselect = new SqlCommand(selectmonth, scnbuboy); SqlDataReader sdrselect = scmselect.ExecuteReader(); drmonth.DataTextField = "month"; drmonth.DataValueField = "monthID"; drmonth.DataSource = sdrselect; drmonth.DataBind(); 
+5
source share
9 answers
 <asp:DropDownList ID="DdlMonths" runat="server"> <asp:ListItem Enabled="true" Text="Select Month" Value="-1"></asp:ListItem> <asp:ListItem Text="January" Value="1"></asp:ListItem> <asp:ListItem Text="February" Value="2"></asp:ListItem> .... <asp:ListItem Text="December" Value="12"></asp:ListItem> </asp:DropDownList> 

You can even use RequiredFieldValidator , which ignore this element, it considers it unselected.

 <asp:RequiredFieldValidator ID="ReqMonth" runat="server" ControlToValidate="DdlMonths" InitialValue="-1"> </asp:RequiredFieldValidator> 
+11
source

You can add an empty value, for example:

 ddlmonths.Items.Insert(0, new ListItem("Select Month", "")) 

And just add validation to prevent using an empty parameter like asp:RequiredFieldValidator .

+2
source

These are ALL great answers if you want to work so hard. But I assume that you already have the items you want for the list coming from the database data item, and only want to add “Hello, man!” To the top of this list. option. Suppose this is so ...

Here is an EASY RESPONSE. And he ALWAYS works ...

  • Make your data list exactly as you planned.
  • Then, in Visual Studio, edit the items in the drop-down menu,
  • Add ONE LEADER, select " Select an item ",
  • Using the properties window for an item in VS2012, check it as selected. Now close this window.
  • Now go to the properties window in Visual Studio in the lower left hand (make sure the drop-down menu is selected) and find the "AppendDataBoundItems" property.
  • It will read False, set this to True.

You will now receive Drop Down with all your data items in it, SUGGESTED in the “Select Product” statement made in a manual item. If possible, try to give it a default value, this will fix any errors that you may encounter. The default value is Zero, so if zero is not a problem, leave it alone; if zero is not a problem, replace the default null parameter with something that will NOT cause your code to crash.

And stop working so hard ... what Visual Studio is for.

+1
source

try this one

  <asp:DropDownList ID="ddList" runat="server"> <asp:ListItem Value="">--Select Month--</asp:ListItem> <asp:ListItem Value="1">January</asp:ListItem> <asp:ListItem Value="2">Feburary</asp:ListItem> ... <asp:ListItem Value="12">December</asp:ListItem> </asp:DropDownList> 

The value should be empty for the selected item by default, then it works fine

+1
source

Let's say you have a ddlMonths drop down list:

 ddlMonths.Items.Insert(0,new ListItem("Select a month","-1"); 
0
source

In a simple way, this is not possible. Since DropdownList contains ListItem and it will be selected by default

But you can use ValidationControl for this:

 <asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic" ValidationGroup="g1" runat="server" ControlToValidate="ControlID" Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator> 
0
source

You can try this

 your_ddl_id.Items.Insert(0,new ListItem("Select",""); 
0
source

VB Code:

 Dim ListItem1 As New ListItem() ListItem1.Text = "put anything here" ListItem1.Value = "0" drpTag.DataBind() drpTag.Items.Insert(0, ListItem1) 

Look:

 <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator" ControlToValidate="drpTag" ValueToCompare="0"> </asp:CompareValidator> 
0
source

To add items to a dropdown with a value in asp.net using C #, just add the following codes, for example:

 try { SqlConnection conn = new SqlConnection(conStr); SqlCommand comm = conn.CreateCommand(); comm = conn.CreateCommand(); comm.CommandText = "SELECT title,gid from Groups"; comm.CommandType = CommandType.Text; conn.Open(); SqlDataReader dr = comm.ExecuteReader(); while (dr.Read()) { dropDownList.Items.Add(new ListItem(dr[0].ToString(),dr[1].ToString())); } } catch (Exception e) { lblText.Text = e.Message; } finally { conn.Close(); } 
0
source

Source: https://habr.com/ru/post/1498171/


All Articles