【发布时间】:2017-04-18 09:29:41
【问题描述】:
我有一个我想多次使用的方法,它基本上填充了一个下拉列表。
public void PopulateDropdown(string selectedValue, object listname)
{
String connString = ConfigurationManager.ConnectionStrings["MySql"].ToString(); //Conn string
MySqlConnection mySqlConnection = new MySqlConnection(connString); //Objekt
MySqlCommand cmd = new MySqlCommand(); //cmd objekt
cmd.CommandText = "SELECT NAME FROM CustomerDb WHERE CITY = \"" + selectedValue + "\"";
cmd.CommandType = CommandType.Text;
cmd.Connection = mySqlConnection;
DropDownList dropDownList = listname as DropDownList;
mySqlConnection.Open();
dropDownList.DataSource = cmd.ExecuteReader();
dropDownList.DataTextField = "NAME";
dropDownList.DataBind();
mySqlConnection.Close();
}
我的电话是这样的:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
string value = DropDownList3.SelectedValue;
PopulateDropdown(value, DropDownList4);
}
我知道我的调用和方法是正确的,但由于某种原因,我无法在DropDownList3_SelectedIndexChanged 中调用它。
当我在DropDownList3 中选择一个值时,它只会重新加载并选择默认值“选择城市”。
<asp:DropDownList ID="DropDownList3" CssClass="btn btn-default btn-md pull-right" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>Select city</asp:ListItem>
<asp:ListItem>City1</asp:ListItem>
<asp:ListItem>City2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" CssClass="btn btn-default btn-md pull-right" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged" AutoPostBack="true" Style="">
</asp:DropDownList>
我的DropDownList3_SelectedIndexChanged 看起来像这样:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
string value = DropDownList3.SelectedValue;
PopulateDropdown(value, DropDownList4);
}
回发未到达方法中的断点。
【问题讨论】:
-
DropDownList4_SelectedIndexChanged 中有什么?您是否在 DropDownList3_SelectedIndexChanged 中放置了断点以查看是否调用了该方法?另外,“Public void PopulateDropdown(...)”……这真的是 Public 中的大写 P吗?
-
我要编辑我的答案 2 秒
-
嗯,没有。该方法未被调用