Results 1 to 7 of 7

Thread: Populating dropdown list on formload

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    5

    Populating dropdown list on formload

    Hi,
    I am new to .Net. plz hel me..
    I am trying to populate dropdown list on page load so that later i can select one item to populate datagrid. But the drop downlist is not populating.
    This is the code i used in page_load ()


    if (!IsPostBack)
    {

    // Put user code to initialize the page here.
    Connectionclass cls = new Connectionclass();
    cls.ConnOpen();
    DropDownList ddl = new DropDownList();
    OleDbCommand cmd = new OleDbCommand("select firstname from f_userdata",cls.conn);

    OleDbDataAdapter UserDA = new OleDbDataAdapter();
    UserDA.SelectCommand = cmd;

    DataSet UserDS = new DataSet();
    UserDA.Fill(UserDS,"f_userdata");

    ddl.DataSource = UserDS.Tables[0];
    ddl.DataTextField = UserDS.Tables[0].Columns[0].ColumnName.ToString();
    ddl.DataValueField = UserDS.Tables[0].Columns[0].ColumnName.ToString();
    ddl.DataBind();
    }

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Populating dropdown list on formload

    The dropdown is populated only on the first page view because of this if(!IsPostBack), so when you do select an item and postback the dropdown will be empty.

    You need to populate the dropdown on every postback and viewstate will 'remember' which item was selected for you.

  3. #3
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Populating dropdown list on formload

    Actually I've just noticed that you are creating the dropdownlist in code but not actually adding it to the page, so how are you even seeing it at all on your page never mind populated?

  4. #4
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196

    Re: Populating dropdown list on formload

    Quote Originally Posted by Fishcake
    The dropdown is populated only on the first page view because of this if(!IsPostBack), so when you do select an item and postback the dropdown will be empty.

    You need to populate the dropdown on every postback and viewstate will 'remember' which item was selected for you.
    what version of .net are you using? Dude, Viewstate fires before pageload. Every time you databind in pageload it will wipe out viewstate for that control. You have to bind it before viewstate fires. That would be at initialization stage. Now on the other hand, if you bind it on page load and wrap it in a if(!postback) then viewstate will keep the control populated that is if it was a design time added control.

    Make sure there is data in
    UserDS.Tables[0]
    Last edited by MasterBlaster; Jul 20th, 2006 at 06:56 PM.
    "And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
    Frank Zappa

  5. #5
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196

    Re: Populating dropdown list on formload

    rssagar take that code out of page load. Override the pages "Init" event and add the control and populate it there. That will give you viewstate for you dynamically added drop down list. after the line
    Code:
    override protected void OnInit(EventArgs e)
        {// Put user code to initialize the page here.
    Connectionclass cls = new Connectionclass();
    cls.ConnOpen();
    DropDownList ddl = new DropDownList();
    OleDbCommand cmd = new OleDbCommand("select firstname from f_userdata",cls.conn);
    
    OleDbDataAdapter UserDA = new OleDbDataAdapter();
    UserDA.SelectCommand = cmd;
    
    DataSet UserDS = new DataSet();
    UserDA.Fill(UserDS,"f_userdata");
    
    ddl.DataSource = UserDS.Tables[0];
    ddl.DataTextField = UserDS.Tables[0].Columns[0].ColumnName.ToString();
    ddl.DataValueField = UserDS.Tables[0].Columns[0].ColumnName.ToString();
    ddl.DataBind();
    this.controls.add(ddl);
    }
    "And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
    Frank Zappa

  6. #6
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Populating dropdown list on formload

    Quote Originally Posted by MasterBlaster
    what version of .net are you using? Dude, Viewstate fires before pageload. Every time you databind in pageload it will wipe out viewstate for that control. You have to bind it before viewstate fires. That would be at initialization stage. Now on the other hand, if you bind it on page load and wrap it in a if(!postback) then viewstate will keep the control populated that is if it was a design time added control.

    Make sure there is data in
    UserDS.Tables[0]
    Sorry all, I don't remember typing this and was out drinking last night.

    It looks like I was confused. I will try and stear clear of replying to posts outside of chitchat when drunk in the future.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    5

    Re: Populating dropdown list on formload

    Thnks friends for those replies..The problem got solved, it was a mistake in using proper dropdown list name

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width