GridView的使用及分页

 

--------------------------------------------Company.aspx-----------------------------------------------------

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Company.aspx.cs" Inherits="member_page_company_news_Company" %>

<%@ Register Src="../Control/left.ascx" TagName="left" TagPrefix="uc1" %>
<%@ Register Src="../Control/top.ascx" TagName="top" TagPrefix="uc2" %>
<%@ Register Src="../Control/botton.ascx" TagName="botton" TagPrefix="uc3" %>

<!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 runat="server">
    <title>无标题页</title>
  <link href="../style/sys_Mzb.css" type="text/css" rel="Stylesheet" />
 
  <script language="javascript">
  function isempty(txt_Company_title,txt_Company_name,txt_Company_content,txt_Company_people)
  {
 
  if(txt_Company_title.value=="")
  {
alert("新闻标题不能为空"); 
txt_Company_title.focus();
  return  false;
  } 
if(txt_Company_name.value=="")
{
alert("企业名称不能为空");
txt_Company_name.focus();
return false;

}
if(txt_Company_content.value=="")
{
alert("企业内容不能为空");
txt_Company_content.focus();
return false;
}

if(txt_Company_people.value=="")
{
alert("联系人不能为空");
txt_Company_people.focus();
return false;
}

}
 
  </script>
  <style type="text/css">
<!--
body {
 margin-left: 0px;
 margin-top: 0px;
 margin-right: 0px;
 margin-bottom: 0px;
}

-->
</style>
    <link href="../style/left.css" rel="stylesheet" type="text/css" />
    <link href="../style/left.css" rel="stylesheet" type="text/css" />
    <link href="../style/left.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../style/left.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />
    <link href="../../style/invite.css" rel="stylesheet" type="text/css" />

 
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <table width="775" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="80" bgcolor="#DEEFEF">&nbsp;<uc2:top ID="Top1" runat="server" />
    </td>
  </tr>
</table>

 

