Asp.Net 小型网站的制作(二),涵盖了asp.net里面所有的基础知识

上一边我们说到实现了接口里面的所有方法!
下面我们看一下数据的显示!
首先看一下前台页面的部分

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ClasswebView.aspx.cs" Inherits="WebSql.view.Classweb.ClasswebView" %>

<%@ Register src="../../myModel/Head.ascx" tagname="Head" tagprefix="uc1" %>
<%@ Register src="../../myModel/Foot.ascx" tagname="Foot" tagprefix="uc2" %>

<!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>
     <script language="javascript" type="text/javascript">
        function del() {
            return confirm("是否删除"); <!--js选择控件-->            
        }    
    </script>
    <link rel="Stylesheet" type="text/css" href="../../css/link.css" />
    <script type="text/javascript" language="javascript" src="../../javascript/onunload.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="0" cellpadding="0" cellspacing="0" width="781" align="center" ID="Table10">
            <!--网页头部的位置:开始-->
            <tr>
                <td align="center">
                    <!--网页头部的位置-->
                    <uc1:Head ID="Head1" runat="server" />
                </td>
            </tr>
            <!--网页头部的位置:结束-->

            <!--DataGrid的位置:开始-->
            <tr>
                <td align="center">
                    <!--DataGrid的位置-->
                    &nbsp;<asp:GridView ID="GridView1" runat="server" Width="100%" 
                        AllowPaging="True" AutoGenerateColumns="False" EnableViewState="False" 
                        onpageindexchanging="GridView1_PageIndexChanging" 
                        onselectedindexchanged="GridView1_SelectedIndexChanged" 
                        DataKeyNames="ClassID" onrowdeleting="Delete" onrowediting="Edit" 
                        onrowdatabound="GridView1_RowDataBound" 
                        onrowcommand="GridView1_RowCommand" onrowcreated="GridView1_RowCreated">
                        <Columns>
                            <asp:BoundField DataField="ClassName" HeaderText="班级名称" />
                             <asp:BoundField DataField="Remark" HeaderText="注释" />                             
                            <asp:TemplateField HeaderText="操作">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">编辑</asp:LinkButton>
                                &nbsp;&nbsp;
                                <asp:LinkButton ID="LinkButton2"  OnClientClick="del()" runat="server" CommandName="Delete">删除</asp:LinkButton>
                            </ItemTemplate>
                            </asp:TemplateField>                             
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <!--DataGrid的位置:结束-->
            <tr>
            <td align="right">

                <asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="注册" />

                <asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="AJAx" />
                <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="加密问题" />

                <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="注册" />

                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                    Text="DownList" />

                <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" Text="新增用户" />

            </td>
            </tr>

            <!--网页尾部的位置:开始-->
            <tr>
                <td align="center">
                    <!--网页尾部的位置-->
                    &nbsp;<uc2:Foot ID="Foot1" runat="server" />
                </td>
            </tr>
            <!--网页尾部的位置:结束-->
        </table>
    </div>
</form>
</body>
</html>

在最上面我写了一个js的脚本,将js内嵌在网页里面,就是一个删除的提示框
下面是一个连接了一个css样式。

BODY {
    PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 11pt; PADDING-BOTTOM: 0px; MARGIN: 0px; COLOR: #000000; PADDING-TOP: 0px; FONT-FAMILY: "Verdana", "宋体"; TEXT-DECORATION: none
}
TD {
    FONT-SIZE: 11pt; COLOR: #554e35; FONT-FAMILY: "Verdana", "宋体"; TEXT-DECORATION: none
}
A:link {
    FONT-SIZE: 11pt; COLOR: #000000; FONT-FAMILY: "Verdana", "宋体"; TEXT-DECORATION: none
}
A:visited {
    FONT-SIZE: 11pt; COLOR: #000000; FONT-FAMILY: "Verdana", "宋体"; TEXT-DECORATION: none
}
A:active {
    FONT-SIZE: 11pt; COLOR: #000000; FONT-FAMILY: "Verdana", "宋体"; TEXT-DECORATION: none
}
A:hover {
    FONT-SIZE: 11pt; COLOR: #cc0000; FONT-FAMILY: "Verdana", "宋体"; TEXT-DECORATION: none
}
BUTTON
{
    BORDER-RIGHT: #cccccc 1px solid; BORDER-TOP: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; COLOR: #ff3300; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 22px; BACKGROUND-COLOR: #efefef
}

然后就是后台的代码

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.Web.UI.WebControls;
using BusinessRules;

namespace WebSql.view.Classweb
{
    public partial class ClasswebView : WebSql.Mypublic.MyPage
    {

