您的位置:首页 > 其它

GridView基于单元格的更新

2013-07-10 21:24 218 查看
CS代码:





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookShopBLL;
using BookShopModels;

public partial class admin_UserList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataKeyNames = new string[] { "Id"};//设置绑定主键
this.GridView1.DataBind();

BindGridView();

//添加删除按钮的onclick代码
this.btnDelete.Attributes.Add("onclick"," return confirm('确定删除吗?')");
}
}

/// <summary>
/// 绑定数据
/// </summary>
private void BindGridView()
{
this.GridView1.DataSource = UsersManager.GetUser();
this.GridView1.DataBind();
}

/// <summary>
/// 在对行数据绑定后激发的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//给删除按钮添加JavaScript脚本
LinkButton lnkbtnDel = e.Row.FindControl("lnkbtnDel") as LinkButton;
lnkbtnDel.Attributes.Add("onclick", "return confirm('确认删除吗?')");

//光棒效果
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}

//判断是否为空,如果不为空就绑定数据
if (e.Row.FindControl("ddlUserRole") != null)
{
DropDownList ddl = e.Row.FindControl("ddlUserRole") as DropDownList;
ddl.DataSource = UserRoleManager.GetFindUserRole();
ddl.DataTextField = "Name";
ddl.DataValueField = "Id";
ddl.DataBind();
}
}

/// <summary>
/// 根据用户类型Id获取用户类型名称
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string UserRolesName(string id)
{
UserRoles ur = UserRoleManager.GetUserRoleById(Convert.ToInt32(id));
return ur.Name;
}

/// <summary>
/// 选择新行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int id = e.NewSelectedIndex;
string key = this.GridView1.DataKeys[id].Value.ToString();//获取选中行的主键
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('"+key+"');</script>");
}

/// <summary>
/// 编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;//设置当前编辑的行
BindGridView();
}

/// <summary>
/// 更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Users user = new Users();
user.Id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
user.LoginId = (this.GridView1.Rows[e.RowIndex].FindControl("txtLoginId") as TextBox).Text;
user.LoginPwd = (this.GridView1.Rows[e.RowIndex].FindControl("txtLoginPwd") as TextBox).Text;
user.Name = (this.GridView1.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text;
user.Address = (this.GridView1.Rows[e.RowIndex].FindControl("txtAddress") as TextBox).Text;
user.Phone = (this.GridView1.Rows[e.RowIndex].FindControl("txtPhone") as TextBox).Text;
user.Mail = (this.GridView1.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text;
user.UserRole = new UserRoles();
user.UserRole.Id = Convert.ToInt32((this.GridView1.Rows[e.RowIndex].FindControl("ddlUserRole") as DropDownList).SelectedValue);
user.Gender = (this.GridView1.Rows[e.RowIndex].FindControl("ddlGender") as DropDownList).Text;

bool isUpdate = UsersManager.UpdateUser(user);
if (isUpdate)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('修改成功!')</script>");
this.GridView1.EditIndex = -1;
BindGridView();//重新绑定
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('修改失败!')</script>");
return;
}
}

/// <summary>
/// 取消编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
try
{
this.GridView1.EditIndex = -1;//退出编辑
BindGridView();//需要重新绑定数据
}
catch (Exception)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('操作失败!')</script>");
}
}

/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//找到当前删除行Id为lnkbtnDel的LinkButton控件
LinkButton lnkbtnDel=GridView1.Rows[e.RowIndex].FindControl("lnkbtnDel") as LinkButton;
//取得删除行绑定的用户编号
int id = Convert.ToInt32(lnkbtnDel.CommandArgument);
//调用删除方法
bool isDelete = UsersManager.DeleteUser(id);
if (isDelete)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"info","<script>alert('删除成功!')</script>");
BindGridView();//重新绑定
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('删除失败!')</script>");
}
}

/// <summary>
/// 删除全部
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in this.GridView1.Rows)//循环每一行
{
//获取模版列中的CheckBox控件对象
CheckBox chk = gr.FindControl("chbSelect") as CheckBox;
if (chk.Checked)
{
//获得主键Id
int id = Convert.ToInt32(this.GridView1.DataKeys[gr.RowIndex].Value);
UsersManager.DeleteUser(id);//执行删除
}
}
BindGridView();//重新绑定
}
}

[b]

[/b]

[b]难点ASP代码:[/b]



<asp:TemplateField HeaderText="邮箱">
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("Mail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Mail") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="130px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="用户类型">
<ItemTemplate>
<asp:Label runat="server" ID="lblUserRole" Text='<%# UserRolesName(Eval("UserRole.Id").ToString()) %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUserRole" runat="server" AutoPostBack="True">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="性别">
<EditItemTemplate>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem Value="True" Text="男"></asp:ListItem>
<asp:ListItem Value="False" Text="女"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGender" runat="server" Text='<%# Eval("Gender").ToString()=="True"?"男":"女" %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="50px" />
</asp:TemplateField>
<asp:CommandField HeaderText="操作" ShowEditButton="True" />
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDel" runat="server" Text="删除" CommandArgument='<%#Eval("Id") %>' CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="全选">
<HeaderTemplate>
<input type="checkbox" onclick="GetAllCheckBox(this)" />全选
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