通过本文主要向大家介绍了dropdownlist,dropdownlist绑定数据,dropdownlist选中值,dropdownlist默认值,dropdownlist事件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问:
请问一下在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;
&nbs
请问一下在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;
&nbs
您可能想查找下面的文章:
- DropDownList 下拉框选择改变促发事件和防全局刷新(推荐)
- DropDownList 下拉框选择改变促发事件和防全局刷新(推荐)
- 在ASP.NET 2.0中操作数据之三十三:基于DataList和Repeater使用DropDownList过滤的主/从报表
- 解决DropDownList总是选中第一项的方法
- DropDownList添加客户端下拉事件操作
- DropDownList设置客户端事件思路
- DropDownList获取的SelectIndex一直为0的问题
- DropDownList绑定数据表实现两级联动示例
- ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
- .net控件dropdownlist动态绑定数据具体过程分解