欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 下拉列表无级数据绑定实现代码

程序员文章站 2024-03-08 11:12:34
复制代码 代码如下: private string toadd = "├".

private void getarticlecateg...
复制代码 代码如下:

private string toadd = "├".<br><br>private void getarticlecategory(string pid)
{
sqlconnection conn = new sqlconnection("server=.;database=test;uid=sa;pwd=;");
string sql = "select articlesgroup_id,groupname from articlesgroup where articlesgroup_parent_id=@pid order by groupname";
sqlcommand cmd = new sqlcommand(sql, conn);
sqlparameter pid = new sqlparameter("@pid", sqldbtype.int);
pid.value = pid;
cmd.parameters.add(pid);
conn.open();
sqldatareader sdr = cmd.executereader();
while (sdr.read())
{
this.dropdownlist1.items.add(new listitem(toadd + " " + sdr[1].tostring(), sdr[0].tostring()));
toadd += "─┴";
this.getarticlecategory(sdr[0].tostring());
toadd = toadd.substring(0, toadd.length - 2);
}
sdr.close();
conn.close();
}

在page_load里调用

程序代码
复制代码 代码如下:

protected void page_load(object sender, eventargs e)
{
this.keyword.attributes.add("onfocus","if( this.value=='key key key') {this.value='' };");
if (!page.ispostback)
{
this.getarticlecategory("0");
}
this.dropdownlist1.items.insert(0, new listitem("search all", "all"));
}