Drop Down Lists Query

A

accyboy1981

Hi,

I new to C# so please forgive me if this is simple. I've got 2 drop
down lists the first is hard coded with data where as the second is
populated from a database. The options that appear in the second should
be determined by what is selected in the first drop down list. When the
page loads it automatically populates the second list with the default
value via a proceedure:

public void popDDL(string dbName)
{

string strConn;
string strSQL;
strConn = "SERVER=localhost,1433; DATABASE="+dbName+"; user
id=HMI_User; password=HMI_User; Language=British";
strSQL = @"SELECT name FROM table1 ORDER BY name";
conn = new SqlConnection( strConn );

try
{
conn.Open();
cmdSelect = new SqlCommand(strSQL, conn);
SqlDataReader dtr = cmdSelect.ExecuteReader();
if (dtr.HasRows)
{
ddlMachine.DataSource = dtr;
ddlMachine.DataTextField = "name";
ddlMachine.DataValueField = "name";
ddlMachine.DataBind();
}
}
catch (Exception ex)
{
lblError.Text = "Error! Could not generate search information!
"+ex;
}
finally
{
conn.Close();
}
}

In the first drop down list the values are the database names. What I'm
am trying to do is something like:
onClick(popDDL("databasename"))
so the page is refreshed with the new data however I'm not sure how to
do it in C#. Any help would be appreciated.

Thanks

Simon
 
G

Guest

This is simple in .NET but you must remember that there will be a post back
to the server. In the DropDownList control add this:

AutoPostBack="True"
OnSelectedIndexChanged="FirstDropDownSelectionChange"

Where FirstDropDownSelectionChange is the method name that refreshes the 2nd
drop down.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top