老板的要求是:查询BigSort表和 SmallSort表 然后将小类全显示出来:请看,如图: http://www.wanet.cn/01.jpg 要达到这样的效果。问题是:我写的只能显示大类名,怎么使小类名循环到每个大类ID的下边呢???
求高人相助,解决马上结帖!十分火急,感谢大家!!con.Query = "select * from [BigSort] order by ID asc";
ada = new SqlDataAdapter(con.Query,con.Connection);
ds=new DataSet();
ds.Tables.Add("myTable");
ada.Fill(ds,"myTable");SelectSort.DataSource=ds.Tables["myTable"];
SelectSort.DataTextField="Name";
SelectSort.DataValueField="ID";
SelectSort.DataBind();
求高人相助,解决马上结帖!十分火急,感谢大家!!con.Query = "select * from [BigSort] order by ID asc";
ada = new SqlDataAdapter(con.Query,con.Connection);
ds=new DataSet();
ds.Tables.Add("myTable");
ada.Fill(ds,"myTable");SelectSort.DataSource=ds.Tables["myTable"];
SelectSort.DataTextField="Name";
SelectSort.DataValueField="ID";
SelectSort.DataBind();
{
if(i%2 == 0)
{
DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString()));
}
else
{
DropDownList1.Items.Add(new ListItem(" " + i.ToString(),i.ToString()));
}
}
用DataReader循环输出,注意空格是中文全角空格
1)数据库中使用查询,生成树状的分类查询结果(ORACLE有这样的函数,SQL SERVER比较麻烦),再直接绑定。2)利用程序中的集合实现
foreach(BigSort big in bigSorts)
{
DropDownList1.Items.Add(new ListItem(big.name,big.bidId));
foreach(SmallSort small in smallSorts)
{
if(small.bigId == big.bigid)
{
DropDownList1.Items.Add(new ListItem(small.name,small.smallId));
}
}}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
namespace Test2
{
/// <summary>
/// DropDownList 的摘要说明。
/// </summary>
public class DropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
string conn = ConfigurationSettings.AppSettings["strConn"];
string biaoshi,biaoshi1;
int i,j;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
DataBindDropDownList();
}
private void DataBindDropDownList()
{
DropDownList1.Items.Add("↓请选择栏目↓");
DropDownList1.SelectedIndex = 0;
string sql = "select * from SoftDown_Catalog order by rootid,orders";
SqlConnection objConn = new SqlConnection(conn);
SqlCommand objCmd = new SqlCommand(sql,objConn);
try
{
if(objConn.State ==ConnectionState.Closed)
objConn.Open();
SqlDataReader objDr = objCmd.ExecuteReader();
while(objDr.Read())
{
i = Convert.ToInt32(objDr.GetValue(4));
if( i == 0)
biaoshi="";
if( i == 1)
biaoshi=" |-";
if( i > 1)
{
for(j=2;j<=i;j++)
{
biaoshi =" |";
}
char ch = ' ';
biaoshi1= " |-";
biaoshi1=biaoshi1.PadLeft(j,ch);
biaoshi = biaoshi+biaoshi1;
}
DropDownList1.Items.Add(new ListItem(biaoshi + objDr.GetValue(1).ToString(),objDr.GetValue(0).ToString()+","+objDr.GetValue(1).ToString()+","+objDr.GetValue(4).ToString()));
}
}
catch(SqlException ex)
{
Response.Write("<script>alert('"+ex.Message.ToString()+"')</script>");
}
finally
{
if(objConn.State == ConnectionState.Open)
objConn.Close();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.Value.ToString();
if(DropDownList1.SelectedIndex == 0)
Response.Write("<script>alert('你没有选择类型!')</script>");
}
}
}
生成的如:http://www.shareware.cn/Publish.asp中的所属分类一样!!希望是你要的,别忘了给分!呵呵
你的方案不错! 我也试了一下,顺便问一下你代码中的表SoftDown_Catalog 结构是怎样设计的?
我是用 BigSort 和 SmallSort 两个表来保存大分类和小分类数据的,请问你那个表是怎么设计的啊?select * from SoftDown_Catalog order by rootid,orders";
SmallSort表如图: 小分类
http://www.wanet.cn/s.jpg
BigSort表如图: 大分类
http://www.wanet.cn/d.jpg
SmallSort表如图: 小分类
http://www.wanet.cn/s.jpg
BigSort表如图: 大分类
http://www.wanet.cn/d.jpg
SmallSort表如图: 小分类
http://www.wanet.cn/s.jpg
BigSort表如图: 大分类
http://www.wanet.cn/d.jpg请教高人是哪里错了呢:con.Query = "select UserBig.ID,UserBig.Name,UserSmall.ID,UserSmall.Name from UserBig,UserSmall where UserBig.ID=UserSmall.UserBigID order by UserBig.ID desc";con.Command.Connection.Open();
dr = con.Command.ExecuteReader();
while (dr.Read())
{
i = Convert.ToInt32(dr.GetValue(0));
if( i == 0)
biaoshi="";
if( i == 1)
biaoshi=" |-";
if( i > 1)
{
for(j=2;j<=i;j++)
{
biaoshi = " |";
}char ch = ' ';
biaoshi1= " |-";
biaoshi1= biaoshi1.PadLeft(j,ch);
biaoshi = biaoshi+biaoshi1;
}
SelectSort.Items.Add(new ListItem(biaoshi + dr.GetValue(1).ToString(),dr.GetValue(0).ToString()+","+dr.GetValue(3).ToString()+","+dr.GetValue(2).ToString()));
}
dr.Close();
con.Command.Connection.Close();
dr = con.Command.ExecuteReader();
while (dr.Read())
{
i = Convert.ToInt32(dr.GetValue(0));
if( i == 0)
biaoshi="";
if( i == 1)
biaoshi=" |-";
if( i > 1)
{
for(j=2;j<=i;j++)
{
biaoshi = " |";
}char ch = ' ';
biaoshi1= " |-";
biaoshi1= biaoshi1.PadLeft(j,ch);
biaoshi = biaoshi+biaoshi1;
}
SelectSort.Items.Add(new ListItem(biaoshi + dr.GetValue(1).ToString(),dr.GetValue(0).ToString()+","+dr.GetValue(3).ToString()+","+dr.GetValue(2).ToString()));
}
dr.Close();
con.Command.Connection.Close();