C# 如何将数据库字段绑定到下拉列表?
我正在通过一个DAL文件从数据库中请求数据。我想绑定数据并显示一个下拉菜单的单个选项,该选项与数据库的选项相对应。我该怎么做 这是我的密码:C# 如何将数据库字段绑定到下拉列表?,c#,asp.net,C#,Asp.net,我正在通过一个DAL文件从数据库中请求数据。我想绑定数据并显示一个下拉菜单的单个选项,该选项与数据库的选项相对应。我该怎么做 这是我的密码: Pages pg = new Pages(); public static string pgId; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { Bind_ddlParentPage();
Pages pg = new Pages();
public static string pgId;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Bind_ddlParentPage();
Bind_ddlPageModel();
Bind_ddlArticleModel();
if (!IsPostBack)
{
pgId = Request.QueryString["pageId"];
if (pgId != null)
{
GetData();
}
}
}
}
public void GetData()
{
ddlParentPage.SelectedValue = pg.ParentPage;
//Bind_ddlParentPage();---dropdownlist which is causing problem.
//I want to set this data:: pg.ParentPage to dropdownlist in another
page
ddlPageModel.SelectedValue = pg.PageModel;
//Bind_ddlPageModel();
//All the three drop downs have same table for the source,
'Pages' table and this page is the same page for adding new entry to
Pages table.
ddlArticleModel.SelectedValue = pg.ArticleModel;
//Bind_ddlArticleModel();
}
首先,您的条件语句中有一个重复项:
if(!IsPostBack) {
...
if(!IsPostBack) {
...
}
}
其次,需要将数据绑定到下拉列表,然后设置所选值。参考:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
}
}