Reputation: 301
i am using this code to fill dropdownlist from database.
public void fillcountry()
{
BL obj = new BL();
DataSet ds = obj.dss("select * from Country ");
drplistcountry.DataSource = ds;
drplistcountry.DataTextField = "CountryName";
drplistcountry.DataValueField = "CountryId";
drplistcountry.DataBind();
drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
}
i am Using this fillcountry() in page load() event. and Rerutning selecteditm.text on Button Click event
drplistcountry is always showing First index text , How to solve it?
Upvotes: 6
Views: 33362
Reputation: 551
I was also facing same issue but in my case dropdown value was same for all items, which was causing issue. So, make sure you are binding dropdown list correctly.
:)
Upvotes: -2
Reputation: 139
DropdownList selected Item Text value will work like this.
String value="India";
drplist.SelectedIndex = drplist.Items.IndexOf(drplist.Items.FindByText(value));
Upvotes: 0
Reputation: 1
Its is very simple but very difficult to find out, check your value Datavaluefield
of all the items should not be blank or null.
Means:
ddl_Name.DataTextField = "NAME";
ddl_Name.DataValueField = "Roll";
if value is blank means you cannot will get the first items only.
Upvotes: 0
Reputation: 1
An easy way You can use repeater to make select in html code like this
<select id="ddlAlbum" class="form-control" name="Cat">
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<option value='<%#Eval("CatID") %>'>
<%#Eval("Title") %>
</option>
</ItemTemplate>
</asp:Repeater>
</select>
for get data from data base use ado.net
public void fillAlbum()
{
SqlConnection myconnection = new SqlConnection("Data Source=.;Initial Catalog=person;Integrated Security=True");
SqlDataAdapter myadapter = new SqlDataAdapter("Select CatID,Title from Category", myconnection);
DataTable dt = new DataTable();
myadapter.Fill(dt);
rpt.DataSource = dt;
rpt.DataBind();
}
and for get selected value. the select element name is ("cat") So
string cat = Request.Form["Cat"];
and store to db simply
SqlConnection myconnection = new SqlConnection("Data Source=.;Initial Catalog=person;Integrated Security=True");
SqlCommand mycommand = new SqlCommand("insert into Picture(Title,Pic,CatID) values(@pname,@pic,@cat)", myconnection);
mycommand.Parameters.AddWithValue("pname", txtname.Text.Trim());
mycommand.Parameters.AddWithValue("pic", path);
string cat = Request.Form["Cat"];
mycommand.Parameters.AddWithValue("cat", cat.ToString());
myconnection.Open();
mycommand.ExecuteNonQuery();
myconnection.Close();
Upvotes: 0
Reputation: 1309
Try this code
And you shoud call this function only once.
public void fillcountry()
{
BL obj = new BL();
DataSet ds = obj.dss("select * from Country ");
drplistcountry.DataSource = ds;
drplistcountry.DataTextField = "CountryName";
drplistcountry.DataValueField = "CountryId";
drplistcountry.DataBind();
drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
drplistcountry.SelectedIndex = drplistcountry.Items.IndexOf(drplistcountry.Items.FindByText("--Select--"));
}
Upvotes: 1
Reputation: 5430
In .aspx page:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default"
EnableViewState="true" %>
For Dropdownlist Control set EnableViewState property to true.
In .aspx.cs page:
In PageLoad event check for following:
if(!IsPostBack)
{
fillcountry();
}
Upvotes: 14