0

i'm requesting data from database through a D A L file. I want to data bind and show a drop down menu's single option selected against that of the database. How should i do it?

here's my 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();

}
Aishwarya
  • 433
  • 3
  • 10

1 Answers1

0

First, you have a duplication in your conditional statement:

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

Second, you need to bind the data to the dropdown, then set the selected value. Refer: 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;
        }
    }
Jason_P
  • 58
  • 1
  • 1
  • 9