首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

循环绑定的有关问题

2011-12-10 
循环绑定的问题数据库:[表名:temp]SECTIONS_IDLINKSECTIONS_NAME00首页10,1通知20,1,2内部通知现在要往dro

循环绑定的问题
数据库:[表名:temp]
SECTIONS_ID           LINK           SECTIONS_NAME
        0                         0                       首页
        1                       0,1                     通知
        2                       0,1,2                 内部通知
现在要往dropdownlist里绑定数据格式如下:
》首页
》首页》通知
》首页》通知》内部通知

以这种层次形式呈现
从数据库读的是
select   LINK   from   temp   order   by   LINK
这样查出来是
》0
》0》1
》0》1》2
然后我写了一个方法来转换SECTIONS_ID为SECTIONS_NAME

方法是:
public   void   additmes()  
        {
                string   items_id   =   " ";
                string   name   =   " ";
                string   addstr   =   " ";
                //加入第一項
                //獲取所有link字段
                string   allitems   =   "select   LINK   from   NEWS_SECTIONS   order   by   LINK ";
                //獲取數據集
                ds1   =   db.getDataSet(allitems,   "NEWS_SECTIONS ");
                //字段斷行數
                int   num   =   ds1.Tables[ "NEWS_SECTIONS "].Rows.Count;
                //從1取值
                for   (int   i   =   0;   i   <   num;   i++)  
                {
                        //循環賦值
                        items_id   =   ds1.Tables[ "NEWS_SECTIONS "].Rows[i][0].ToString();
                        string[]   arr   =   items_id.Split( ', ');

                        for   (int   j   =   0;   j   <   arr.Length;   j++)  
                        {
                                string   getname   =   "select   SECTIONS_NAME   from   NEWS_SECTIONS   where   SECTIONS_ID= "+arr[j];
                                ds2   =   db.getDataSet(getname,   "NEWS_SECTIONS ");
                                name   =   ds2.Tables[ "NEWS_SECTIONS "].Rows[0][0].ToString();//獲取名字


                                addstr   +=   "> > "   +   name;
                                Response.Write(addstr+ " <br   /> ");
                        }
                                                       
                        parent.Items.Add(addstr);
                       
                }

但是addstr的值有问题   具体如下:
> > 首頁
> > 首頁> > 首頁
> > 首頁> > 首頁> > 通知
> > 首頁> > 首頁> > 通知> > 首頁
> > 首頁> > 首頁> > 通知> > 首頁> > 通知
> > 首頁> > 首頁> > 通知> > 首頁> > 通知> > 內部通知
我知道肯定是循环出问题了,但是又不知道该怎么改,   麻烦大家了!

[解决办法]
or:

for (int j = 0; j < arr.Length; j++)
{
string getname = "select SECTIONS_NAME from NEWS_SECTIONS where SECTIONS_ID= "+arr[j];
ds2 = db.getDataSet(getname, "NEWS_SECTIONS ");
name = ds2.Tables[ "NEWS_SECTIONS "].Rows[0][0].ToString();//獲取名字
addstr += "> > " + name;
Response.Write(addstr+ " <br /> ");
addstr = " ";
}

热点排行