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();
}
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.
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?
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? :confused: 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]
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);
}
Re: Populating dropdown list on formload
Quote:
Originally Posted by MasterBlaster
what version of .net are you using? :confused: 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. :o
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 :(