2017-07-01 12 views
0

私はデータベースからD 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(); 

} 

答えて

0

まず、あなたはあなたの条件文で重複を持っている:

if(!IsPostBack) { 
... 
    if(!IsPostBack) { 
    ... 
    } 
    } 

第二に、あなたがドロップダウンにデータをバインドする必要があり、その後、選択した値を設定します。参照:this post

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; 
     } 
    } 
関連する問題