我有三个下拉列表下面的代码
<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);
}
//////////////////
}
}
}
DropForumTitle() for dropdown1(ForumTitleList) list one 工作正常,然后对于 dropdown2(ForumSubTitleList) 我想根据 dropdown1(ForumTitleList) 的选定值进行搜索,当我为 dropdown1(ForumTitleList) 编写代码时执行此操作然后它没有显示任何东西,但是当将代码从 if (!Page.IsPostBack) 更改为 if (Page.IsPostBack) 时,它会显示,但选定的索引会自动变为 0...它显示正确,但是当从 dropdown2 中选择任何选项时( ForumSubTitleList) 然后它默认转到选定的索引 0,对于这个错误 dropdown3(ForumSubjectTitleList) 可以接收选定的项目值并且不显示数据库中的主题列表...如果下拉列表显示任何内容,则每个下拉列表都与一个 ID 相连,然后第二个下拉列表 = 下拉列表 1 的选定值,与下拉列表 3 = 下拉列表 2 的选定值相同
但是我在使用 dropdown2 和 dropdown3 时都遇到了错误
简而言之:
1-dropdown2 不会停留在我选择的值:假设在 LIst A、b、C 和 D 中。当我单击 A 时,它 posrback 并且选择的值再次是 A;
2- dropdown3 无法访问 dropdown2 的选定值,因此它没有显示任何内容...