<table width="775" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="4" colspan="3"></td>
  </tr>
  <tr>
    <td width="160" valign="top" bgcolor="#BDDFDE">
        &nbsp;<uc1:left ID="Left1" runat="server" />
        </td>
    <td width="5">&nbsp;</td>
    <td valign="top" bgcolor="#DEEFEF">&nbsp;
   
   
   
   
   
   
     <table width="100%" border="0" cellspacing="0" cellpadding="0" id="table1" runat="server">
         <tr>
            <td style="height: 287px"><table width="100%" border="0" cellpadding="0" cellspacing="2" bgcolor="#BDDFDE" class="Mzb_button">
              <tr>
                <td colspan="4" align="center" bgcolor="#BDDFDE">
                    <strong>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></strong></td>
              </tr>
              <tr>
                <td height="25" align="center" bgcolor="#DEEFEF" style="width: 103px">
                    新闻标题:</td>
                <td colspan="3" bgcolor="#DEEFEF">&nbsp;
                    <asp:TextBox ID="txt_Company_title" runat="server" Width="225px"></asp:TextBox>
                  </td>
              </tr>
              <tr>
                <td align="center" bgcolor="#DEEFEF" style="height: 24px; width: 103px;">
                    企业名称:</td>
                <td colspan="3" bgcolor="#DEEFEF" style="height: 24px"> &nbsp;&nbsp;<asp:TextBox ID="txt_Company_name" runat="server" Width="223px"></asp:TextBox></td>
              </tr>
              <tr>
                <td align="center" bgcolor="#DEEFEF" style="height: 65px; width: 103px;">
                    新闻内容:</td>
                <td colspan="3" bgcolor="#DEEFEF" style="height: 65px">&nbsp;<font color="#f95644">
                    <asp:TextBox ID="txt_Company_content" runat="server" Height="277px" TextMode="MultiLine" Width="473px"></asp:TextBox></font><font color="#f95644"></font></td>
              </tr>
                <tr>
                    <td align="center" bgcolor="#deefef" style="height: 15px; width: 103px;">
                        发布人</td>
                    <td bgcolor="#deefef" colspan="3" style="height: 15px">
                        &nbsp;
                        <asp:TextBox ID="txt_Company_people" runat="server"></asp:TextBox>
                        <asp:HiddenField ID="Hi" runat="server" />
                    </td>
                </tr>
            </table></td>
          </tr>
          <tr>
            <td align="center" bgcolor="#BDDFDE" style="height: 32px">
                <asp:Button ID="btn_y" runat="server" Text="提交" Width="53px" 5 Font-Bold="True" OnClick="btn_y_Click" CausesValidation="False" /></td>
          </tr>
         
        </table>
        <div style="text-align: center">
            <table border="0" id="table2" cellpadding="0" cellspacing="0" style="width: 100%" runat="server">
                <tr>
                    <td style="width: 100%; height: 70px" align="center">
                        <asp:GridView ID="GridView1" runat="server" SelectedRowStyle-Height="20px" AutoGenerateColumns="False" Width="100%"   DataKeyNames="Company_id" HorizontalAlign="Center" OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound"  OnRowDeleting="GridView1_RowDeleting" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
                            <FooterStyle HorizontalAlign="Center" />
                            <EmptyDataRowStyle HorizontalAlign="Center" />
                            <Columns>
                                <asp:BoundField DataField="Company_title" HeaderText="新闻标题" >
                                    <ItemStyle HorizontalAlign="Center" Width="15%" />
                                </asp:BoundField>
                                <asp:BoundField DataField="Company_name" HeaderText="企业名称" >
                                    <ItemStyle HorizontalAlign="Center" Width="15%" />
                                </asp:BoundField>
                                <asp:BoundField DataField="Company_people" HeaderText="发布人" />
                                <asp:BoundField DataField="Company_date" HeaderText="发布时间" />
                                <asp:TemplateField ShowHeader="False" HeaderText="修改">
                                    <EditItemTemplate>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                            Text="更新"></asp:LinkButton>
                                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                            Text="取消"></asp:LinkButton>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit"
                                            Text="编辑"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField ShowHeader="False" HeaderText="删除">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                                            Text="删除"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                           
                            <PagerTemplate>
          <table style="width: 540px; text-align: center" class="font14">
          <tr>
         

 

                 <td style="height: 23px; text-align: center;">
                 当前页:<font color=red><asp:Label ID="LabelCurrentPage" runat="server"
Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> </font>&nbsp;&nbsp;
共<font color=red><asp:Label ID="LabelPageCount" runat="server"
Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label></font> 页

                  
          
              <asp:LinkButton ID="btnFirst" runat="server" CommandArgument="First" CommandName="Page" Text="首页" Enabled="<%# this.GridView1.PageIndex > 0 %>" />
   <asp:LinkButton ID="btnPrev" runat="server" CommandArgument="Prev" CommandName="Page" Text="上一页" Enabled="<%# this.GridView1.PageIndex > 0 %>" />
                    
<asp:LinkButton ID="btnNext" runat="server" CommandArgument="Next" CommandName="Page" Text="下一页" Enabled="<%# this.GridView1.PageIndex < this.GridView1.PageCount - 1 %>" />
<asp:LinkButton ID="btnLast" runat="server" CommandArgument="Last" CommandName="Page" Text="末页" Enabled="<%# this.GridView1.PageIndex < this.GridView1.PageCount - 1 %>" />
<%--<asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0}/{1}", this.GridView1.PageIndex + 1, this.GridView1.PageCount) %>'></asp:Label>
--%><%--<asp:TextBox ID="tbxPage" runat="server" Width="30px" Text="0"></asp:TextBox>--%>
<%--<asp:LinkButton ID="btnGoto" runat="server" CommandName="Page"  Text="Go" />
--%>
<%--<asp:Button ID="btnGoto" runat="server" CommandName="Page" Text="Go" CommandArgument="go" />--%>
<%--<asp:RegularExpressionValidator ID="revPage" runat="server" ControlToValidate="tbxPage" ValidationExpression="/d+" SetFocusOnError="True" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RangeValidator ID="rvlPage" runat="server" ControlToValidate="tbxPage" ErrorMessage="请输入正确的页数!"
        Type="Integer"   MinimumValue="1" MaximumValue="<%# this.GridView1.PageCount %>"
        SetFocusOnError="false"  Display="Dynamic"></asp:RangeValidator>--%>
          
          
        
                 </td>
                 </tr>
             </table>
         </PagerTemplate>
                            <RowStyle Height="20px" HorizontalAlign="Center" />
                            <SelectedRowStyle Height="20px" HorizontalAlign="Center" />

                           
                        </asp:GridView>
                    </td>
                </tr>
        
            </table>
        </div>
        <asp:Panel ID="Panel1"  runat="server"   Height="50px" Width="100%" Visible="False">
          &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:Panel>
   
   
   
   
    </td>
  </tr>
