当前位置: 移动技术网 > IT编程>开发语言>.net > 设置DropDownList的当前选项

设置DropDownList的当前选项

2018年04月22日  | 移动技术网IT编程  | 我要评论

罗通拜师,校刊卷首语,blowmeakiss

问:
请问一下在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,感谢你,你的方法最直观:)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网