http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4569|回复: 0

[Asp.Net] GridView的精妙使用

[复制链接]
发表于 2012-7-4 20:17:44 | 显示全部楼层 |阅读模式
gridview.jpg
下面的界面Message_Library.aspx
[HTML] 纯文本查看 复制代码
代码 
<%@ 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="width:90%;" cellpadding="0" cellspacing="0" align="center">
        <tr>
            <td class="td_bg" colspan="2">
                                                             
                <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>
                                   <asp:Button 
                    ID="btnSelectbyType" runat="server" Text="查询" 
                    Height="23px" />
                                                   
            </td>
        </tr>
        <tr>
            <td class="style1">
                短 信 内  容:</td>
            <td class="style2">
                <asp:TextBox ID="txtContent" runat="server" Width="361px" Height="102px" 
                    TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                录 入  时 间:</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>
                 
                <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">
                            
                <asp:Button ID="btnInsert" runat="server" Text="保存" />
       <asp:Button ID="btnSelectAll" runat="server" 
                    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

[C#] 纯文本查看 复制代码
代码 
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.Cells[j].ToolTip = gvMessage.Rows.Cells[j].Text;
                    //长度过八个字符的时候隐藏显示
                    if (gvMessage.Rows.Cells[j].Text.Length > 8)
                    {
                        gvMessage.Rows.Cells[j].Text = gvMessage.Rows.Cells[j].Text.Substring(0, 8) + "...";
                    }
                }
            }
        }
        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();
        }
    }
}



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-15 16:06

© 2014-2021

快速回复 返回顶部 返回列表