如何使用下拉列表从数据库获取显示的值? [英] How to get displayed values from database using dropdownlist?
问题描述
我创建了一个包含2个dropdownmenu(dpmtList amd empList)的表单
通过数据库访问项目.一个下拉菜单包含部门列表(来自数据库),另一个下拉菜单是选定部门的雇员列表,
我曾经使用过嵌套的if else语句,而且总是得到else语句中的员工列表...
编码如下
创建了两个功能
loademploeelist()
loaddepartmentlist()
I have created one form containing 2 dropdownmenu(dpmtList amd empList)
accessing items through database.One dropmenu contains list of department (from database) and other dropmenu is list of employees of specified department which is selected
i have used nested if else statement and always i get list of employees which are in else statement...
coding is below
created two functions
loademploeelist()
loaddepartmentlist()
protected void Page_Load(object sender, EventArgs e)
{
loademploeelist();
loaddepartmentlist();
}
protected void loaddepartmentlist()
{
SqlConnection CON;
SqlCommand COM;
SqlDataReader READER;
string ConnectionString = ConfigurationManager.ConnectionStrings["EMP"].ConnectionString;
CON = new SqlConnection(ConnectionString);
COM = new SqlCommand("Select department from dpmt", CON);
try
{
CON.Open();
READER = COM.ExecuteReader();
dpmtList.DataSource = READER;
dpmtList.DataValueField = "department";
dpmtList.DataBind();
READER.Close();
}
catch
{
CON.Close();
}
}
protected void loademploeelist();
{
SqlConnection CON;
SqlCommand COM;
SqlDataReader READER;
string ConnectionString = ConfigurationManager.ConnectionStrings["EMP"].ConnectionString;
CON = new SqlConnection(ConnectionString);
if (dpmtList.Text == "Administrartor")
{
COM = new SqlCommand("Select Name,ID from AD",CON);
}
else if (dpmtList.Text == "hr")
{
COM = new SqlCommand("Select Name,ID from Her", CON);
}
else if (dpmtList.Text == "IT")
{
COM = new SqlCommand("Select Name,ID from IT", CON);
}
else
{
COM = new SqlCommand("Select Name,ID from HD",CON);
}
CON.Open();
READER = COM.ExecuteReader();
empList.DataSource = READER;
empList.DataValueField = "ID";
empList.DataTextField = "Name";
empList.DataBind();
READER.Close();
CON.Close();
}
}
在这里administerartor,HR IT,HD are department
请告诉我但我的错误并纠正它
问候,
here administerartor,HR IT ,HD are department
Pls do tell me abut my mistake and rectify it
regards,
推荐答案
我建议不要为相同的信息使用单独的表.用一个列创建一个表,该列指示该记录所属的部门.这将使数据库更具扩展性,并使您的代码更容易.可以使用简单的级联模式填充下拉列表.
I would suggest not using separate tables for the same information. Create one table with a column that indicates what department the record belongs to. It will make the database more extensible and your code much easier. The dropdowns could be populated with a simple cascading pattern.
在选定索引中调用此loademploeelist()函数
的已更改事件 您的部门下拉菜单
取选择的值/文本作为您的要求
并根据该值/文本绑定您的员工下拉菜单
还可以使Department的下拉列表autopostback属性= true;/xml>
Call this loademploeelist() function in selected index Changed event of
your Department dropdown
take that selected value/text whichever is your Requirement
and on the basis of that value/text bind your employee dropdown
also Make Department dropdown autopostback property = true;/xml>
处理选定的下拉列表更改事件并调用您的方法
并在pageload上检查pagepostback
handle selectedidexchange events for dropdown and call your methods
and check pagepostback on pageload
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loademploeelist();
loaddepartmentlist();
}
}
这篇关于如何使用下拉列表从数据库获取显示的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!