简体   繁体   中英

Select Option Change DropdownList C# ASP.NET Fire Event

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.

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

使货币 DropDown Autopostback True。

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.

您只需设置Autopostback = True即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM