【发布时间】:2013-04-10 17:17:12
【问题描述】:
当我尝试从“不能在 DropDownList 中选择多个项目”下拉框中选择一个项目时出现此错误。有人可以帮我吗我不知道为什么我会得到这个。这是我的代码:
private void Bind_GridView()
{
this.BindGroupNameList(DropDownList1);
}
private void GetGroupNameList(DropDownList DropDownList1)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnection con2 = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd1 = new SqlCommand("select distinct Name" +
" from MyTable");
cmd1.Connection = con2;
con2.Open();
DropDownList1.DataSource = cmd1.ExecuteReader();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Name";
DropDownList1.DataBind();
con2.Close();
DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString())
.Selected = true;
}
//on item change
protected void NameChanged(object sender, EventArgs e)
{
DropDownList DropDownList1 = (DropDownList)sender;
ViewState["MyFilter"] = DropDownList1.SelectedValue;
this.Bind_GridView();
}
这是我在 aspx 中的下拉框
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="NameChanged"
DataTextField="Name" DataValueField="Name"
AppendDataBoundItems="true">
<asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>
<asp:ListItem Text="Top 10" Value="10"></asp:ListItem>
</asp:DropDownList>
这是页面加载的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["MyFilter"] = "ALL";
this.Bind_GridView();
}
}
这里是调用GetGroupNameList的方法:
private void Bind_GridView()
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("sp_filter_Names");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@MyFilter", ViewState["MyFilter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GV_Test.DataSource = dt;
GV_Test.DataBind();
GetGroupNameList();
}
【问题讨论】:
-
page_load 中是否有任何代码,如果是,那么请。也发一下。
-
Ratina,我添加了页面加载代码。谢谢
-
好吧,在 GetGroupNameList(DropDownList DropDownList1) 之前的 DropDownList1.Items.FindByValue(ViewState["MyFilter"].ToString()) .Selected = true;插入这个--> DropDownList1.selectedIndex=-1;
-
已添加但仍然是同样的问题。
-
谁告诉你可以在 DropDownList 中选择多个项目? DropDownList是一个组合框,在里面只能选择一项,多选就得用ListBox之类的控件了。我什至惊讶地看到每个人都在这里提出答案。
标签: c# asp.net dropdownbox