选择选项更改下拉列表 C# ASP.NET 火灾事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12530161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Option Change DropdownList C# ASP.NET Fire Event
提问by MaxI
I have a drop down list in asp.net When I click on an option, I should get the value selected then pass it to a database and later use the query results to populate a second drop down list.
我在 asp.net 中有一个下拉列表 当我单击一个选项时,我应该获取选定的值,然后将其传递给数据库,然后使用查询结果填充第二个下拉列表。
I seem not to be able to "fire" this event when I click on the first drop down menu. Below is what I have:
当我点击第一个下拉菜单时,我似乎无法“触发”这个事件。以下是我所拥有的:
ASPX Code
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 code C#
代码隐藏代码 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);
}
}
I can populate the first dropdownlist without a problem. The second one is what seems not to work. Very new in ASP.NET. I come from a PHP background where this would be achieved easily using jquery ajax, but I want to learn C#.
我可以毫无问题地填充第一个下拉列表。第二个似乎不起作用。在 ASP.NET 中非常新。我来自 PHP 背景,使用 jquery ajax 可以轻松实现这一点,但我想学习 C#。
Any help appreciated.
任何帮助表示赞赏。
EDIT
编辑
All the answers suggest that I make the currencies dropdown list to AutoPostBack = trueI have done that:
所有的答案都表明AutoPostBack = true我已经完成了货币下拉列表:
<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
But it still seems not to work. On a side note, the page reloads and my select menu option gets reset to the first option.
但它似乎仍然不起作用。附带说明一下,页面会重新加载,我的选择菜单选项会重置为第一个选项。
采纳答案by Chris Dixon
Change
改变
<asp:DropDownList ID="Currencies" runat="server"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
To
到
<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True"
onselectedindexchanged="Currencies_SelectedIndexChanged">
</asp:DropDownList>
UPDATE
更新
Update to your question, after your update.
更新后更新您的问题。
Change this:
改变这个:
protected void Page_Load(object sender, EventArgs e)
{
populatecurrencylist();
}
To this:
对此:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
populatecurrencylist();
}
}
回答by sharad
Make sure your dropdown list Currenciesis set to AutoPostBack="True".
确保您的下拉列表Currencies设置为AutoPostBack="True"。
As you are new to this things.Go through below links.
由于您对这件事不熟悉。请浏览以下链接。
http://asp-net-example.blogspot.in/2009/03/how-to-use-dropdownlist-autopostback.html
http://asp-net-example.blogspot.in/2009/03/how-to-use-dropdownlist-autopostback.html
This link may help you
这个链接可能对你有帮助
回答by RL89
Make CurrenciesDropDown Autopostback True.
使货币下拉自动回发为真。
回答by Grant Thomas
By default the AutoPostBackproperty of DropDownListis false.
默认情况下,的AutoPostBack属性DropDownList为 false。
true if a postback to the server automatically occurs whenever the user changes the selection of the list; otherwise, false. The default is false.
如果每当用户更改列表选择时自动发生到服务器的回发,则为 true;否则为假。默认值为假。
Specify it as true:
将其指定为 true:
<asp:DropDownList ... AutoPostBack="True" ...>
...
</asp:DropDownList>
If this still doesn't work then it could be that you have controls within an UpdatePaneland need to specify a trigger.
如果这仍然不起作用,那么可能是您在 a 中具有控件UpdatePanel并且需要指定触发器。
回答by Shubham
You need to just set Autopostback=True.
您只需要设置 Autopostback=True。

