我有三个下拉列表,下面是代码
<asp:DropDownList ID="ForumTitleList" runat="server"
AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="ForumSubTitleList" runat="server" AutoPostBack="True"
>
</asp:DropDownList>
<asp:DropDownList ID="ForumSubjectTitleList" runat="server" AutoPostBack="True"
>
</asp:DropDownList>
,背后的代码是
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
namespace Auzine.Forums
{
public partial class ForumIT : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ConfigurationFuntion();
DropForumTitle();
DropForumSubTitle();
DropForumSubjectTitle();
}
protected void DropForumTitle()
{
if (!Page.IsPostBack)
{
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString;
string selectSQL = "select DISTINCT ForumTitlesID,ForumTitles from ForumTtitle";
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
try
{
ListItem newItem = new ListItem();
newItem.Text = "Select";
newItem.Value = "0";
ForumTitleList.Items.Add(newItem);
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem1 = new ListItem();
newItem1.Text = reader["ForumTitles"].ToString();
newItem1.Value = reader["ForumTitlesID"].ToString();
ForumTitleList.Items.Add(newItem1);
}
reader.Close();
reader.Dispose();
con.Close();
con.Dispose();
cmd.Dispose();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
//////////////////
}
}
protected void DropForumSubjectTitle()
{
if (Page.IsPostBack)
{
// ForumSubjectTitleList.Items.Clear();
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand com = new SqlCommand("select DISTINCT ForumSubjectTitle from ForumSubject where ForumSubTitlesID='" + ForumSubTitleList.SelectedValue.ToString() + "'", con);
SqlDataReader reader = com.ExecuteReader();
// ForumTitleList.Items.Clear();
while (reader.Read())
{
ForumSubjectTitleList.Items.Add(reader[0].ToString());
}
reader.Close();
con.Close();
}
}
protected void DropForumSubTitle()
{
if (Page.IsPostBack)
{
ForumSubTitleList.Items.Clear();
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString;
string selectSQL = "select DISTINCT ForumTitlesID,ForumSubTitles from ForumSubtitle where ForumTitlesID='" + ForumTitleList.SelectedValue.ToString() + "' ";
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;
try
{
ListItem newItem = new ListItem();
newItem.Text = "Select";
newItem.Value = "0";
ForumSubTitleList.Items.Add(newItem);
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem1 = new ListItem();
newItem1.Text = reader["ForumSubTitles"].ToString();
newItem1.Value = reader["ForumTitlesID"].ToString();
ForumSubTitleList.Items.Add(newItem1);
}
reader.Close();
reader.Dispose();
con.Close();
con.Dispose();
cmd.Dispose();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
//////////////////
}
}
}
但是我收到了dropdown2和dropdown3的错误
简而言之:
1-dropdown2不会保持我选择的值:让我们假设在LI A,b,C和D.当我点击A它后面时,所选的值再次是A;
2- dropdown3无法访问dropdown2的选定值,因此它没有显示任何内容......
答案 0 :(得分:1)
在每个回发中,你都会在每个下拉列表中做两件事:
第一步就是摆脱你选择的价值。清除值并添加新值时,它无法保留所选值。
您需要将这些操作分开。首先,我们假设你有DropDownList1
,其选择应该是DropDownList2
。然后Page_Load
应该只填充DropDownList1
,并且只有当它不是回复时才填充protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
PopulateDropDownList1();
}
。像这样:
DropDownList2
要填充SelectedIndexChanged
,您需要回复DropDownList1
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var value = DropDownList1.SelectedValue;
PopulateDropDownList2(value);
}
事件。像这样:
Page_Load
请注意,SelectedIndexChanged
会在每次加载页面时调用,甚至是回发,并且在事件之前称为,如{{1} }。因此,如果您在Page_Load
中重新填充父列表,则SelectedIndexChanged
中将不再显示所选值。
根据上述情况,事件的顺序为:
Page_Load
执行。DropDownList1
会填充值。DropDownList1
中选择一个值并触发回发。Page_Load
执行。Page_Load
没有做任何事情。DropDownList1_SelectedIndexChanged
执行。DropDownList2
会填充值。此时,用户现在可以查看他们在DropDownList1
中选择的内容以及DropDownList2
中的新值。将其扩展到第三个DropDownList
是相同的模式。您创建的DropDownList2_SelectedIndexChanged
与DropDownList1_SelectedIndexChanged
的功能相同,但使用下一个级联列表。
答案 1 :(得分:0)
填充示例代码:
方法
private void Bind()
{
var dt = YourFunctionReturningDataTable();
//Assuming your table has two columns Id,Name
dropdownlist1.datasource = dt;
dropdownlist1.DataTextField = "Name";
dropdownlist1.DataValueField="Id";
dropdownlist1.DataBind();
dropdownlist1.Items.Insert(0, new ListItem("---Select---","-1"));
}
private void Page_Load()
{
if(!IsPostback)
{
Bind();
}
}
答案 2 :(得分:0)
我认为这是在下拉列表中显示所有类型名称的最佳方式。我使用EDMX和LINQ查询。
ASPX:
===============
<asp:DropDownList ID="GenreList" runat="server" SelectMethod="GenreList_GetData" DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
C#:
============
public IEnumerable<Genre> GenreList_GetData()
{
using(PlanetWroxEntities myEntities= new PlanetWroxEntities())
{
return (from genre in myEntities.Genres
orderby genre.SortOrder
select genre).ToList();
}
}