GridView基于单元格的更新

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();//重新绑定
    }
} 


难点ASP代码:


	   <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" οnclick="GetAllCheckBox(this)" />全选
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chbSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值