选择选项 更改下拉列表 C# ASP.NET 触发事件

9

我在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> 

但是似乎还是不起作用。顺便说一下,页面会重新加载,我的选择菜单选项也会重置为第一个选项。


将AutoPostBack属性设置为true。 - A Developer
5个回答

11

改变

<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>

更新

根据您的更新,回答如下。

更改如下:

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

    }

转换为:

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

    }

3

2

默认情况下,DropDownListAutoPostBack属性为false。

如果用户更改列表的选择,则自动向服务器发生回发,则为true;否则为false。默认值为false。

将其设置为true:

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

如果仍然不能正常工作,那可能是您的控件位于 UpdatePanel 内部,需要指定触发器。

2
使货币下拉框自动提交为真。

0

你只需要将Autopostback设置为True。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接