I am displaying values from a database into a dropdownlist. but I am unable to get the corresponding values into the DDL.
if (dt.Rows.Count > 0)
{
DataTable dt1 = new DataTable();
dt1 = bll.getnewid(TextBox1.Text);
if (dt1.Rows.Count > 0)
{
Session["pid"] = dt1.Rows[0]["NewidColumn"].ToString();
Session["email"] = dt1.Rows[0]["EmailID"].ToString();
Session["gender"] = dt1.Rows[0]["Gender"].ToString();
Session["mobile"] = dt1.Rows[0]["MobileNo"].ToString();
Session["country"] = dt1.Rows[0]["Country"].ToString();
Session["state"] = dt1.Rows[0]["State"].ToString();
}
and I am displaying like this
DropDownList1.Text = Session["country"].ToString();
DropDownList2.Text = Session["state"].ToString();
I am able to get the country and state values in the datatable. but I am unable to display them in the DDL.
DropDownList1.Items.Add(new ListItem(Session["country"].ToString());
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());
DropDownList1.Items.Add(new ListItem(Session["country"].ToString());
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());
Dropdownlist2.databind();
Dropdownlist1.databind();
您需要设置下拉列表的SelectedValue
或SelectedIndex
属性。
try like this
DropDownList1.Items.Add("yourvalue");
DropDownList1.Items.Add(Session["country"].ToString());
Try This Code :
DropDownList1.Items.Insert(0, new ListItem(Session["country"].ToString(), "0"));
DropDownList1.DataBind();
DropDownList2.Items.Insert(0, new ListItem(Session["state"].ToString(), "0"));
DropDownList2.DataBind();
What is the need of using sessions ..? Instead try this..hope it works. After this line of code (dt1 = bll.getnewid(TextBox1.Text);) use this two lines instead of sessions.
DropDownList1.DataSource=dt1;
DropDownList1.DataTextField="Country";
DropDownList1.DataValueField="Country";
DropDownList1.DataBind();
DropDownList2.DataSource=dt1;
DropDownList2.DataValueField="Country";
DropDownList2.DataValueField="Country";
DropDownList2.DataBind();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.