选择Option Change DropdownList C#ASP.NET Fire Event

时间:2012-09-21 11:58:47

标签: c# asp.net

我在asp.net中有一个下拉列表当我点击一个选项时,我应该选择值然后将其传递给数据库,然后使用查询结果填充第二个下拉列表。

当我点击第一个下拉菜单时,我似乎无法“触发”此事件。以下是我的内容:

ASPX代码

<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代码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);
        }            
    } 

我可以毫无问题地填充第一个下拉列表。第二个似乎不起作用。 ASP.NET中的新功能。我来自PHP背景,可以使用jquery ajax轻松实现,但我想学习C#。

任何帮助表示感谢。

编辑

所有答案都表明我将货币下拉列表设为 AutoPostBack = true 我做到了:

<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True" 
                    onselectedindexchanged="Currencies_SelectedIndexChanged">
                </asp:DropDownList> 

但似乎仍无法奏效。另外,页面重新加载,我的选择菜单选项重置为第一个选项。

5 个答案:

答案 0 :(得分:11)

更改

<asp:DropDownList ID="Currencies" runat="server" 
                    onselectedindexchanged="Currencies_SelectedIndexChanged">
                </asp:DropDownList>

<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
                    onselectedindexchanged="Currencies_SelectedIndexChanged">
                </asp:DropDownList>

更新

更新后更新您的问题。

改变这个:

protected void Page_Load(object sender, EventArgs e)
    {
        populatecurrencylist();

    }

对此:

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack) {
        populatecurrencylist();
        }

    }

答案 1 :(得分:3)

确保您的下拉列表Currencies设置为AutoPostBack="True"

由于您不熟悉这些内容。请通过以下链接。

http://asp-net-example.blogspot.in/2009/03/how-to-use-dropdownlist-autopostback.html

此链接可以帮助您

答案 2 :(得分:2)

使货币 DropDown Autopostback True。

答案 3 :(得分:2)

默认情况下,DropDownList的{​​{3}}属性为false。

  

如果每次都自动发生对服务器的回发,则为true   用户更改列表的选择;否则,错误。默认   是假的。

将其指定为true:

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

如果仍然无效,则可能是UpdatePanel内的控件需要指定触发器。

答案 4 :(得分:0)

您只需设置Autopostback = True。