用户管理Width="120px"/>AutoGenerateColumns="False"AllowP" />
效果图:
用https://www.doczj.com/doc/a415725697.html,做
网上讲了很多直接绑定数据库的我这里就不用那方法了。
新建一个文件夹叫Data的用来存储数据
下面就是前台的代码:
<%@OutputCache Duration="1"VaryByParam="None" %>
<%@Page Title=""Language="C#"MasterPageFile="~/Views/Shared/AdminSite.Master"
AutoEventWireup="true"CodeBehind="UserManage.aspx.cs" Inherits="https://www.doczj.com/doc/a415725697.html,erManage" %>
用户管理
Width="120px"/>
AutoGenerateColumns="False"AllowPaging="True"PageSize="12" DataKeyNames="UserId" DataMember="Telephone,Email,DepartmentId"GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" onselectedindexchanged="GridView1_SelectedIndexChanged"> Visible="false"/> ItemStyle-HorizontalAlign="Center"> <%# Eval("Telephone")%> runat="server"Width="100px"/> ItemStyle-HorizontalAlign="Center"> <%# Eval("Email") %> runat="server"Width="140px"/> ItemStyle-HorizontalAlign="Center"> <%#DepartmentIdToNameConverter(Eval("DepartmentId"))%> Width="90px"/> ItemStyle-HorizontalAlign="Center"/> ReadOnly="True"ItemStyle-HorizontalAlign="Center"/> ReadOnly="True"ItemStyle-HorizontalAlign="Center"/>
下面是后天运行的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OrderFood.Views.Admin
{
public partial class UserManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
private void GridViewBind()
{
GridView1.DataSource = https://www.doczj.com/doc/a415725697.html,erInfo.ToList();
GridView1.DataBind();
}
///
///增加用户
///
///
///
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("EditUserForm.aspx");
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("ddlDepartments")) != null)
{
DropDownList ddlDepartments = (DropDownList)e.Row.FindControl("ddlDepartments");
// 生成 DropDownList 的值,绑定数据
ddlDepartments.DataSource = DataStore.OrderFoodDataContext.Department; ddlDepartments.DataTextField = "DepartmentName";
ddlDepartments.DataValueField = "DepartmentId";
ddlDepartments.DataBind();
// 选中 DropDownList
ddlDepartments.SelectedValue = ((HiddenField)e.Row.FindControl("hdfDepartmentId")).Value;
//Label labelDepartment = (Label)e.Row.FindControl("labelDepartment");
//labelDepartment.Text = ((https://www.doczj.com/doc/a415725697.html,erInfo)e.Row.DataItem).Department.DepartmentName;
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
string userId = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string telephone = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTelephone")).Text;
string email = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmail")).Text;
string departmentId = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlDepartments")).SelectedValue;
var first = https://www.doczj.com/doc/a415725697.html,erInfo.FirstOrDefault(p => https://www.doczj.com/doc/a415725697.html,erId == userId);
if (first != null)
{
first.Telephone = telephone;
first.Email = email;
first.DepartmentId = Convert.ToInt32(departmentId);
DataStore.OrderFoodDataContext.SubmitChanges();
}
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
string userId = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
if (userId == "admin")
{
Helpers.HtmlHelper.Show(this, "管理员用户禁止删除!");
return;
}
var first = https://www.doczj.com/doc/a415725697.html,erInfo.FirstOrDefault(p => https://www.doczj.com/doc/a415725697.html,erId == userId);
if (first != null)
{
https://www.doczj.com/doc/a415725697.html,erInfo.DeleteOnSubmit(first);
DataStore.OrderFoodDataContext.SubmitChanges();
}
GridView1.EditIndex = -1;
GridViewBind();
}
public string DepartmentIdToNameConverter(object id)
{
return DataStore.OrderFoodDataContext.Department.FirstOrDefault(p => p.DepartmentId == (int)id).DepartmentName;
}
}
}
希望能帮到需要的。一起加油!