Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据库字段绑定到下拉列表?_C#_Asp.net - Fatal编程技术网

C# 如何将数据库字段绑定到下拉列表?

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();

我正在通过一个DAL文件从数据库中请求数据。我想绑定数据并显示一个下拉菜单的单个选项,该选项与数据库的选项相对应。我该怎么做

这是我的密码:

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;
        }
    }