【发布时间】:2013-10-05 13:09:42
【问题描述】:
我在下面的代码中有三个下拉列表
<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) 编写代码时执行此操作) then it is not showing any thing but when change the code from if (!Page.IsPostBack) to if (Page.IsPostBack) then it shows but the selected index goes aoutomatically to 0... It display right but when select any option从 dropdown2(ForumSubTitleList) 然后它默认进入选定的索引 0 并且对于这个错误 dropdown3(ForumSubjectTitleList) 可以接收选定的项目值并且不显示来自数据库的主题列表...如果下拉列表显示,每个下拉列表都与一个 ID 连接然后第二个下拉列表 = 下拉列表 1 的选定值,与下拉列表 3 = 下拉列表 2 的选定值相同
但是我在使用 dropdown2 和 dropdown3 时都遇到了错误
简而言之:
1-dropdown2 不会停留在我选择的值上:假设在 LIst A、b、C 和 D 中。当我点击 A 时,它会返回并且选择的值再次是 A;
2- dropdown3 无法访问 dropdown2 的选定值,因此它没有显示任何内容...
【问题讨论】: