2017-07-01 2 views
0

Ich fordere Daten von der Datenbank über eine DA-Datei. Ich möchte Daten binden und die Auswahl eines Dropdown-Menüs für die Datenbank anzeigen lassen. Wie soll ich es machen?Wie bindet man Datenbankfelder an Drop-Downs?

hier ist mein Code:

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

} 

Antwort

0

Zuerst Sie eine Vervielfältigung in Ihrer bedingte Anweisung haben:

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

Zweitens Sie die Daten in der Dropdown-Liste binden müssen, dann stellen Sie den gewählten Wert . Siehe: 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; 
     } 
    } 
Verwandte Themen