我在 asp.net 中有一个下拉列表当我单击一个选项时,我应该获取选定的值,然后将其传递给数据库,然后使用查询结果来填充第二个下拉列表。
单击第一个下拉菜单时,我似乎无法“触发”此事件。以下是我所拥有的:
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);
}
}
我可以毫无问题地填充第一个下拉列表。第二个似乎不起作用。在 ASP.NET 中非常新。我来自 PHP 背景,使用 jquery ajax 可以轻松实现,但我想学习 C#。
任何帮助表示赞赏。
编辑
所有答案都表明我将货币下拉列表设置为AutoPostBack = true
我已经这样做了:
<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
但它似乎仍然不起作用。在旁注中,页面重新加载,我的选择菜单选项被重置为第一个选项。