|
-
Jul 20th, 2006, 09:34 AM
#1
Thread Starter
New Member
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();
}
-
Jul 20th, 2006, 05:01 PM
#2
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.
-
Jul 20th, 2006, 05:05 PM
#3
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?
-
Jul 20th, 2006, 06:47 PM
#4
Addicted Member
Re: Populating dropdown list on formload
 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
-
Jul 20th, 2006, 06:52 PM
#5
Addicted Member
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
-
Jul 21st, 2006, 03:34 AM
#6
Re: Populating dropdown list on formload
 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.
-
Jul 21st, 2006, 09:35 AM
#7
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|