gridview弹出窗口

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class System_ManageDepartment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = null;
            BindGridView();
        }
    }

    protected void ImgDepartment_Click(object sender, ImageClickEventArgs e)
    {
        if (!JS.isCookiesAvailabile())
        {
            return;
        }

        //插入部门名称的SQL语句
        StringBuilder strsql = new StringBuilder("INSERT INTO DICT_DEPARTMENT(DEPARTMENTNAME) VALUES('")
                               .Append(txbDepartment.Text.Trim())
                               .Append("')");

        try
        {
            if (txbDepartment.Text.Trim() != "")
            {
                //插入用户增加的部门名称

                DbHelperSQL.ExecuteSql(strsql.ToString());

                //显示所有的部门名称
                BindGridView();

                //清空文本框中的内容

                txbDepartment.Text = "";
            }
            else
            {
                JS.Alert("部门名称不能为空!请输入部门名称!");
            }
        }
        catch (Exception ex)
        {
            JS.Alert("操作失败,详细信息:" + ex.Message.Replace("\r\n", "").Replace("'", ""));
        }
    }


    #region 绑定GridView1
    protected void BindGridView()
    {
        if (!JS.isCookiesAvailabile())
        {
            return;
        }

        try
        {
            DataTable dt = DbHelperSQL.gettable("SELECT * FROM DICT_DEPARTMENT WHERE DEPARTMENTID>0");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            JS.Alert("操作失败,详细信息:" + ex.Message.Replace("\r\n", "").Replace("'", ""));
        }
    }
    #endregion

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGridView();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string departmentname = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txbDepartment")).Text.Trim();
        if (!string.IsNullOrEmpty(departmentname))
        {
            StringBuilder strsql = new StringBuilder("UPDATE DICT_DEPARTMENT SET DEPARTMENTNAME='")
                                   .Append(departmentname)
                                   .Append("'")
                                   .Append(" WHERE DEPARTMENTID='")
                                   .Append(GridView1.DataKeys[e.RowIndex].Value)
                                   .Append("'");
         
            try
            {
                DbHelperSQL.ExecuteSql(strsql.ToString());
                GridView1.EditIndex = -1;
                BindGridView();
            }
            catch (Exception ex)
            {
                JS.Alert("修改部门名称失败,详细信息:" + ex.Message.Replace("\r\n", "").Replace("'", ""));
            }
        }
        else
        {
            JS.Alert("部门名称不可为空,请重新输入!");
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        StringBuilder strsql = new StringBuilder("DELETE FROM DICT_DEPARTMENT WHERE DEPARTMENTID='")
                               .Append(GridView1.DataKeys[e.RowIndex].Value)
                               .Append("'");
       
        try
        {
            DbHelperSQL.ExecuteSql(strsql.ToString());
            BindGridView();

        }
        catch (Exception ex)
        {
            JS.Alert("删除部门名称失败!" + ex.Message.Replace("\r\n", "").Replace("'", ""));
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (null == this.GridView1.DataSource)
        {
            return;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Add("onclick", "return confirm('确定要永久删除该条记录吗?');");

            int index = e.Row.RowIndex;
            if (index >= 0)
            {
                if (this.GridView1.DataSource != null)
                {
                    DataTable dt = ((DataTable)this.GridView1.DataSource);
                    e.Row.Cells[1].Text = "分配权限";
                    e.Row.Cells[1].Attributes.Add("onclick", string.Format("window.open('DepartmentPower.aspx?id={0}','','left=250,top=100,scrollbars=yes,width=700,height=470,resizable=yes');", dt.Rows[e.Row.RowIndex]["DEPARTMENTID"]));
            
                }
            }
        }
    }
}
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ManageDepartment .aspx.cs" Inherits="System_ManageDepartment" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>部门管理</title>
    <link href="../css/CRM.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <table width="100%" height="100%">
             <tr height="20px">
           <td colspan="3" style="font-weight: bold; font-size: 20px; color: white;  height: 5px;
                    font-family: 楷体_GB2312; height: 27px; background-color: #63769b">
                    部门管理
           </td>
       </tr>
            <tr height="20px">
                <td align="center" style="width: 20%; font-weight: bold; font-size:20px; color:Black; height:5px; font-family: 楷体_GB2312; height:27px;">
                   部门名称:</td>
                <td width="30%">
                    <asp:TextBox ID="txbDepartment" MaxLength="20" Width="300px" Height="20px" runat="server"></asp:TextBox>
                </td>
                <td align="left">
                    &nbsp;<asp:ImageButton ID="ImgDepartment" runat="server" ImageUrl="~/Images/button/btnAdd.gif"
                        OnClick="ImgDepartment_Click" /></td>
            </tr>
            <tr valign="top">
                <td colspan="3">
                    <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
                    GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
                    OnRowUpdating="GridView1_RowUpdating" DataKeyNames="DEPARTMENTID" OnRowCreated="GridView1_RowCreated"
                    OnRowDeleting="GridView1_RowDeleting">
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#EFF3FB" Height="13px" />
                        <EditRowStyle BackColor="#2461BF" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Height="13px" />
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="部门名称" >
                                <EditItemTemplate>
                                    <asp:TextBox ID="txbDepartment" MaxLength="20" runat="server" Text='<%# Eval("DEPARTMENTNAME") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblDepartment" runat="server" Text='<%# Eval("DEPARTMENTNAME") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:BoundField  HeaderText="权限">
                                            <HeaderStyle Wrap="False"  />
                                            <ItemStyle Wrap="False" Font-Bold="True" Width="60" ForeColor="MidnightBlue"/>
                                        </asp:BoundField>
                            <%--<asp:HyperLinkField DataNavigateUrlFields="DEPARTMENTID" DataNavigateUrlFormatString="DepartmentPower.aspx?id={0}"
                                 HeaderText="权限" Target="_blank" Text="分配权限" ItemStyle-Width=60>
                                <HeaderStyle Wrap="False" />
                            </asp:HyperLinkField>--%>
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" ItemStyle-Width=40 />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True"  ItemStyle-Width=40/>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

posted @ 2014-03-29 15:22  开开123  阅读(269)  评论(0编辑  收藏  举报