9

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>
                &nbsp;</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.

1
  • Add AutoPostBack property to true. Commented Sep 21, 2012 at 12:02

5 Answers 5

11

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();
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

Make Currencies DropDown Autopostback True.

Comments

2

By default the AutoPostBack property of DropDownList is false.

true if a postback to the server automatically occurs whenever the user changes the selection of the list; otherwise, false. The default is false.

Specify it as true:

<asp:DropDownList ... AutoPostBack="True" ...>
  ...
</asp:DropDownList>

If this still doesn't work then it could be that you have controls within an UpdatePanel and need to specify a trigger.

Comments

0

You need to just set Autopostback=True.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.