.NET Web快速开发手册

来源:互联网 发布:淘宝订单可以拆分吗 编辑:程序博客网 时间:2024/04/27 19:50

.NET   Web快速开发手册

          花时间补了很多东西进去,可用于原形的快速开发,仅作个人备忘。 

1 母板页

母板页可以把网站总体风格和功能设计和特定网页的设计分隔开来。

1:母板页通常的命名

首先建立新母板页,在其中除了进行网页设计外,还可以写入如超链接,网站地图,甚至可以加入数据源,控件和事件响应,因为母板页虽然以master为后缀名,但实质上还是一个aspx页面。

 

2:母板页其实就是一个aspx页面

          母板页不同于其他页面的地方就在于其中可以加入ContentPlaceHolder,这只是个占位符似的控件,使用后,只要创建页面后使用该母板页为模版,就可以只设计开发ContentPlaceHolder中的内容而其余部分保持与母板页一致。

 

3:新创建的母板页都包含ContentPlaceHolder(其中紫色的框就是)

          母板页中还可以加入SiteMap,网站地图,实际上首先要在项目中加入一个叫SitemapXML

 

 

4:网站地图的XML

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

    <siteMapNode url="~/Default.aspx" title="Data Process"  description="">

        <siteMapNode url="~/Another.aspx" title="Another"  description="" />

        <siteMapNode url="~/Again.aspx" title="Again"  description="" />

    </siteMapNode>

</siteMap>

      Xml内容如上,语法很简单,节点(叶子,之下没有子节点的)用<siteMapNode url="~/***.aspx" title="XXX"  description="" /> 表示,如果其下还有子节点用<siteMapNode url="~/Default.aspx" title="Data Process"  description="">******</siteMapNode> 来包裹,其中还需要记录下网站各节点的相对路径。

在母板页中ContentPlaceHolder控件之外的地方加上一个TreeView或者其他可用于显示地图的控件,然后以XML作为数据源,便可在TreeView中显示网站结构,而且这可以在每一个网页中显示(因为用于母板页之外),更改时只要改那个XML就可以了。

 


2 GridView


以下以AccessDataSource为例,只要写少量代码,便实现一个增删查改的页面。

一、先建立解决方案,使用Access做数据库。


二、数据库表中设定字段



三、添加一个ASPX页面,注意输入文件名后要钩选使用MasterPage,然后就可以使用之前设计的MasterPage,在新建页面中,可以看到能设计的部分仅局限于ContentPlaceHolder中,其余部分与母板页相同。

在页面上拖入如下图的控件,作为Insert时信息输入框。

id是自增类型,time则自动获取时间,所以只需要录入titlecontent两个字段的内容,下面是一个Gridview控件,使用数据源AccessDataSource1在添加数据源时记得钩选自动生成SQL语句的选项,能够将增删查改的语句都生成,通过使用AccessDataSource只要在add按钮的事件里写入一行简单的代码,便实现了增删查改。如下图:

编辑和删除功能集成在gridview控件里,不再需要另外写代码了。
AccessDataSource
里要做的设置:

增删查改的四个sql语句都需要设置的,例如插入:

AccessDataSource的详细设置代码:

       <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/data.mdb"
            DeleteCommand
="DELETE FROM Test WHERE (id = ?)" InsertCommand="INSERT INTO Test (title, content, [time]) VALUES (?,?,DATE())"
            SelectCommand
="SELECT * FROM [Test]" UpdateCommand="UPDATE Test SET title = ?, content = ?, [time] = ? WHERE (id = ?)">
           
<InsertParameters>
               
<asp:ControlParameter ControlID="TextBox1" Name="title" PropertyName="Text" />
               
<asp:ControlParameter ControlID="TextBox2" Name="content" PropertyName="Text" />
           
</InsertParameters>
       
</asp:AccessDataSource>


当然,在GirdView中也需要相应的增加修改和删除的按钮,可以通过编辑列来添加CommandField来实现:

GridView的属性里,添加上图标出的两个CommandFieldGridView的代码如下:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
            DataSourceID
