zoukankan      html  css  js  c++  java
  • GridView的精妙使用

    下面的界面Message_Library.aspx

    代码
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Message_Library.aspx.cs" Inherits="PersonnelManagement_Messages_Message_Library" %>

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    <%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %>

    <!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>Message_Library</title>
        
    <link href="http://www.cnblogs.com/App_Themes/personnel_Css/forum.css" rel="stylesheet" type="text/css" />
        
    <link href="http://www.cnblogs.com/App_Themes/personnel_Css/tablecss.css" rel="stylesheet" type="text/css" />
        
    <style type="text/css">
            .style1
            
    {
                border-right-style
    : groove;
                border-bottom-style
    : groove;
                border-left-style
    : groove;
                border-right-color
    : #006699;
                border-bottom-color
    : #006699;
                border-left-color
    : #006699;
                border-right-width
    : 1px;
                border-bottom-width
    : 1px;
                border-left-width
    : 1px;
            
    }
            .style2
            
    {
                border-right
    : 1px groove #3877A9;
                border-bottom
    : 1px groove #3877A9;
                
    }
        
    </style>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <asp:ScriptManager ID="ScriptManager1" EnableScriptGlobalization="true" runat="server">
        
    </asp:ScriptManager>
        
    <table style="90%;" cellpadding="0" cellspacing="0" align="center">
            
    <tr>
                
    <td class="td_bg" colspan="2">
                    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    
    <asp:Label ID="Label1" runat="server" ForeColor="White" Text="短信类别添加"></asp:Label>
                
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1">
                    短信类别名称:
    </td>
                
    <td class="style2">
                    
    <asp:DropDownList ID="ddlMesageType" runat="server" CssClass="ddlmargin-top" 
                        Height
    ="24px" 
                        Width
    ="138px">
                    
    </asp:DropDownList>
                    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button 
                        
    ID="btnSelectbyType" runat="server" onclick="btnSelect_Click" Text="查询" 
                        Height
    ="23px" />
                    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1">
                    短 信 内
    &nbsp; 容:</td>
                
    <td class="style2">
                    
    <asp:TextBox ID="txtContent" runat="server" Width="361px" Height="102px" 
                        TextMode
    ="MultiLine"></asp:TextBox>
                
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1">
                    录 入
    &nbsp; 时 间:</td>
                
    <td class="style2">
                 
    <cc1:CalendarExtender ID="CalendarExtender1" Format="yyyy-M-dd"  TargetControlID="txtTime" runat="server">
                    
    </cc1:CalendarExtender>
                    
    <asp:TextBox ID="txtTime" runat="server" Width="132px"></asp:TextBox>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    
    <asp:Button ID="btnSelectByTime" runat="server" Text="查询" Height="23px" 
                        onclick
    ="btnSelectByTime_Click" />
                    
    <asp:Label ID="Label2" runat="server" ForeColor="Red" Text="时间仅用于查询"></asp:Label>
                
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1">
                    短信 点 击率:
    </td>
                
    <td class="style2">
                    
    <asp:TextBox ID="txtCount" runat="server" Width="132px"></asp:TextBox>
                    
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                        ControlToValidate
    ="txtCount" ErrorMessage="只能是1到8位的数字" 
                        ValidationExpression
    ="\d{1,8}"></asp:RegularExpressionValidator>
                
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1" colspan="2">
                    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    
    <asp:Button ID="btnInsert" runat="server" Text="保存" onclick="btnInsert_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:Button ID="btnSelectAll" runat="server" onclick="btnSelectAll_Click" 
                        Text
    ="查询所有" />
                    
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1" colspan="2">
                    
    <asp:Label ID="lblMessage" runat="server" Text="lblMessage" Visible="False" 
                        ForeColor
    ="Red"></asp:Label>
                
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1" colspan="2">
                    
    <asp:GridView ID="gvMessage" runat="server" AutoGenerateColumns="False" 
                        BackColor
    ="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
                        CellPadding
    ="3" Width="100%" 
                        onrowcancelingedit
    ="gvMessageType_RowCancelingEdit" 
                        onrowdeleting
    ="gvMessageType_RowDeleting" 
                        onrowediting
    ="gvMessageType_RowEditing" 
                        onrowupdating
    ="gvMessageType_RowUpdating">
                        
    <FooterStyle BackColor="White" ForeColor="#000066" />
                        
    <RowStyle ForeColor="#000066" />
                        
    <Columns>
                            
    <asp:BoundField DataField="MB_Id" HeaderText="短信ID" ReadOnly="True" 
                                SortExpression
    ="MB_Id" />
                            
    <asp:BoundField DataField="Messagetype_Name" HeaderText="类型名称" ReadOnly="True" 
                                SortExpression
    ="Messagetype_Name" />
                            
    <asp:BoundField DataField="Message_Content" HeaderText="短信内容" 
                                SortExpression
    ="Message_Content" />
                            
    <asp:BoundField DataField="Message_Time" HeaderText="添加时间" ReadOnly="True" 
                                SortExpression
    ="Message_Time" />
                            
    <asp:BoundField DataField="Message_Count" HeaderText="点击率" 
                                SortExpression
    ="Message_Count" />
                            
    <asp:CommandField ShowEditButton="True" />
                            
    <asp:CommandField ShowDeleteButton="True" />
                        
    </Columns>
                        
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    
    </asp:GridView>
                
    </td>
            
    </tr>
            
    <tr>
                
    <td class="style1" colspan="2">
             
    <webdiyer:AspNetPager 
                        
    ID="AspNetPager1" runat="server" BorderColor="#006699" FirstPageText="第一页" 
                        HorizontalAlign
    ="Center" LastPageText="最后一页" NextPageText="下一页" 
                        onpagechanged
    ="AspNetPager1_PageChanged1" PageIndexBoxType="DropDownList" 
                        PrevPageText
    ="上一页" ShowCustomInfoSection="Right" 
                        ShowPageIndexBox
    ="Always" SubmitButtonText="GO" TextAfterPageIndexBox="页" 
                        TextBeforePageIndexBox
    ="转到" 
                        CustomInfoHTML
    ="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条" 
                        AlwaysShow
    ="True">
                    
    </webdiyer:AspNetPager>
                   
                
    </td>
            
    </tr>
        
    </table>
        
    </form>
    </body>
    </html>

    下面是Message_Library.aspx.cs

    代码
    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 ChinaMobleBll.PersonalBll;

    public partial class PersonnelManagement_Messages_Message_Library : System.Web.UI.Page
    {
        MessagesManagerService objMessagesManagerService 
    = new MessagesManagerService();
        DataTable dt_Message 
    = new DataTable();
        
    //短信添加时间
        private static string TimeMesage = "";
        
    private static string MessageName = "";
        
    //0查询所有,1根据名称查询
        private static int F = 0;
        
    //验证是否存在这个名称

        
    private void dtMessage()
        {
            
    try
            {
                
    //0查询所有
                if (F == 0)
                {
                    AspNetPager1.RecordCount 
    = Convert.ToInt32(objMessagesManagerService.selectAllMessage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[1].Rows[0][0].ToString().Trim());
                    dt_Message 
    = objMessagesManagerService.selectAllMessage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[0];
                }
                
    //1根据名称查询
                else if (F == 1)
                {
                    AspNetPager1.RecordCount 
    = Convert.ToInt32(objMessagesManagerService.selectAllMessageByName(MessageName, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[1].Rows[0][0].ToString().Trim());
                    dt_Message 
    = objMessagesManagerService.selectAllMessageByName(MessageName, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[0];
                }
                
    //2根据时间查询
                else if (F == 2)
                {
                    AspNetPager1.RecordCount 
    = Convert.ToInt32(objMessagesManagerService.selectAllMessageByTime(TimeMesage, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[1].Rows[0][0].ToString().Trim());
                    dt_Message 
    = objMessagesManagerService.selectAllMessageByTime(TimeMesage, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[0];
                }
                gvMessage.DataSource 
    = dt_Message;
                gvMessage.DataBind();

                
    // 使用GridView自带的ToolTip隐藏过长的数据
                for (int j = 1; j < gvMessage.Columns.Count; j++)
                {
                    
    for (int i = 0; i < gvMessage.Rows.Count; i++)
                    {
                        gvMessage.Rows[i].Cells[j].ToolTip 
    = gvMessage.Rows[i].Cells[j].Text;
                        
    //长度过八个字符的时候隐藏显示
                        if (gvMessage.Rows[i].Cells[j].Text.Length > 8)
                        {
                            gvMessage.Rows[i].Cells[j].Text 
    = gvMessage.Rows[i].Cells[j].Text.Substring(08+ "...";
                        }
                    }
                }
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }
        
    protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Visible 
    = false;
            
    try
            {
                
    if (!IsPostBack)
                {
                    
    //初始化类型名称
                    ddlMesageType.DataSource = objMessagesManagerService.selectAllMessageTypeByName()[0];
                    ddlMesageType.DataTextField 
    = "Messagetype_Name";
                    ddlMesageType.DataBind();
                    F 
    = 0;
                    
    //刷新数据
                    dtMessage();
                }
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }

        
    //名称查询
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            
    try
            {
                MessageName 
    = ddlMesageType.Text.ToString().Trim();
                F 
    = 1;
                
    //刷新数据
                dtMessage();
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }

        
    //查询所有
        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            
    try
            {
                F 
    = 0;
                
    //刷新数据
                dtMessage();
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }

        
    //添加
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            
    try
            {
                
    if (objMessagesManagerService.CP_InsertAllMessage(ddlMesageType.Text.ToString().Trim(), txtContent.Text.ToString().Trim(), txtCount.Text.ToString().Trim()))
                {
                    lblMessage.Visible 
    = true;
                    lblMessage.Text 
    = "添加成功";
                    
    //刷新数据
                    dtMessage();
                }
                
    else
                {
                    lblMessage.Visible 
    = true;
                    lblMessage.Text 
    = "添加失败";
                }
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = "注意点击率只能是整数";
            }
        }

        
    //取消
        protected void gvMessageType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            
    try
            {
                
    if (e.RowIndex >= 0)
                {
                    gvMessage.EditIndex 
    = -1;
                }
                
    //刷新数据
                dtMessage();
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }

        
    //删除
        protected void gvMessageType_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
    try
            {
                
    string ID = gvMessage.Rows[e.RowIndex].Cells[0].Text.ToString().Trim();
                
    if (objMessagesManagerService.DeleteAllMessage(ID))
                {
                    
    //刷新数据
                    dtMessage();
                    lblMessage.Visible 
    = true;
                    lblMessage.Text 
    = "删除成功";
                }
                
    else
                {
                    lblMessage.Visible 
    = true;
                    lblMessage.Text 
    = "删除失败";
                }
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }

        
    //编辑时
        protected void gvMessageType_RowEditing(object sender, GridViewEditEventArgs e)
        {
            
    try
            {
                
    if (e.NewEditIndex >= 0)
                {
                    gvMessage.EditIndex 
    = e.NewEditIndex;
                }
                
    //刷新数据
                dtMessage();
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }

        
    //更新
        protected void gvMessageType_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
    try
            {
                
    string ID = gvMessage.Rows[e.RowIndex].Cells[0].Text.ToString().Trim();
                
    string Content = ((TextBox)gvMessage.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();
                
    string count = ((TextBox)gvMessage.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString().Trim();
                
    if (objMessagesManagerService.CP_UpdateAllMessage(ID, Content, count))
                {
                    gvMessage.EditIndex 
    = -1;
                    
    //刷新数据
                    dtMessage();
                }
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }

        
    //分页
        protected void AspNetPager1_PageChanged1(object sender, EventArgs e)
        {
            
    try
            {
                
    //刷新数据
                dtMessage();
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }
        
    //时间查询
        protected void btnSelectByTime_Click(object sender, EventArgs e)
        {
            
    try
            {
                TimeMesage 
    = txtTime.Text.ToString().Trim();
                F 
    = 2;
                
    //刷新数据
                dtMessage();
            }
            
    catch (Exception ex)
            {
                lblMessage.Visible 
    = true;
                lblMessage.Text 
    = ex.Message.ToString().Trim();
            }
        }
    }

     阅读全文,下载代码:http://www.cckan.net/forum.php?mod=viewthread&tid=34

  • 相关阅读:
    oracle 用户 权限
    oracle move 释放 表空间
    oracle 视图 参数
    js 、c# 编码解码
    Oracle实体化视图
    简单工厂VS工厂方法
    面向对象——(9)封装和接口
    写给明天的软件工程师——(转)
    设计模式简介
    设计模式的六大原则
  • 原文地址:https://www.cnblogs.com/sufei/p/1485995.html
Copyright © 2011-2022 走看看