實現效果:第一個下拉控件是選擇學院,第二個下拉控件是選擇系部,第三個下拉控件是選擇班級。
從網上找了個後台代碼:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudInfo.aspx.cs" Inherits="Modules_StudInfo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
請選擇學院:<asp:DropDownList ID="drdlXY" runat="server">
</asp:DropDownList><br />
請選擇系部:<asp:DropDownList ID="drdlXB" runat="server">
</asp:DropDownList><br />
請選擇班級:<asp:DropDownList ID="drdlCL" runat="server">
</asp:DropDownList><br />
<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>
</div>
</form>
</body>
</html>
後台代碼:
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.Data.SqlClient;
using System.Configuration;
public partial class Modules_StudInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string ConnString = ConfigurationSettings.AppSettings["SqlConnString"];
SqlConnection Conn = new SqlConnection(ConnString);
try
{
Conn.Open();
this.drdlXY.Items.Add("請選擇!");
this.drdlXB.Items.Add("請選擇!");
this.drdlCL.Items.Add("請選擇!");
string strSQL = "select XueYuanName from XueYuan select XiBuName from XiBu select ClassName from Class";
SqlCommand com = new SqlCommand(strSQL, Conn);
SqlDataReader dr = com.ExecuteReader();
if(dr.Read())
{
this.drdlXY.Items.Add(dr["XueYuanName"].ToString());
this.drdlXB.Items.Add(dr["XiBuName"].ToString());
this.drdlCL.Items.Add(dr["ClassName"].ToString());
}
}
//catch (Exception ex)
//{
// Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");
//}
finally
{
Conn.Close();
}
}
}
}
但是這個出不來,如果把while中的後兩個刪掉,sql語句中只輸入select XuYuanName from XuYuan 就不會提示錯誤,但是只有第一個下拉控件能實現功能。
顯示錯誤
我想樓主所謂的 while應該是指if 這段代碼:
if(dr.Read())
{
this.drdlXY.Items.Add(dr["XueYuanName"].ToString());
this.drdlXB.Items.Add(dr["XiBuName"].ToString());
this.drdlCL.Items.Add(dr["ClassName"].ToString());
}
你要具體說明一下你的錯誤信息是什麼
看完你的SQL語句,我知道問題在哪裡了,問題出在你的SQL語句上:
string strSQL = "select XueYuanName from XueYuan select XiBuName from XiBu select ClassName from Class";
你換成這段試試:
string strSQL = "select XueYuanName,XiBuName,ClassName from XueYuan ,XiBu ,Class";