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

时间:2017-07-01 11:18:53

标签: c# asp.net

我通过D A L文件从数据库请求数据。我想要数据绑定并显示一个下拉菜单的单选项,选择对应数据库的选项。我该怎么办?

这是我的代码:

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

}

1 个答案:

答案 0 :(得分:0)

首先,您在条件语句中有重复:

>>> df[['GDP', 'Units']] = df['GDP'].str.split(' ', 1, expand=True)
>>> print(df)

                GDP     Units
Date                         
Mar 31, 2017  19.03  trillion
Dec 31, 2016  18.87  trillion

其次,您需要将数据绑定到下拉列表,然后设置所选值。请参阅:this post

>>> df['GDP'], df['Units'] = df['GDP'].str.split(' ', 1, expand=True)
>>> print(df)

              GDP  Units
Date                    
Mar 31, 2017    0      1
Dec 31, 2016    0      1