Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DropDownList initialization question
Message
General information
Forum:
ASP.NET
Category:
Web forms
Miscellaneous
Thread ID:
00900902
Message ID:
00901261
Views:
29
This message has been marked as a message which has helped to the initial question of the thread.
>>>My form has several DropDownLists on it. I want to populate them from tables, as I think I mentioned...

Ok...

I created a web form... put two ddl's and a button. I named the first control ddlAuthors and the second ddlTitles. When I run the code, I select items from the combos.. then I press the submit... when the page reloads my selections are intact. Here is my code from the page_load event:
if (! IsPostBack)
{
	System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("trusted_connection=SSPI;data source=(local);database=pubs");
				
	System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM authors", conn);
				
	System.Data.DataTable authors = new DataTable("authors");
	System.Data.DataTable titles = new DataTable("titles");
	da.Fill(authors);
				
	da.SelectCommand = new System.Data.SqlClient.SqlCommand("SELECT * FROM titles", conn);
	da.Fill(titles);
	ddlAuthors.DataSource = authors;
	ddlAuthors.DataTextField = "au_lname";
	ddlAuthors.DataBind();
	ddlAuthors.Items.Insert(0, "");

	ddlTitles.DataSource = titles;
	ddlTitles.DataTextField = "title";
	ddlTitles.DataBind();
	ddlTitles.Items.Insert(0, "");
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform