I have two dropdownlists and trying to populate second dropdownlist based on selection of item of another(first) dropdownlist.First dropdownlist contain class number and according to the class number the second dropdownlist should show the subjects from
database.
I have two dropdownlists and trying to populate second dropdownlist based on selection of item of another(first) dropdownlist.First dropdownlist contain class number and according to the class number the second dropdownlist should show the subjects from database.
I have two dropdownlists and trying to populate second dropdownlist based on selection of item of another(first) dropdownlist.First dropdownlist contain class number and according to the class number the second dropdownlist should show the subjects from
database.
I am using c# with ASP.NET and MSSQL server.
Here's an example for your reference:
private string GetConnectionString()
{
//calling up the connection string that was set up from the web config file
return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
}
private void BindDropDownList1()
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
try
{
connection.Open();
string sqlStatement = "SELECT ColumnName * FROM TableName";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList1.DataSource =dt;
DropDownList1.DataTextField = "ColumnName"; // the items to be displayed in the list items
DropDownList1.DataValueField = "ColumnName"; // the id of the items displayed
DropDownList1.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
private void BindDropDownList2(string field)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
try
{
connection.Open();
string sqlStatement = "SELECT * FROM Table WHERE ColumnName = @Value1";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
sqlCmd .Parameters.AddWithValue("@Value1", field)
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList2.DataSource =dt;
DropDownList2.DataTextField = "ColumnName"; // the items to be displayed in the list items
DropDownList2.DataValueField = "ColumnName; // the id of the items displayed
DropDownList2.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDownList1();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindDropDownList2(DropDownList1.SelectedItem.Text);
}
NOTE: Don't forget to add the following Namespaces below for you to make it work
Using System.Data;
Using System.Data.SqlClient;
Also don't forget to set AutoPostBack to TRUE in your first DropDownList to fire up the SelectedIndexChanged event
conNorthwind = new SqlConnection(@"Server=localhost;Integrated Security=SSPI; Database=cms");
conNorthwind.Open();
cmdSelect = new SqlCommand("Select Subject From subject where Semester='"+sems+"'", conNorthwind);
dtrProducts = cmdSelect.ExecuteReader();
here the problem is that the event will not fire at all when i change the item at first dropdownlist.Instead when i pressed the button the event is fired.hoping for the help
here the problem is that the event will not fire at all when i change the item at first dropdownlist
AFAIK, the SelectedIndexChanged event will ONLY fire once the Index of the ListItem is changed.Like for instance if the default Selected items in the list is the ListItem1 and when you select ListItem1 on the DLL again then SelectedIndex changed will NEVER
fire because the index is not changed yet.. It will only fire once you select ListItem 2 or any items on the DDL except for the current selected item.
i have 3 dropdown list box 3rd dropdown populate depending on 2nd drop down and 2nd drop down populate depnding on 1st value of drop down in asp .NET C# plz help me
i have 3 dropdown list box 3rd dropdown populate depending on 2nd drop down and 2nd drop down populate depnding on 1st value of drop down in asp .NET C# plz help me
Hi
Please create a separate topic for your own issue so that we can discuss your issue there. Please note that this thread was already resolved.Replying to old threads like this is considered as
hijacking..
To start a new thread then you can follow these few steps:
Sign in to the site.
Navigate to the appropriate forum in which you want to post a question.
Click on the "Write a New Post" button located at the upper left portion of the page.
Enter a Subject which summarizes your question.
Enter a Message which details your question.
Press the Post button.
You're done
Also, since you are new to this forum then I would suggest you to read on this post:
rosae619
Member
2 Points
16 Posts
populate one dropdownlist based upon action of another dropdownlist
Jun 05, 2009 07:21 AM|LINK
dear all,
I have two dropdownlists and trying to populate second dropdownlist based on selection of item of another(first) dropdownlist.First dropdownlist contain class number and according to the class number the second dropdownlist should show the subjects from database.
I am using c# with ASP.NET and MSSQL server.
waiting for the reply..
.
yjaved
Participant
1113 Points
228 Posts
Re: populate one dropdownlist based upon action of another dropdownlist
Jun 05, 2009 07:44 AM|LINK
set Autopostback = true fro first dropdownlist in your aspx page and on selectedIndexChange event get the records of second dropdownlist
and bind it...
Mark as ans if got your answer.because it will be used for the readers of the forum
venkatu2005
All-Star
32487 Points
6742 Posts
Re: populate one dropdownlist based upon action of another dropdownlist
Jun 05, 2009 07:47 AM|LINK
Check this
http://msdn.microsoft.com/en-us/library/aa581792.aspx
Or use Cascade dropdownlist
Thanks.
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: populate one dropdownlist based upon action of another dropdownlist
Jun 05, 2009 07:55 AM|LINK
Here's an example for your reference:
private string GetConnectionString() { //calling up the connection string that was set up from the web config file return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; } private void BindDropDownList1() { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); string sqlStatement = "SELECT ColumnName * FROM TableName"; SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { DropDownList1.DataSource =dt; DropDownList1.DataTextField = "ColumnName"; // the items to be displayed in the list items DropDownList1.DataValueField = "ColumnName"; // the id of the items displayed DropDownList1.DataBind(); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } private void BindDropDownList2(string field) { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); string sqlStatement = "SELECT * FROM Table WHERE ColumnName = @Value1"; SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection); sqlCmd .Parameters.AddWithValue("@Value1", field) SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { DropDownList2.DataSource =dt; DropDownList2.DataTextField = "ColumnName"; // the items to be displayed in the list items DropDownList2.DataValueField = "ColumnName; // the id of the items displayed DropDownList2.DataBind(); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDropDownList1(); } } protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { BindDropDownList2(DropDownList1.SelectedItem.Text); }NOTE: Don't forget to add the following Namespaces below for you to make it work
Using System.Data;
Using System.Data.SqlClient;
Also don't forget to set AutoPostBack to TRUE in your first DropDownList to fire up the SelectedIndexChanged event
PS: You might be also interested on these demos: Cascading DropDownLists with jQuery and ASP.NET and ASPNET Ajax Cascading DropDownList Control
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
rosae619
Member
2 Points
16 Posts
Re: populate one dropdownlist based upon action of another dropdownlist
Jun 07, 2009 09:50 AM|LINK
hey thanx for the quick reply.i've followed what you mention but still it does not work.here is the code
<script runat="server">
string sems;
void semesterselection(object o, EventArgs e)
{ sems = DropDownList1.SelectedItem.Value.ToString();
SqlConnection conNorthwind;
SqlCommand cmdSelect;
SqlDataReader dtrProducts;
conNorthwind = new SqlConnection(@"Server=localhost;Integrated Security=SSPI; Database=cms");
conNorthwind.Open();
cmdSelect = new SqlCommand("Select Subject From subject where Semester='"+sems+"'", conNorthwind);
dtrProducts = cmdSelect.ExecuteReader();
DropDownList2.DataSource = dtrProducts;
DropDownList2.DataTextField = "Subject";
DropDownList2.DataValueField = "Subject";
DropDownList2.DataBind();
dtrProducts.Close();
conNorthwind.Close();
}protected void Button1_Click(object sender, EventArgs e)
{
}
</script>
<form id="form1" runat="server">
<p>
<asp:label runat="server" text="Unit Test1" font-size="X-Large" font-underline="True"></asp:label>
</p>
<div style="height: 100px; position: absolute; top: 70px; left: 10px; width: 962px;">
<asp:label id="Label3" runat="server" text="Semester:"></asp:label>
<asp:dropdownlist id="DropDownList1" runat="server" autopostback="true" onselectedindexchanged="semesterselection">
<asp:ListItem Selected="True">None</asp:ListItem>
<asp:ListItem>7 </asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:label id="Label4" runat="server" text="Subject"></asp:label>
:
<asp:dropdownlist id="DropDownList2" runat="server">
<asp:ListItem>--</asp:ListItem>
</asp:dropdownlist>
<asp:label id="Label1" runat="server" text="Full mark:"></asp:label>
<asp:label id="Label2" runat="server" text="Pass mark"></asp:label>
<asp:button id="Button1" runat="server" text="Generate" width="113px"
onclick="Button1_Click" />
</div>
</form>
here the problem is that the event will not fire at all when i change the item at first dropdownlist.Instead when i pressed the button the event is fired.hoping for the help
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: populate one dropdownlist based upon action of another dropdownlist
Jun 08, 2009 01:36 AM|LINK
AFAIK, the SelectedIndexChanged event will ONLY fire once the Index of the ListItem is changed.Like for instance if the default Selected items in the list is the ListItem1 and when you select ListItem1 on the DLL again then SelectedIndex changed will NEVER fire because the index is not changed yet.. It will only fire once you select ListItem 2 or any items on the DDL except for the current selected item.
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
amar_matia
Member
11 Points
28 Posts
Re: populate one dropdownlist based upon action of another dropdownlist
Mar 02, 2010 06:02 AM|LINK
i have 3 dropdown list box 3rd dropdown populate depending on 2nd drop down and 2nd drop down populate depnding on 1st value of drop down in asp .NET C# plz help me
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: populate one dropdownlist based upon action of another dropdownlist
Mar 02, 2010 06:27 AM|LINK
Hi
Please create a separate topic for your own issue so that we can discuss your issue there. Please note that this thread was already resolved.Replying to old threads like this is considered as hijacking..
To start a new thread then you can follow these few steps:
Also, since you are new to this forum then I would suggest you to read on this post:
Finding your way in the ASP.NET Forums - Tips and Guidelines
MessageBox Controls for WebForms | Blog | Twitter | Linkedin