="AccessDataSource1">
           
<Columns>
               
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression
="id" />
               
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
               
<asp:BoundField DataField="content" HeaderText="content" SortExpression="content" />
               
<asp:BoundField DataField="time" HeaderText="time" SortExpression="time" />
               
<asp:CommandField ShowEditButton="True" />
               
<asp:CommandField ShowDeleteButton="True" />
           
</Columns>
       
</asp:GridView>


其余代码

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;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        AccessDataSource1.SelectCommand="SELECT * FROM [UserTable]";

 

        this.Button3.Attributes.Add("onclick", "return confirm('Are you sure?')");

        //给全删除加验证

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        //this.DetailsView1.PageIndex = this.GridView1.SelectedRow.DataItemIndex;

        this.DetailsView1.PageIndex = this.GridView1.SelectedRow.DataItemIndex;

        //TextBox1.Text = this.GridView1.SelectedRow.DataItemIndex.ToString();

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        AccessDataSource1.Insert();

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        AccessDataSource1.SelectCommand = "select * from [UserTable] where Name LIKE '%'+'" + TextBox3.Text.ToString() + "'+'%' AND Detail LIKE '%'+'"+TextBox4.Text.ToString()+"'+'%'";

    }

    protected void Button3_Click(object sender, EventArgs e)

    {

        //string sqlString = "DELETE FROM [UserTable] WHERE [ID] = ?";

 

        foreach (GridViewRow gr in GridView1.Rows)

        {

            CheckBox chk = (CheckBox)gr.Cells[4].FindControl("Check");

            if (chk.Checked==true)

            {

                //this.GridView1.DeleteRow();

 

                TextBox1.Text = Convert.ToInt32(gr.Cells[0].Text.ToString()).ToString();

                TextBox2.Text = gr.RowIndex.ToString();

 

                this.GridView1.DeleteRow(gr.RowIndex);

 

 

                //Response.Write("<script>alert("+gr.Cells[0].Text.ToString()+");<script>");

            }

        }

    }

 

 

    protected void Button4_Click(object sender, EventArgs e)

    {

        Button but=(Button)this.GridView1.HeaderRow.Cells[4].FindControl("Button4");

        //this.TextBox1.Text = "Click";

 

 

        if (but.Text.Equals("全选"))

        {    

            foreach (GridViewRow gr in this.GridView1.Rows)

            {

                CheckBox chk = (CheckBox)gr.Cells[4].FindControl("Check");

 

                if (chk.Checked == false)

                {

                    chk.Checked = true;

                }

                else

                {

                    chk.Checked = true;

                }

            }

            but.Text = "取消全选";

        }

        else if (but.Text.Equals("取消全选"))

        {

            foreach (GridViewRow gr in this.GridView1.Rows)

            {

                CheckBox chk = (CheckBox)gr.Cells[4].FindControl("Check");

 

                if (chk.Checked == true)

                {

                    chk.Checked = false;

                }

                else

                {

                    chk.Checked = false;

                }

            }

            but.Text = "全选";

        }

    }

}

 

3 IIS配置

安装前先找一张系统安装盘,注意版本一定要和当前系统一致,否则即使可以安装成功,IIS也会无法正常使用,比较保险的方法是先安装了IIS后再安装.NET Framework,如果是先装.NET Framework再装IIS,就必须在.netframework目录下找到aspnet_regiis,运行aspnet_regiis –i

装好后,可以在Web Sites下新建站点,然后将其路径指向Web项目存放目录,也可以就用默认目录然后将项目拷贝到该目录中,在Home Diretory中可以设定部分访问权限,一般只要Read 权限就可以了,然后在Home Diretory-Configuration-Options中的Enable parent paths选上。在Documents中加入**.aspx其中**代表入口页面名。接下来在ASP.NET中将版本改为2.0注意,3.03.5都是基于2.0。最后,再将存放数据库文件的文件夹设为可读写可更改,否则无法对数据进行修改,只能浏览。

原创粉丝点击