62,041
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class details : System.Web.UI.Page
{
public static string conStr =
@"Data Source=20100310-0848\SQLEXPRESS;Initial Catalog=EmployeeManager;Integrated Security=True";
public static int id;
protected void Page_Load(object sender, EventArgs e)
{
id = Convert.ToInt32(Request.QueryString["id"]); //获取部门id
if (!IsPostBack)
{
BindGv(0);
BindList();
}
}
public void BindList()
{//将数据绑定到下拉列表
string strSql = "select * from Department";
SqlDataAdapter adpter=new SqlDataAdapter(strSql,conStr);
DataSet ds=new DataSet();
adpter.Fill(ds);
dlDept.DataSource = ds.Tables[0];
dlDept.DataValueField = "deptId";//设置为表的主键
dlDept.DataTextField = "deptName";
dlDept.DataBind();
}
public void BindGv(int deptId)
{//将数据绑定到控件上
if(deptId==0)
{
string strSql = "select * from Person where deptId="+id; //部门id
SqlDataAdapter adpter=new SqlDataAdapter(strSql,conStr);
DataSet ds=new DataSet();
adpter.Fill(ds);
gvDetails.DataSource=ds.Tables[0];
gvDetails.DataBind();
}
else
{
string strSql = "select * from Person where deptId=" + deptId; //下拉列表选取的部门id
SqlDataAdapter adpter = new SqlDataAdapter(strSql, conStr);
DataSet ds = new DataSet();
adpter.Fill(ds);
gvDetails.DataSource = ds.Tables[0];
gvDetails.DataBind();
}
}
protected void gvEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//删除行记录
int personId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values[0]);
string strSql = "delete from Person where PersonId =" + personId;
Query(strSql);
BindGv(Convert.ToInt32(dlDept.SelectedValue));
}
protected void gvEmployee_RowEditing(object sender, GridViewEditEventArgs e)
{//进入行编辑状态
int deptId = Convert.ToInt32(dlDept.SelectedValue);
gvDetails.EditIndex = e.NewEditIndex;
BindGv(deptId);
}
protected void gvEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
{//更新记录
int personId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values[0]);
string Nm = ((TextBox) gvDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string sex = ((TextBox) gvDetails.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
int age = Convert.ToInt32(((TextBox) gvDetails.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
string Occupation = ((TextBox) gvDetails.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
SqlConnection con=new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand("proc_updatePerson", con);
SqlParameter[] parm =
{
new SqlParameter("@Nm",SqlDbType.NVarChar),
new SqlParameter("@age",SqlDbType.Int),
new SqlParameter("@Sex",SqlDbType.NChar,30),
new SqlParameter("@Occupation",SqlDbType.NVarChar,30),
new SqlParameter("@PersonId",SqlDbType.Int)
};
parm[0].Value = Nm;
parm[1].Value = age;
parm[2].Value = sex;
parm[3].Value = Occupation;
parm[4].Value = personId;
foreach(SqlParameter parameter in parm)
{
cmd.Parameters.Add(parameter);
parameter.Direction = ParameterDirection.Input;
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
gvDetails.EditIndex = -1;
BindGv(Convert.ToInt32(dlDept.SelectedValue));
}
protected void btnAdd_Click(object sender, EventArgs e)
{//添加记录
SqlConnection con=new SqlConnection(conStr);
con.Open();
int deptId = Convert.ToInt32(dlDept.SelectedValue); //获得和数据库对应的id 主键
SqlCommand cmd = new SqlCommand("Proc_InsertPerson",con);
SqlParameter[] parm =
{
new SqlParameter("@Nm", SqlDbType.NChar),
new SqlParameter("@Age", SqlDbType.Int),
new SqlParameter("@Sex", SqlDbType.NChar),
new SqlParameter("@Occupation", SqlDbType.NChar,30),
new SqlParameter("@deptId", SqlDbType.Int)
};
parm[0].Value = tbNam.Text;
parm[1].Value = tbAge.Text;
parm[2].Value = tbSex.Text;
parm[3].Value = tbOccp.Text;
parm[4].Value = deptId;
foreach (SqlParameter parameter in parm)
{
cmd.Parameters.Add(parameter);
parameter.Direction = ParameterDirection.Input;
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
BindGv(deptId);
//清空输入框
tbAge.Text = "";
tbNam.Text = "";
tbOccp.Text = "";
tbSex.Text = "";
}
public static void Query(string strSql)
{//通用方法,执行查询语句
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(strSql, con))
{
cmd.ExecuteNonQuery();
}
}
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{//取消行编辑
gvDetails.EditIndex = -1;
}
protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
{
BindGv(Convert.ToInt32(dlDept.SelectedValue));
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 327px;
}
.style2
{
width: 296px;
}
</style>
</head>
<body>
<div style="font-size:large; font-weight:bold; text-align:center; background-color:Gray" >
管理系统
</div>
<div>
</div>
<form id="form1" runat="server">
<div style="text-align:center;background-color:Gray">
<div>
<table style="height: 76px; width: 324px; background-color:Teal; margin-left:50px" >
<tr>
<td class="style1">姓名:<asp:TextBox ID="tbNam" runat="server"></asp:TextBox>
</td>
<td class="style2">年龄:<asp:TextBox ID="tbAge" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">职业:<asp:TextBox ID="tbOccp" runat="server"></asp:TextBox>
</td>
<td class="style2">姓别:<asp:TextBox ID="tbSex" runat="server" style="height: 22px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<div style="text-align:left">选择要添加的部门:<asp:DropDownList ID="dlDept" runat="server"
AutoPostBack="True">
</asp:DropDownList> <asp:Button ID="btnAdd" runat="server" Text="添加" onclick="btnAdd_Click"
Width="46px" /></div>
</td>
</tr>
</table>
</div>
<div style="height:50px;">
</div>
<div style="background-color:Gray; height: 337px; width: 538px;">
<asp:GridView ID="gvDetails" runat="server" Width="716px"
AutoGenerateColumns="False" onrowdeleting="gvEmployee_RowDeleting"
onrowediting="gvEmployee_RowEditing"
onrowupdating="gvEmployee_RowUpdating" DataKeyNames="PersonId"
Height="136px" onrowcancelingedit="gvDetails_RowCancelingEdit"
onselectedindexchanged="gvDetails_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="PersonId" HeaderText="员工Id" ReadOnly="True" />
<asp:BoundField DataField="Nm" HeaderText="员工姓名" />
<asp:BoundField DataField="Sex" HeaderText="姓别" />
<asp:BoundField DataField="Age" HeaderText="年龄" />
<asp:BoundField DataField="Occupation" HeaderText="职位" />
<asp:CommandField ShowEditButton="True" InsertVisible="False"
ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>