オプションをクリックすると、選択した値を取得してデータベースに渡し、後でクエリ結果を使用して2番目のドロップダウンリストを作成する必要があります。
最初のドロップダウン メニューをクリックすると、このイベントを「起動」できないようです。以下は私が持っているものです:
ASPXコード
<td class="style3">
<asp:DropDownList ID="Currencies" runat="server"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
</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>
しかし、まだうまくいかないようです。ちなみに、ページがリロードされ、選択メニュー オプションが最初のオプションにリセットされます。