</table>
        <div style="text-align: center">
            <table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
                <tr>
                    <td style="width: 100px" align="center">
                        <uc3:botton ID="Botton1" runat="server" />
                    </td>
                </tr>
            </table>
        </div>
        <div style="text-align: center">
            &nbsp;</div>


   
    </div>
    </form>
</body>
</html>

 

 

 

 

 

---------------------------------Company.aspx.cs-----------------------------------

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.Data.SqlClient;
public partial class member_page_company_news_Company : System.Web.UI.Page
{
    Member_all c_company = new Member_all(); //实例化类
      
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.ControlStyle.Height = 20;
    
       btn_y.Attributes.Add("onclick", "return isempty(" + txt_Company_title.ClientID + ","+txt_Company_name.ClientID+","+txt_Company_content.ClientID+","+txt_Company_people.ClientID+");");  //用于弹出对话框 
        if (!IsPostBack)
        {

            Hi.Value = "0";

         
          
            if (Request.QueryString["Name"] != null)
            {
              
       
      
                string add = Request.QueryString["Name"].ToString();
                if (add == "Add")//这是发布的,传过来的值为Add,显示的隐藏,只显示发布内容
                {
                  
                    Label1.Text = "<strong>企业新闻发布<strong>";
                    table1.Visible = true;
                    table2.Visible = false;
                }
            }

            else
            {
                if(GridView1.PageCount<0)
                {
                    Panel1.Visible = true;
               
            }
            else
            {
            edit_company(0);//调用修改方法,因为是修改,暂时传参为0,以便进入修必
                    //如果:点击修改,就把发布的隐藏
                Label1.Text = "<strong>修改企业新闻发布<strong>";
          
                table1.Visible = false;
                table2.Visible = true;

            }}
           
        }


    }


    protected void Company_databind()
    {

        int databind_id = 0;
        GridView1.DataSource = Member_all.Company_display(databind_id,Session["User_name"].ToString(),Session["User_id"].ToString());
            GridView1.DataBind();

          
       
   
    }
    protected void btn_y_Click(object sender, EventArgs e)
    {


       // int id1=Convert.ToInt32(Hi.Value);
     
      
       int id1 = Convert.ToInt32(Hi.Value);
      
        if (id1> 0)
        {
            ArrayList arralist = new ArrayList();

            try
            {//这是用来更新的
                arralist.Add(SqlZhuru.SQLzhuru(txt_Company_title.Text.Trim()));
                arralist.Add(SqlZhuru.SQLzhuru(txt_Company_name.Text.Trim()));
                arralist.Add(SqlZhuru.SQLzhuru(txt_Company_content.Text.Trim()));
                arralist.Add(SqlZhuru.SQLzhuru(txt_Company_people.Text.Trim()));
                arralist.Add(System.DateTime.Now);
                arralist.Add(0);

                btn_y.Text = "修改";
                //c_company._title = txt_Company_title.Text.Trim();
                //c_company._name = txt_Company_name.Text.Trim();
                //c_company._Content = txt_Company_content.Text.Trim();
                //c_company._people = txt_Company_people.Text.Trim();
                c_company.Company_update(id1, arralist);

                Jscript.Alert("更新成功");
                table1.Visible = false;
                table2.Visible = true;

            }
            catch
            {

                Jscript.Alert("您的填写有误,请正确填写");
            }
         
        }
        else
        {
            //这是添加用的

//SqlZhuru.SQLzhuru为防注入类的,在这里就不介绍了
            c_company._title = SqlZhuru.SQLzhuru(txt_Company_title.Text.Trim());
            c_company._name = SqlZhuru.SQLzhuru(txt_Company_name.Text.Trim());
            c_company._Content = SqlZhuru.SQLzhuru(txt_Company_content.Text.Trim());
            c_company._people = SqlZhuru.SQLzhuru(txt_Company_people.Text.Trim());
            c_company.Company_insert(Session["User_name"].ToString(),Session["User_id"].ToString());
            Jscript.Alert("添加成功");
            Label1.Text = "<strong>企业新闻发布<strong>";
            table2.Visible = true;
            table1.Visible = false;

       
        }
        Company_databind();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        edit_company(Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value));
    }

    private void edit_company(int id)


    {
        Hi.Value =id.ToString();
     
        if (id > 0)
        {
            DataTable dt;
            dt = Member_all.Company_display(id, Session["User_name"].ToString(), Session["User_id"].ToString());
            if (dt.Rows.Count > 0)
            {
                txt_Company_title.Text = dt.Rows[0][0].ToString();
                txt_Company_name.Text = dt.Rows[0][1].ToString();
                txt_Company_content.Text = dt.Rows[0][2].ToString();
                txt_Company_people.Text = dt.Rows[0][3].ToString();     
           
             }
        
             table1.Visible = true;
             table2.Visible = false;
          }

          Company_databind();
  }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GridView1.PageIndex = e.NewPageIndex;//这是分页的内容
    
                Company_databind();
    
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       c_company.Company_del(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value));//获取GridViews主键的索引的值
        Company_databind();
            }

 

 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        c_company.get_rows(ref e,5,1);//这是调用的行颜色的变化,及弹出删除对话框

    }
   // protected void Page_databind()
   // {

 

   //     GridViewRow pagerRow = GridView1.BottomPagerRow;            // 获得 GridView 控件中的底部页导航行。
   //     //TextBox txt = (TextBox)pagerRow.Cells[0].FindControl("tbxPage");
   //     LinkButton link = (LinkButton)pagerRow.Cells[0].FindControl("btnGoto");
   //     link.CommandArgument = txt.Text.Trim();

   //}

  
}

 

 

 

 

 

 

 

 

 

