I am using asp.net for fetching data from sql table to dropdown list. The problem is that, when I give default selection to the dropdown list. It does not take the default value.
Please see the code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from States_agri", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddl_state.DataSource = dr;
ddl_state.Items.Clear();
ddl_state.Items.Add("--Please Select state--");
ddl_state.DataTextField = "StateName";
ddl_state.DataValueField = "StateID";
ddl_state.DataBind();
conn.Close();
}
}
`
Also Please see the dropdown list aspx code for your reference.
<asp:UpdatePanel ID="FormUpdate" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_state" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<table>
<tr>
<td>State*</td>
<td>
<asp:DropDownList ID="ddl_state" runat="server" CssClass="cbfld-popup1" AutoPostBack="true" OnSelectedIndexChanged="ddl_state_SelectedIndexChanged">
<asp:ListItem Enabled="true" Selected="True" Text="Please select State"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>District*</td>
<td>
<asp:DropDownList ID="ddl_district" CssClass="cbfld-popup1" runat="server">
<asp:ListItem Enabled="true" Selected="True" Text="Please select city"></asp:ListItem>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
After data-binding, do this:
ddl_state.Items.Insert(0, new ListItem("Select","NA")
Or add it in markup as:
<asp:DropDownList .. AppendDataBoundItems="true">
<Items>
<asp:ListItem Text="Select" Value="" />
</Items>
</asp:DropDownList>
Method 2
you have to use below menioned code after Databind
ddl_state.Items.Insert(0, new ListItem("--Please Select state--", "0"));
Method 3
you need to set AppendDataBoundItems="true" property in your dropdown list
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"> <asp:ListItem Text="Add New" Value="0" /> </asp:DropDownList>
Method 4
ddl_state.Items.Insert(0, new ListItem("Select","NA")
Method 5
You could set the AppendDataBoundItems to true in the declaration of your DropDownList
<asp:DropDownList AppendDataBoundItems="true" ...>,
This is needed because as it is stated in MSDN
The AppendDataBoundItems property allows you to add items to the
ListControl object before data binding occurs.
Please have a look here.
Method 6
if you want to fetch data from database den this will be helpful:
<asp:DropDownList id="MainCat_DropDownList" runat="server" Width="185px" AutoPostBack="True" DataSourceID="Maincategory_SqlDataSource" DataTextField="main_catname" DataValueField="main_catid" AppendDataBoundItems="True"></asp:DropDownList> <asp:SqlDataSource id="Maincategory_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT main_catid, Title_id, main_catname FROM main_cat WHERE (Title_id = 1)"></asp:SqlDataSource> </TD></TR><TR><TD align=right>Package</TD><TD align=left><asp:DropDownList id="MainPack_DropDownLis" runat="server" Width="185px" AutoPostBack="True" DataSourceID="MainPackage_SqlDataSource" DataTextField="pack_name" DataValueField="pack_id">
</asp:DropDownList>
<asp:SqlDataSource id="MainPackage_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString %>" SelectCommand="select main_catid,pack_id,pack_name from main_pack where <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="533e323a3d0c3032273a376e133e323a3d">[email protected]</a>_catid">
<SelectParameters>
<asp:ControlParameter ControlID="MainCat_DropDownList" Name="main_catid" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Method 7
The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs.
if we will do this so it will append item from databases in it will add all items from column whenever we do selection and overloaded with all values from column.
Consider following example of :
1. DDL Country [if we will select DDLcountry so it should reflect DDLState]
2. DDL State [if we will select DDLstate so it should reflect DDLcity]
3. DDL City
but whenever we select new DDLcountry so DDLState should refelect with ‘–select state–‘ and DDLcity should be ‘–select City–‘
Method 8
In this example the 3rd parameter is the default value :
@Html.DropDownList("Items", new SelectList(ViewBag.Items), (string)ViewBag.MyFilter.ToString())
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0