[英]Display DB values in a dropdownlist
我正在將數據庫中的值顯示到下拉列表中。 但我無法將相應的值輸入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();
}
我正在這樣展示
DropDownList1.Text = Session["country"].ToString();
DropDownList2.Text = Session["state"].ToString();
我能夠在數據表中獲取國家和州的值。 但是我無法在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
屬性。
試試這樣
DropDownList1.Items.Add("yourvalue");
DropDownList1.Items.Add(Session["country"].ToString());
試試此代碼:
DropDownList1.Items.Insert(0, new ListItem(Session["country"].ToString(), "0"));
DropDownList1.DataBind();
DropDownList2.Items.Insert(0, new ListItem(Session["state"].ToString(), "0"));
DropDownList2.DataBind();
使用會話有什么需要..? 相反,嘗試這個..希望它有效。 在這行代碼之后(dt1 = bll.getnewid(TextBox1.Text);)使用這兩行代替會話。
DropDownList1.DataSource=dt1;
DropDownList1.DataTextField="Country";
DropDownList1.DataValueField="Country";
DropDownList1.DataBind();
DropDownList2.DataSource=dt1;
DropDownList2.DataValueField="Country";
DropDownList2.DataValueField="Country";
DropDownList2.DataBind();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.