本文介绍了使用包含部门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

推荐答案

<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,就应该从数据库中提取该部门的员工......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:05