设置DropDownList的当前选项
程序员文章站
2024-01-31 09:39:04
问: 请问一下在asp.net中的dropdownlist绑定到一个数据表后,怎么设置他其中的一个项目为已选项啊?不要叫我用selectedindex来设置第几的个,我只能...
问:
请问一下在asp.net中的dropdownlist绑定到一个数据表后,怎么设置他其中的一个项目为已选项啊?不要叫我用selectedindex来设置第几的个,我只能知道要设置已选的那个项目的值,并不知道他排在第几位
______________________________________________________________________________________________
答1:
mydrop.items.add("请选择");
mydrop.selectedindex=mydrop.items.count-1;
______________________________________________________________________________________________
答2:
ddlunitquery.items.findbytext("所有").selected=true;
______________________________________________________________________________________________
答3:
由于你的dropdownlist是绑定到数据表的,所以dropdownlist和数据表中的顺序是一样的。你可以写个函数,判断当前dropdownlist的选定值在数据表中是第几个:
//
public int getselectedindex(string str)
{
int idx=0;
dseditdata1=(dseditdata)session["dseditdata1"];
for(int i=0;i<dseditdata1.edit_datalist.rows.count;i++)
{
dseditdata.edit_datalistrow editrow=(dseditdata.edit_datalistrow)dseditdata1.edit_datalist.rows[i];
string datastr=editrow.editvalue;
if(datastr==str)
{
idx=i;
break;
}
}
return idx;
}
然后在html代码中绑定selectedindex值:
//
asp:dropdownlist id=dropdownlist1 runat="server" datamember="edit_datalist" datasource="<%# dseditdata1 %>" width="93px" datatextfield="editdata" datavaluefield="editvalue" selectedindex='<%# getselectedindex(databinder.eval(container, "dataitem.personationid").tostring()) %>'>
</asp:dropdownlist>
______________________________________________________________________________________________
答4:
dropdownlist.items.findbytext("你的值").selected=true;
dropdownlist.items.findbyvalue("你的值").selected=true;
______________________________________________________________________________________________
答5:
dropdownlist1.selectedindex=-1;
dropdownlist1.items.findbytext("选定项目的值").selected=true;
or
dropdownlist1.selectedindex=-1;
dropdownlist1.items.findbyvalue("选定项目的值").selected=true;
______________________________________________________________________________________________
答6:
我有一办法,从数据库检取,这个是radiobuttonlist,需要使用哈希表,你可以参考一下
using system.web.sessionstate;
public class modrole : system.web.ui.page
{
public hashtable stateindex;
private void page_load(object sender, system.eventargs e)
{
stateindex = new hashtable();
myconnection = new oledbconnection(system.configuration.configurationsettings.appsettings["connectionstring"]);
if (!ispostback)
bindgrid();
}
//数据绑定
public void bindgrid()
{
oledbdatareader myreader;
string sql = "select * from tb_role order by roleid";
oledbdataadapter mycommand = new oledbdataadapter(sql, myconnection);
dataset ds = new dataset();
mycommand.fill(ds, "tb_role");
dataview dv = ds.tables["tb_role"].defaultview;
if (ds.tables["tb_role"].rows.count !=0) //如果表不空,绑定数据
{
rbtl_role.datasource=ds.tables["tb_role"].defaultview;
rbtl_role.datatextfield = "rolename";
rbtl_role.datavaluefield = "roleid";
rbtl_role.databind();
}
//对radiobuttonlist进行哈稀编号,保持同radiobuttonlist.selectedindex的值一致编号
int i = 0;
foreach(datarowview drv in dv )
{
stateindex[drv.row["roleid"]]=i;
i++;
}
//进行比较,对选中的进行设置
sql = "select roleid from tb_userrole where user_id=1";
oledbcommand mycmd = new oledbcommand(sql, myconnection);
myconnection.open();
myreader = mycmd.executereader();
while (myreader.read())
{
//此句选中设置
rbtl_role.selectedindex = convert.toint32(stateindex[myreader["roleid"]].tostring());
}
// always call close when done reading.
myreader.close();
// close the connection when done with it.
myconnection.close();
}
______________________________________________________________________________________________
答7:
imfine,感谢你,你的方法最直观:)
请问一下在asp.net中的dropdownlist绑定到一个数据表后,怎么设置他其中的一个项目为已选项啊?不要叫我用selectedindex来设置第几的个,我只能知道要设置已选的那个项目的值,并不知道他排在第几位
______________________________________________________________________________________________
答1:
mydrop.items.add("请选择");
mydrop.selectedindex=mydrop.items.count-1;
______________________________________________________________________________________________
答2:
ddlunitquery.items.findbytext("所有").selected=true;
______________________________________________________________________________________________
答3:
由于你的dropdownlist是绑定到数据表的,所以dropdownlist和数据表中的顺序是一样的。你可以写个函数,判断当前dropdownlist的选定值在数据表中是第几个:
//
public int getselectedindex(string str)
{
int idx=0;
dseditdata1=(dseditdata)session["dseditdata1"];
for(int i=0;i<dseditdata1.edit_datalist.rows.count;i++)
{
dseditdata.edit_datalistrow editrow=(dseditdata.edit_datalistrow)dseditdata1.edit_datalist.rows[i];
string datastr=editrow.editvalue;
if(datastr==str)
{
idx=i;
break;
}
}
return idx;
}
然后在html代码中绑定selectedindex值:
//
asp:dropdownlist id=dropdownlist1 runat="server" datamember="edit_datalist" datasource="<%# dseditdata1 %>" width="93px" datatextfield="editdata" datavaluefield="editvalue" selectedindex='<%# getselectedindex(databinder.eval(container, "dataitem.personationid").tostring()) %>'>
</asp:dropdownlist>
______________________________________________________________________________________________
答4:
dropdownlist.items.findbytext("你的值").selected=true;
dropdownlist.items.findbyvalue("你的值").selected=true;
______________________________________________________________________________________________
答5:
dropdownlist1.selectedindex=-1;
dropdownlist1.items.findbytext("选定项目的值").selected=true;
or
dropdownlist1.selectedindex=-1;
dropdownlist1.items.findbyvalue("选定项目的值").selected=true;
______________________________________________________________________________________________
答6:
我有一办法,从数据库检取,这个是radiobuttonlist,需要使用哈希表,你可以参考一下
using system.web.sessionstate;
public class modrole : system.web.ui.page
{
public hashtable stateindex;
private void page_load(object sender, system.eventargs e)
{
stateindex = new hashtable();
myconnection = new oledbconnection(system.configuration.configurationsettings.appsettings["connectionstring"]);
if (!ispostback)
bindgrid();
}
//数据绑定
public void bindgrid()
{
oledbdatareader myreader;
string sql = "select * from tb_role order by roleid";
oledbdataadapter mycommand = new oledbdataadapter(sql, myconnection);
dataset ds = new dataset();
mycommand.fill(ds, "tb_role");
dataview dv = ds.tables["tb_role"].defaultview;
if (ds.tables["tb_role"].rows.count !=0) //如果表不空,绑定数据
{
rbtl_role.datasource=ds.tables["tb_role"].defaultview;
rbtl_role.datatextfield = "rolename";
rbtl_role.datavaluefield = "roleid";
rbtl_role.databind();
}
//对radiobuttonlist进行哈稀编号,保持同radiobuttonlist.selectedindex的值一致编号
int i = 0;
foreach(datarowview drv in dv )
{
stateindex[drv.row["roleid"]]=i;
i++;
}
//进行比较,对选中的进行设置
sql = "select roleid from tb_userrole where user_id=1";
oledbcommand mycmd = new oledbcommand(sql, myconnection);
myconnection.open();
myreader = mycmd.executereader();
while (myreader.read())
{
//此句选中设置
rbtl_role.selectedindex = convert.toint32(stateindex[myreader["roleid"]].tostring());
}
// always call close when done reading.
myreader.close();
// close the connection when done with it.
myconnection.close();
}
______________________________________________________________________________________________
答7:
imfine,感谢你,你的方法最直观:)
上一篇: Oracle 11gR2 RAC连接时ORA-12545错误
下一篇: php zend_db施用
推荐阅读
-
设置DropDownList的当前选项
-
常用网页浏览器的浏览器选项设置打开方法图文教程
-
PHP setTime 设置当前时间的代码_php实例
-
ini_set函数能设置的全部选项
-
使用Docker run的选项以覆盖Dockerfile中的设置详解
-
UltraEdit怎么保存书签? UltraEdit保存当前书签设置的技巧
-
JQuery设置获取下拉菜单某个选项的值(比较全)
-
PHP函数ini_get_all获取设置选项变量的方法介绍
-
如何让默认运行tcsh的Linux 终端标题自动设置为当前路径
-
oracle查询当前运行的进程、查询被锁的对象、杀Seesion的SQL、设置SQL语句计时等教程