        int currentPage
        {
            get
            {
                return GridView1.PageIndex + 1;
            }
            set
            {
                GridView1.PageIndex = value - 1;
            }
        }
        int totalPageCount
        {
            get
            {
                return GridView1.PageCount;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           // this.Button5.Enabled = false;
           // this.Button5.PostBackUrl = "http://www.baidu.com";
            CheckLogin();//检查是否登录
            this.Button5.Visible = false;
            init();
        }
        void init()
        {
            ClassTable myClasstable = new ClassTable();
            DataTable dt = new DataTable();
            GridView1.Visible = true;
            GridView1.AllowPaging = true;
            GridView1.PagerSettings.Mode = PagerButtons.NextPrevious;
            GridView1.PagerSettings.NextPageText = "";
            GridView1.PageSize = myPageSize;
            GridView1.PagerSettings.PreviousPageText = "";
            GridView1.PagerSettings.Position = PagerPosition.TopAndBottom;
            GridView1.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
            try
            {
                dt = myClasstable.Query();

            }
            catch (Exception e)
            {
                string url = "../Error/404.aspx";
                Response.Redirect(url,true);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            //GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Delete(object sender, GridViewDeleteEventArgs e)
        {
            ClassTable myClassTable = new ClassTable();
            string myKey = GridView1.DataKeys[e.RowIndex].Value.ToString();//获取删除行的索引
            bool tishi = false;
            string IsDeleteResult = null;
            try
            {
                IsDeleteResult = myClassTable.IsDelete(myKey);//错误日志信息
                if (myClassTable.IsDelete(myKey) == null)
                {
                    tishi = myClassTable.Delete(myKey);//判断能否删除
                    if (tishi)
                    {
                        string myMessage = myKey + ":删除成功";
                        Response.Write("<script language=javascript> alert('" + myMessage + "') </script>");
                        init();
                    }
                    else
                    {
                        string myMessage = "删除失败,发送错误报告";
                        Response.Write("<script language=javascript> alert('" + myMessage + "')</script>");
                    }
                }
                else 
                {
                    string myMessage = "删除失败原因是:" + IsDeleteResult;
                    Response.Write("<script language=javascript> alert('" + myMessage + "')</script>"); 
                }
            }
            catch
            {
                string url = "../Error/404.aspx";
                Response.Redirect(url, true);
            }
        }

        protected void Edit(object sender, GridViewEditEventArgs e)
        {

                string myKey = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
                myKey = Server.UrlEncode(myKey).ToString();//进行编码,防止特殊符号的出现
                string url = "ClasswebEdit.aspx?myID=" + myKey;//网页用来传值的方法!问号传值,?名称 = 值!            
                Response.Redirect(url, true);


        }

        protected void btnadd_Click(object sender, EventArgs e)
        {
            string url = "ClasswebAdd.aspx";
                Response.Redirect(url, true);


        }

        protected void Button1_Click(object sender, EventArgs e)
        {

                string url = "../List/DownList.aspx";
                Response.Redirect(url, true);


        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //设置鼠标移动“行上”的颜色
                e.Row.Attributes.Add("onmouseover", "bgColor='#97CFF7'");
                //设置鼠标移动“行外”的颜色
                e.Row.Attributes.Add("onmouseout", "bgColor='#E9F1F3'");
                //设置每行的背景色
                e.Row.Attributes.Add("bgColor", Color);

            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

                string url = "../List/zhuce.aspx";
                Response.Redirect(url, true);

        }

        protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {

                if (e.Row.RowType == DataControlRowType.Pager)
                {
                    LinkButton lbtnFirstPage = new System.Web.UI.WebControls.LinkButton();//生成一个linkButton
                    lbtnFirstPage.EnableViewState = false;
                    lbtnFirstPage.Text = "首页";
                    lbtnFirstPage.ID = "lbtnFirstPage";
                    lbtnFirstPage.CommandName = "lbtnFirstPage";
                    if (this.currentPage != 1)
                    {
                        lbtnFirstPage.Enabled = true;
                    }
                    else
                    {
                        lbtnFirstPage.Enabled = false;
                    }

                    //上一页
                    LinkButton lbtnPreviousPage = new LinkButton();
                    lbtnPreviousPage.EnableViewState = false;
                    lbtnPreviousPage.Text = "上一页";
                    lbtnPreviousPage.ID = "lbtnPreviousPage";
                    lbtnPreviousPage.CommandName = "lbtnPreviousPage";
                    if (this.currentPage > 1)
                    {
                        lbtnPreviousPage.Enabled = true;
                    }
                    else
                    {
                        lbtnPreviousPage.Enabled = false;
                    }

                    //下一页
                    LinkButton lbtnNextPage = new LinkButton();
                    lbtnNextPage.EnableViewState = false;
                    lbtnNextPage.Text = "下一页";
                    lbtnNextPage.ID = "lbtnNextPage";
                    lbtnNextPage.CommandName = "lbtnNextPage";
                    if (this.currentPage < this.totalPageCount)
                    {
                        lbtnNextPage.Enabled = true;
                    }
                    else
                    {
                        lbtnNextPage.Enabled = false;
                    }

                    //末页
                    LinkButton lbtnLastPage = new LinkButton();
                    lbtnLastPage.EnableViewState = false;
                    lbtnLastPage.Text = "末页";
                    lbtnLastPage.ID = "lbtnLastPage";
                    lbtnLastPage.CommandName = "lbtnLastPage";
                    if (this.currentPage != this.totalPageCount)
                    {
                        lbtnLastPage.Enabled = true;
                    }
                    else
                    {
                        lbtnLastPage.Enabled = false;
                    }

                    Label lblFirstPage = new Label();
                    lblFirstPage.EnableViewState = false;
                    lblFirstPage.ID = "lblFirstPage";
                    lblFirstPage.Text = "&nbsp;";

                    //上一页后的文字
                    Label lblPreviousPage = new Label();
                    lblPreviousPage.EnableViewState = false;
                    lblPreviousPage.ID = "lblPreviousPage";
                    lblPreviousPage.Text = "&nbsp;&nbsp;";

                    //下一页后的文字
                    Label lblNextPage = new Label();
                    lblNextPage.EnableViewState = false;
                    lblNextPage.ID = "lblNextPage";
                    lblNextPage.Text = "&nbsp;&nbsp;";

                    //末页后的文字
                    Label lblLastPage = new Label();
                    lblLastPage.EnableViewState = false;
                    lblLastPage.ID = "lblLastPage";
                    lblLastPage.Text = "&nbsp;&nbsp;";

                    //当前第几页文字
                    Label lblCurrentPage = new Label();
                    lblCurrentPage.EnableViewState = false;
                    lblCurrentPage.ID = "lblCurrentPage";
                    int j = this.currentPage;
                    lblCurrentPage.Text = "当前第&nbsp;" + j.ToString() + "&nbsp;页&nbsp;&nbsp;跳转到&nbsp;";


                    //跳转的DropdownList
                    DropDownList drpJump = new DropDownList();
                    drpJump.EnableViewState = false;
                    drpJump.ID = "drpJump";


                    drpJump.AutoPostBack = true;
                    for (int i = 1; i < this.totalPageCount + 1; i++)
                    {
                        drpJump.Items.Add(i.ToString());
                    }
                    drpJump.SelectedIndex = this.currentPage - 1;

                    //注册SelectedIndexChanged事件对应的方法drpJump_SelectedIndexChanged

                    drpJump.SelectedIndexChanged += new EventHandler(drpJump_SelectedIndexChanged);

                    //总页数文字
                    Label lblTotalPageCount = new Label();
                    lblTotalPageCount.EnableViewState = false;
                    lblTotalPageCount.ID = "lblTotalPageCount";
                    lblTotalPageCount.Text = "&nbsp;页&nbsp;&nbsp;总页数&nbsp;" + this.totalPageCount;

                    //将上面的定义加入分页区域
                    e.Row.Cells[0].Controls.Add(lbtnFirstPage);
                    e.Row.Cells[0].Controls.Add(lblFirstPage);
                    e.Row.Cells[0].Controls.Add(lbtnPreviousPage);
                    e.Row.Cells[0].Controls.Add(lblPreviousPage);
                    e.Row.Cells[0].Controls.Add(lbtnNextPage);
                    e.Row.Cells[0].Controls.Add(lblNextPage);
                    e.Row.Cells[0].Controls.Add(lbtnLastPage);
                    e.Row.Cells[0].Controls.Add(lblLastPage);
                    e.Row.Cells[0].Controls.Add(lblCurrentPage);
                    e.Row.Cells[0].Controls.Add(drpJump);
                    e.Row.Cells[0].Controls.Add(lblTotalPageCount);
                }


        }

        void drpJump_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList drp = (DropDownList)sender;
            this.currentPage = drp.SelectedIndex + 1;
            init();

        }

        protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {

            if (e.CommandName == "lbtnFirstPage")
            {
                this.currentPage = 1;
                init();

            }
            if (e.CommandName == "lbtnPreviousPage")
            {
                if (this.currentPage != 1)
                {
                    this.currentPage = this.currentPage - 1;
                    init();
                }

            }
            if (e.CommandName == "lbtnNextPage")
            {
                if (this.currentPage < this.totalPageCount)
                {
                    this.currentPage = this.currentPage + 1;
                    init();
                }

            }
            if (e.CommandName == "lbtnLastPage")
            {
                this.currentPage = this.totalPageCount;
                init();

            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {

                string url = "../List/PasswordEdcode.aspx";
                Response.Redirect(url, true);




        }

        protected void Button4_Click(object sender, EventArgs e)
        {


                string url = "../List/PageAjax.aspx";
                Response.Redirect(url, true);


        }

        protected void Button5_Click(object sender, EventArgs e)
        {

            string url = "../List/Login.aspx";
            Response.Redirect(url, true);
        }
    }
}

在后台的代码里面有几个页面,404异常的页面,登录的页面,对数据的操作就是调用之前写好的方法!在pageloda里面会有一个检查你是否已经登录的方法,这个以后会慢慢说到!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值