設置DropDownList的當前選項
更新時間:2008年01月16日 20:24:07 作者:
設置DropDownList的當前選項
問:
請問一下在asp.net中的DropDownList綁定到一個數(shù)據(jù)表后,怎么設置他其中的一個項目為已選項???不要叫我用SelectedIndex來設置第幾的個,我只能知道要設置已選的那個項目的值,并不知道他排在第幾位
______________________________________________________________________________________________
答1:
myDrop.Items.Add("請選擇");
myDrop.SelectedIndex=myDrop.Items.Count-1;
______________________________________________________________________________________________
答2:
DDLUnitQuery.Items.FindByText("所有").Selected=true;
______________________________________________________________________________________________
答3:
由于你的DropDownList是綁定到數(shù)據(jù)表的,所以DropDownList和數(shù)據(jù)表中的順序是一樣的。你可以寫個函數(shù),判斷當前DropDownList的選定值在數(shù)據(jù)表中是第幾個:
//
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:
我有一辦法,從數(shù)據(jù)庫檢取,這個是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();
}
//數(shù)據(jù)綁定
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) //如果表不空,綁定數(shù)據(jù)
{
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綁定到一個數(shù)據(jù)表后,怎么設置他其中的一個項目為已選項???不要叫我用SelectedIndex來設置第幾的個,我只能知道要設置已選的那個項目的值,并不知道他排在第幾位
______________________________________________________________________________________________
答1:
myDrop.Items.Add("請選擇");
myDrop.SelectedIndex=myDrop.Items.Count-1;
______________________________________________________________________________________________
答2:
DDLUnitQuery.Items.FindByText("所有").Selected=true;
______________________________________________________________________________________________
答3:
由于你的DropDownList是綁定到數(shù)據(jù)表的,所以DropDownList和數(shù)據(jù)表中的順序是一樣的。你可以寫個函數(shù),判斷當前DropDownList的選定值在數(shù)據(jù)表中是第幾個:
//
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:
我有一辦法,從數(shù)據(jù)庫檢取,這個是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();
}
//數(shù)據(jù)綁定
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) //如果表不空,綁定數(shù)據(jù)
{
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基于ajax和jquery-ui實現(xiàn)進度條
這篇文章主要介紹了Asp.net基于ajax和jquery-ui實現(xiàn)進度條,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-12-12
ASP.NET Core部署前期準備 使用Hyper-V安裝Ubuntu Server 16.10
這篇文章主要為大家詳細介紹了ASP.NET Core部署的前期準備,使用Hyper-V安裝Ubuntu Server 16.10,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
asp.net下通過泛解析和偽靜態(tài)實現(xiàn)二級域名的實現(xiàn)方法
當我們想做一個站群或想為每一個會員的主頁設置為一個二級域名時,總是想拼命的去找些組件來實現(xiàn)。2010-10-10
ASP.NET?MVC使用JSAjaxFileUploader插件實現(xiàn)單文件上傳
這篇文章介紹了ASP.NET?MVC使用JSAjaxFileUploader插件實現(xiàn)單文件上傳的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
HttpRequest的QueryString屬性 的一點認識
我們開發(fā)asp.net程序獲取QueryString時,經(jīng)常性的遇到一些url編碼問題2012-11-11

