我在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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以毫无问题地填充第一个下拉列表.第二个似乎不起作用.ASP.NET中的新功能.我来自PHP背景,可以使用jquery ajax轻松实现,但我想学习C#.
任何帮助赞赏.
编辑
所有的答案都表明我将货币下拉列表设为AutoPostBack = true
我已经完成了:
<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
但它似乎仍无法奏效.另外,页面重新加载,我的选择菜单选项重置为第一个选项.
Chr*_*xon 11
更改
<asp:DropDownList ID="Currencies" runat="server"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
至
<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
UPDATE
更新后更新您的问题.
改变这个:
protected void Page_Load(object sender, EventArgs e)
{
populatecurrencylist();
}
Run Code Online (Sandbox Code Playgroud)
对此:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
populatecurrencylist();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54497 次 |
| 最近记录: |