使用包含部门ID的下拉列表创建Web表单。一旦用户选择了特定的部门ID,就应该从数据库中提取该部门的员工...... [英] Create a web form with a drop down list containing the department ids. As soon as the user selects a particular Department id the employees of the department should be fetched from the database...
本文介绍了使用包含部门ID的下拉列表创建Web表单。一旦用户选择了特定的部门ID,就应该从数据库中提取该部门的员工......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用包含部门ID的下拉列表创建Web表单。一旦用户选择了特定的部门ID,该部门的员工就应该从数据库中获取并以表格格式列出
Create a web form with a drop down list containing the department ids. As soon as the user selects a particular Department id the employees of the department should be fetched from the database and listed in a tabular format
推荐答案
以下是您的问题的解决方案:
在sql server中创建2个表
1->名为tblDepartment habing字段的部门表a)DeptId int
b)部门名称nvarchar(30)
2->名为tblEmployes的员工表有字段a)id int,
b)EmployeeId nvarchar(30),
c)DeptId int,
D)员工姓名等根据您的要求添加字段名称。
注意:tblDepartment中DeptId的值对于tblEmployes值中的DeptId应该是多少。
ASPX代码:
Hi,
Here is the solution for your question:
Create 2 table in sql server
1-> Table for Department named tblDepartment habing fields a)DeptId int
b)DeptName nvarchar(30)
2->Table for employees named tblEmployes having fields a)id int,
b)EmployeeId nvarchar(30),
c)DeptId int,
D)Employee Name etc add field name as per your requirement.
Note: What ever the value of DeptId in tblDepartment should be same for DeptId in tblEmployes values.
ASPX code:
<pre><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlDepartment" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlDepartment_SelectedIndexChanged">
</asp:DropDownList>
<asp:GridView ID="gvEmployes" runat="server" AutoGenerateColumns="false">
</asp:GridView>
</div>
</form>
</body>
</html>
代码背后:
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDepartmentIds();
}
}
private void BindDepartmentIds()
{
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tblDepartment",con);
DataSet dsDepartment = new DataSet();
da.Fill(dsDepartment);
ddlDepartment.DataTextField = "DeptId";
ddlDepartment.SelectedValue = "DeptId";
ddlDepartment.DataSource = dsDepartment;
ddlDepartment.DataBind();
}
catch { }
finally { con.Close(); }
}
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from tblEmployes where deptid='"+ddlDepartment.SelectedValue.ToString()+"'",con);
DataSet dsEmployees = new DataSet();
da.Fill(dsEmployees);
gvEmployes.DataSource = dsEmployees;
gvEmployes.DataBind();
}
catch { }
finally { con.Close(); }
}
}
这篇关于使用包含部门ID的下拉列表创建Web表单。一旦用户选择了特定的部门ID,就应该从数据库中提取该部门的员工......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文