繁体   English   中英

在下拉列表中显示DB值

[英]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();

您需要设置下拉列表的SelectedValueSelectedIndex属性。

试试这样

   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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM