当我尝试从下拉框“不能在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(); } }
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(); }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。