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> 

但它似乎仍然不起作用。在旁注中,页面重新加载,我的选择菜单选项被重置为第一个选项。

4

5 回答 5

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

    }
于 2012-09-21T12:00:30.953 回答
3

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

因为你是新手。通过下面的链接。

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

此链接可能对您有所帮助

于 2012-09-21T12:01:29.337 回答
2

使货币下拉自动回发为真。

于 2012-09-21T12:01:59.750 回答
2

默认情况下, 的AutoPostBack属性DropDownList为假。

如果用户更改列表选择时自动回发到服务器,则返回 true;否则为假。默认值为假。

将其指定为 true:

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

如果这仍然不起作用,那么可能是您在 an 中有控件UpdatePanel并且需要指定触发器。

于 2012-09-21T12:06:00.113 回答
0

您只需设置 Autopostback=True。

于 2016-07-26T08:29:28.227 回答