以上代码涉及到的类如下:

这是业务类:Member_all.cs

Member_all:可以继承于base的类,Memeber_all:base,你也可以实例化,不继取

 

    #region //用于发布企业新闻插入
    public  void Company_insert(string username,string userid)
            {

       string Company_sql = "insert into Company_news(Company_title,Company_name,Company_content,Company_people,Company_date,User_name,User_id)Values('" + this.title + "','" + this.name + "','" + this.Content + "','" + this.people + "','"+System.DateTime.Now+"','"+username+"','"+userid+"')";
        ExcSQL(Company_sql);

    }

    #region

    public  void Company_update(int Company_id,ArrayList array)
    {

        string Company_update = "update Company_news set Company_name='" + array[1] + "', Company_title='" + array[0]+ "',Company_content='" + array[2] + "', Company_people='" + array[3] + "',Company_date='"+array[4]+"' where Company_id='"+Company_id+"'";

        ExcSQL(Company_update);
    }

    #endregion
    #endregion
    //返回datatable对象
    public static DataTable  Company_display(int id,string user_name,string uid)
    {
        SqlConnection mycon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Str_sql"].ToString());
        DataSet ds = new DataSet();
        mycon.Open();
        if (id > 0)
        {
            SqlDataAdapter pter = new SqlDataAdapter("select Company_title,Company_name,Company_content,Company_people,Company_date, Company_id  from Company_news  where Company_id='"+id+"'", mycon);
            pter.Fill(ds);
        }
        else
        {
            SqlDataAdapter pter1 = new SqlDataAdapter("select Company_title=left(Company_title,6),Company_name=left(Company_name,6),Company_people=left(Company_people,6),Company_date, Company_id  from Company_news where User_name='" + user_name+ "'and User_id='"+uid+"' order by Company_date desc", mycon);
            pter1.Fill(ds);
       
        }
 
      
        
        ds.Dispose();
     
        mycon.Close();
        return ds.Tables[0];
               
   
    }

  // 用于企业信息的删除

    public void Company_del(int id)
    {
        ExcSQL("delete from Company_news where Company_id='"+id+"'");
   
    }
   
    //用于行的操作,颜色的变换,删除
    public void get_rows(ref GridViewRowEventArgs e, int c, int w)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#baddd9'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            if (w > 0)
            {
                ((LinkButton)(e.Row.Cells[c].Controls[w])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");

            }
   
        }
   
   
   
   
   
    }

 

 

 

 

 

 

 

//这是数据类base.cs
    public static void ExcSQL(string s)
    {

        SqlConnection conn = new SqlConnection(strcon);

        try
        {
            SqlCommand sqlcmd = new SqlCommand(s, conn);
            conn.Open();
            sqlcmd.ExecuteNonQuery();

        }

        catch (Exception ex)
        {


            throw (ex);

        }

        finally
        {
            conn.Close();

        }

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值