如何从asp.net中的数据库将数据导入DropdownList [英] How to getting data into DropdownList from database in asp.net
本文介绍了如何从asp.net中的数据库将数据导入DropdownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码>嗨大家好,
我有静态文本的下拉列表。
code>Hi Guys,
I have dropdownlist with static text.
<asp:DropDownList ID="ddlStatus" runat="server" Width="150px">
<asp:ListItem Text="Select Item" Value="0"></asp:ListItem>
<asp:ListItem Text="Accept" Value="1"></asp:ListItem>
<asp:ListItem Text="Reject" Value="2"></asp:ListItem>
</asp:DropDownList>
我得到了来自数据库的数据并将数据绑定到下拉列表。
I am getting the data from database and bind the data to dropdownlist.
dsEmpRequisition = new DataSet();
dsEmpRequisition = g.Get_CFO_Requisition(hdnRequisitionNumber.Value);
if (dsEmpRequisition.Tables[0].Rows.Count > 0)
{
ddlApprove.SelectedItem.Text= dsEmpRequisition.Tables[0].Rows[0]["APPROVED_HR_STATUS"].ToString();
//这里
}
如何绑定到下拉列表。
请任何人帮助我。
// here
}
how can bind to the dropdownlist.
Please any one help me.
推荐答案
以下是一些例子
从数据库填充下拉列表 - ASP.NET [ ^ ]
使用ASP.NET中的数据库绑定DropDownList [ ^ ]
如何将数据库表绑定到下拉列表值 [ ^ ]
Here are some examples
Populate Dropdownlist from Database - ASP.NET[^]
Binding DropDownList With Database in ASP.NET[^]
how to bind database table to dropdownlist value[^]
hi ,
试试这个,
try this,
//put your datasource here
ddlApprove.DataSource = dsEmpRequisition.Tables[0];
//data from specific column that you want to display
ddlApprove.DataTextField="TitleColumn";
//value of the data that you want to get when the text is selected
ddlApprove.DataValueField="ValueColumn";
//lastly, bind it
ddlApprove.DataBind();
希望你明白!快乐的编码:)
请将此标记为解决方案,如果它可以帮助您!谢谢
您好b $ b
下面我创建了一个单独的类,我从我的数据库中选择数据
Hi
Below I created a separate class where I select data from my database
//Metod to populate combobox for cognos courses
public ArrayList CognosComboBox()
{
dbConnection();
string sqlCBO = "SELECT DISTINCT courseCode FROM Courses;";
dbCmd = new OleDbCommand(sqlCBO, dbConn);
OleDbDataReader reader = dbCmd.ExecuteReader();
ArrayList myList = new ArrayList();
myList.Add("Choose course");
while (reader.Read())
{
myList.Add(reader[0].ToString());
}
reader.Close();
dbConn.Close();
return myList;
}
下面是后面的代码
Below is the back code
N.B cboCourses //is a DropDownList name
public void populateCombo()
{
DataCls dtClass = new DataCls();
cboCourses.DataSource = dtClass.CognosComboBox();
cboCourses.DataBind();
}
那么你甚至可以在Page_Load上调用populateCombo() ()
希望有帮助......
Then you can call populateCombo() any where you want even on Page_Load()
Hope it helps...
这篇关于如何从asp.net中的数据库将数据导入DropdownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文