9

オプションをクリックすると、選択した値を取得してデータベースに渡し、後でクエリ結果を使用して2番目のドロップダウンリストを作成する必要があります。

最初のドロップダウン メニューをクリックすると、このイベントを「起動」できないようです。以下は私が持っているものです:

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

最初のドロップダウンリストに問題なく入力できます。2 つ目は、機能していないように見えるものです。ASP.NET では非常に新しい。私は、jquery ajax を使用してこれを簡単に実現できる PHP のバックグラウンドを持っていますが、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

通貨のドロップダウンの自動ポストバックをTrue にします。

于 2012-09-21T12:01:59.750 に答える
2

デフォルトでは、のAutoPostBackプロパティDropDownListは false です。

ユーザーがリストの選択を変更するたびに、サーバーへのポストバックが自動的に発生する場合は true 。それ以外の場合は false。デフォルトは false です。

true として指定します。

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

それでもうまくいかない場合は、 内にコントロールがUpdatePanelあり、トリガーを指定する必要がある可能性があります。

于 2012-09-21T12:06:00.113 に答える
0

Autopostback=True を設定するだけです。

于 2016-07-26T08:29:28.227 に答える