I have a drop down list in asp.net When I click on an option, I should get the value selected then pass it to a database and later use the query results to populate a second drop down list.
I seem not to be able to "fire" this event when I click on the first drop down menu. Below is what I have:
ASPX Code
<td class="style3">
<asp:DropDownList ID="Currencies" runat="server"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
Payment Mode</td>
<td class="style3">
<asp:DropDownList ID="PaymentModes" runat="server">
</asp:DropDownList>
</td>
CodeBehind code C#
String conn = WebConfigurationManager.ConnectionStrings["pvconn"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
populatecurrencylist();
}
public void populatecurrencylist()
{
SqlCommand sql = new SqlCommand("SELECT * FROM CURRENCIES_TBL ORDER BY Currency_Initials",new SqlConnection(conn));
sql.Connection.Open();
SqlDataReader listcurrencies;
listcurrencies = sql.ExecuteReader();
Currencies.DataSource = listcurrencies;
Currencies.DataTextField = "Currency_Initials";
Currencies.DataValueField = "Currency_Group_ID";
Currencies.DataBind();
sql.Connection.Close();
sql.Connection.Dispose();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void Currencies_SelectedIndexChanged(object sender, EventArgs e)
{
var currid = Currencies.SelectedValue;
HttpContext.Current.Response.Write(currid);
//int currid = 0;
try
{
SqlCommand sql = new SqlCommand("SELECT * FROM PAYMENT_MODES_TBL WHERE Currency_ID = @currencyid", new SqlConnection(conn));
SqlParameter param0 = new SqlParameter();
param0.ParameterName = "@currencyid";
param0.SqlDbType = System.Data.SqlDbType.Int;
param0.Value = currid;
sql.Parameters.Add(param0);
sql.Connection.Open();
SqlDataReader listpaymodes;
listpaymodes = sql.ExecuteReader();
PaymentModes.DataSource = listpaymodes;
PaymentModes.DataTextField = "Payment_Mode";
PaymentModes.DataValueField = "Paying_Account_Code";
PaymentModes.DataBind();
sql.Connection.Close();
sql.Connection.Dispose();
}
catch(Exception s)
{
HttpContext.Current.Response.Write("Error Occured " + s.Message);
}
}
I can populate the first dropdownlist without a problem. The second one is what seems not to work. Very new in ASP.NET. I come from a PHP background where this would be achieved easily using jquery ajax, but I want to learn C#.
Any help appreciated.
EDIT
All the answers suggest that I make the currencies dropdown list to AutoPostBack = true
I have done that:
<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
But it still seems not to work. On a side note, the page reloads and my select menu option gets reset to the first option.
Change
<asp:DropDownList ID="Currencies" runat="server"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
To
<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
UPDATE
Update to your question, after your update.
Change this:
protected void Page_Load(object sender, EventArgs e)
{
populatecurrencylist();
}
To this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
populatecurrencylist();
}
}
Make sure your dropdown list Currencies
is set to AutoPostBack="True"
.
As you are new to this things.Go through below links.
http://asp-net-example.blogspot.in/2009/03/how-to-use-dropdownlist-autopostback.html
This link may help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With