Archive

Archive for the ‘ASP.net’ Category

Asp.net中MasterPage的使用

June 4th, 2011 Tony hu No comments

1. 创建 MasterPage,后缀名 .master, 如 x.master.
其中用 <asp:ContentPlaceHolder /> 定义空位。如:

<asp:ContentPlaceHolder ID=”ContentPlaceHolder1″ Runat=”Server”>
</asp:ContentPlaceHolder>
2. 创建内容页面。
在 NewItem 对话框里选择 “select master page”, 选择上一步创建的 MasterPage.
产生的代码里, MasterPageFile 属性指定了 MasterPage 的位置:

<%@ Page Language=”VB” MasterPageFile=”~/x.master” Title=”无标题页面” %>

页面里用 <asp:Content /> 来添加内容到对应的空位:

<asp:Content ID=”Content1″ ContentPlaceHolderId=”ContentPlaceHolder1″ Runat=”Server”>
内容
</asp:Content/>

内容页面没有 <form id=”form1″ runat=”server”>
3. 利用 MasterPage 可以使用多种语言来编写一个页面的各个部分。
4. 除了在 <%@ Page %> 里面指定 MasterPage, 也可以在 web.config 指定:

<configuration>
<system.web>
<pages masterPageFile=”~/x.master” />
</system.web>
</configuration>

这样定义后,如果创建 Page 时选择了 master page, 则在 <%@ Page %> 里面不需要指定即可使用该 MasterPage.
其他页面要使用不同的 MasterPage 的话,只要用第一种方法在 Page directive 里面明确的覆盖 web.config 里的设置即可。

可以仅对一组 pages 指定 MasterPage. 下例利用 web.config 的 location 元素,设定了 Admin 目录下的页面采用的不同的 MasterPage.

<configuration>
<location path=”Admin”>
<system.web>
<pages masterPageFile=”~/y.master” />
</system.web>
</location>
</configuration>
5. 在内容页面如何设定 Page 的 Title ?

默认情况下,Title 在 MasterPage 中指定后,其他具体页面就都使用这个 Title.
在具体页面,可以有两个办法修改 Title:

a. <%@ Page Title=”test” %>

b. 代码中:

protected void Page_LoadComplete(object sender, EventArgs e)
{
Master.Page.Title = “Hello”;
}
6. 访问 MasterPage 中的属性和控件。

用 Master 属性来访问。

a. 假设 MasterPage 中有一个 Label1, 那么在内容页面可以这样:

protected void Page_LoadComplete(object sender, EventArgs e)
{
string text = (Master.FindControl(“Label1″) as Label).Text;
}

页面加载的次序:

要获取在 MasterPage 的 Page_Load 里面设定的值,必须在内容页面的 Page_LoadComplete 中来写。

前面提到的 FindControl() 方法来查找 MasterPage 中的控件,是一种后期绑定的做法,一般是不安全的。因为这取决于 MasterPage 中是否存在这个 tag,如果被删除了,则会导致错误。
比较好的做法是,在 MasterPage 中用属性封装对他的控件的访问;如果用 FindControl(), 则总是检查其结果是否为 null.
7. 指定 MasterPage 中的默认内容

直接在 <asp:ControlPlaceHolder /> 标签之间指定即可。
如果子页面不重新指定,则会采用该默认内容。
8. 编程的方式指定 Master Page

protected void Page_PreInit(object sender, EventArgs e)
{
Page.MasterPageFile = “~/x.master”;
}
9. 嵌套的 Master Page

Master Page 可以继承自更高层次的 Master Page. 但是在 VS2005 中创建这种子 Master Page 的时候,不会有默认的支持。
假设有了一个 A.master,
我们现在先创建一个普通的 B.master,
然后删除其中除了 Page directive 的其他部分。
把 Page Directive 修改为如下,并加入自己要定义的 PlaceHolder:

<%@ Master MasterPageFile=”~/A.master” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”server”>
Hello!
<asp:ContentPlaceHolder ID=”ContentPlaceHolder2″ Runat=”server”>
</asp:ContentPlaceHolder>
</asp:Content>

用嵌套的模板产生的子页面将不能采用 VS2005 的 design 模式。
10. 容器特定的 Master Pages

为了能兼容不同的浏览器,asp.net 2.0 支持多个 Master Page. 在运行时将自动加载合适的 Master Page.

语法如下:

<%@ Page Language=”VB” MasterPageFile=”~/Abc.master”
Mozilla:MasterPageFile=”~/AbcMozilla.master”
Opera:MasterPageFile=”~/AbcMozilla.master” %>
11. 页面请求的次序

当用户请求一个用 Master Page 构建的页面时,各种事件发生的次序如下:

Master Page 子控件初始化;
内容页面子控件初始化;
Master Page 初始化;
内容页面初始化;
内容页面 Page_Load;
Master Page 的 Page_Load;
Master Page 子控件加载;
内容页面子控件加载;
注意点:

因为内容页面的 Page_Load 先于 Master Page 的 Page_Load,所以,如果要访问 Master Page 里的服务器控件,则必须在内容页面的 Page_LoadComplete 方法里书写代码。
12. 使用缓存

只有在内容页面才可以使用如下的 directive 指定缓存:

<%@ OutputCache Duration=”10″ Varybyparam=”None” %>

(这个指令让服务器在内存里缓存该页面 10 秒钟)

如果对 Master Page 指定该指令,本身并不会引发错误。但是当他的子页面下一次来获取其 Master Page 的时候,如果这时 Master Page 已经过期,则会引发一个错误。
所以实际上只能对子页面指定缓存。

Categories: ASP.net Tags:

Asp.net中使用Cookie时用中文汉字引起的乱码

December 3rd, 2010 Tony hu 12 comments

在写入cookie前,将内容用HttpUtility.UrlEncode进行处理,读取后,再用HttpUtility.UrlDecode进行还原

Categories: ASP.net, Web Tags:

GridView 排序 分页 编辑 删除 更新 模板 综合使用

June 13th, 2010 Tony hu No comments

这是我最近在做一个项目,我尽量全部手写代码。现在将CS文件贴分来分享一下,作一个备忘吧!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Capsugel.Web
{
public partial class Customers : System.Web.UI.Page
{
//使用DataView,排序的时候使用到
DataView dv = new DataView();

protected void Page_Load(object sender, EventArgs e)
{
//第一次加载页面
if (!IsPostBack)
{
bind();
}
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//测试 Ajax
//System.Threading.Thread.Sleep(1000);
GridView1.PageIndex = e.NewPageIndex;
//重新绑定一次
bind();
}

///
/// 绑定GridView
///
void bind()
{
Capsugel.BLL.Customer bll = new BLL.Customer();
//根据TextBox中的关键词,显示数据
DataTable dt = bll.GetList_ByName(TextBox1.Text.Trim()).Tables[0];
if (ViewState["sortExpr"] != null)
{
dv = new DataView(dt);
dv.Sort = (string)ViewState["sortExpr"] + ” ” + ViewState["sortingOrder"].ToString();
}
else
{
dv = dt.DefaultView;
}
GridView1.DataSource = dv;
GridView1.DataBind();
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int idx = 0;
string Uid = “”;
BLL.Customer bll = new BLL.Customer();

if (e.CommandName == “myedit”)
{
//得到当前行的索引号,确定要将哪一行开启编辑状态,
//如果是第二行的第一个,那应该是 0,所以才有了下面的公式
idx = Convert.ToInt32(e.CommandArgument)-GridView1.PageIndex * GridView1.PageSize;
GridView1.EditIndex = idx;
}
else if (e.CommandName == “myupdate”)
{
idx = Convert.ToInt32(e.CommandArgument) – GridView1.PageIndex * GridView1.PageSize;
Uid = GridView1.DataKeys[idx].Values[0].ToString();
//得到编辑框中的值
string uname = ((TextBox)GridView1.Rows[idx].FindControl(“TextBox1″)).Text;
string custid = ((TextBox)GridView1.Rows[idx].FindControl(“TextBox2″)).Text;
string userid = ((TextBox)GridView1.Rows[idx].FindControl(“TextBox3″)).Text;
bool check = ((CheckBox)GridView1.Rows[idx].FindControl(“CheckBox1″)).Checked;
//对像模型
Model.Customer m = new Model.Customer();
m.CID = Convert.ToInt32(Uid);
m.Checked = check;
m.Name = uname;
m.CustID = custid;
m.Uid = userid;
//更新
bll.Update(m);
//取消编辑状态
GridView1.EditIndex = -1;
}
else if (e.CommandName == “mydelete”)
{
idx = Convert.ToInt32(e.CommandArgument) – GridView1.PageIndex * GridView1.PageSize;
Uid = GridView1.DataKeys[idx].Values[0].ToString();
bll.Delete(Convert.ToInt32(Uid));
}
else if (e.CommandName == “mycancel”)
{
GridView1.EditIndex = -1;
}
//最后都要进行重新绑定一次
bind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断是否是DataRow,以防止鼠标经过Header也有效果
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add(“onmouseover”, “e=this.style.backgroundColor;this.style.backgroundColor=’#BDF9AC’;”);
e.Row.Attributes.Add(“onmouseout”, “this.style.backgroundColor=e;”);
}
}

protected void Button3_Click(object sender, EventArgs e)
{
//POSTBACK
bind();
}

///
/// 处理排序的问题,定义的排序
///
///
///
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//点一次时是desc,第二次是asc
if (ViewState["sortingOrder"] == null)
ViewState["sortingOrder"] = “desc”;
else if (Convert.ToString(ViewState["sortingOrder"]) == “asc”)
ViewState["sortingOrder"] = “desc”;
else if (Convert.ToString(ViewState["sortingOrder"]) == “desc”)
ViewState["sortingOrder"] = “asc”;

ViewState["sortExpr"] = e.SortExpression;
bind();
}

///
/// 显示所有的,只要清空一下TextBox中的值
///
///
///
protected void Button4_Click(object sender, EventArgs e)
{
TextBox1.Text = “”;
bind();
}
}
}

Categories: ASP.net, Web Tags: , , , ,

ASP.NET 4.0 新特性(不完全版)

June 11th, 2010 Tony hu 2 comments
  • 图表控件
  • SEO优化支持
  • URL Routing
  • Web.Config Transformation

if (!Page.IsPostBack)
{
Page.Title = “在这里可以设置页面的Title”;
Page.MetaDescription = “在这里可以设置页面的Description”;
Page.MetaKeywords = “在这里可以设置页面的keywords,做SEO”;
}

ASP.net 中,自定义、填充 DataTable,GridView 显示

December 30th, 2009 Tony hu 3 comments

void bind()
{
DateTime dt1 = Convert.ToDateTime(this.DropDownList1.SelectedValue + “-” + this.DropDownList2.SelectedValue + “-1″);
DateTime dt2 = Convert.ToDateTime(this.DropDownList3.SelectedValue + “-” + this.DropDownList4.SelectedValue + “-1″);
int i = 0;
int j = 0;
j = Convert.ToInt32(DB.GetFirstValue(“Select Datediff(m,’” + dt1 + “‘,’” + dt2 + “‘) AS J;”, DB.connectionString)) + 1;
DataTable dt;
‘定义每一列的属性
DataTable Dt = new DataTable(“GetSummary”);
Dt.Columns.Add(“Date”, Type.GetType(“System.String”));
Dt.Columns.Add(“SORTING100″, Type.GetType(“System.Int32″));
Dt.Columns.Add(“HCMNOBYPASS”, Type.GetType(“System.Int32″));
Dt.Columns.Add(“PCMNOBYPASS”, Type.GetType(“System.Int32″));
Dt.Columns.Add(“REWORK”, Type.GetType(“System.Int32″));
Dt.Columns.Add(“VCAPS”, Type.GetType(“System.Int32″));
Dt.Columns.Add(“SUM”, Type.GetType(“System.Int32″));
Dt.Columns.Add(“Total”, Type.GetType(“System.Int32″));
Dt.Columns.Add(“Percent”, Type.GetType(“System.String”));
Dt.Columns.Add(“HCMNOBYPASSCC”, Type.GetType(“System.Int32″));
Dt.Columns.Add(“PCMNOBYPASSCC”, Type.GetType(“System.Int32″));
‘填充
for (i = 0; i < j; i++)
{
string date = dt1.AddMonths(i).ToShortDateString();
string tsql = “SELECT Count(f_LC_LotUID) AS Total FROM [t_Rel_LotCar] where Datediff(m,f_LC_ProdCalDate,’” + date + “‘)=0 and f_LC_CarStatus=’ST01/OK’”;

dt = NoPrintClass.GetGetSummary(date);
‘将从数据库中得到的表的各个列的值,填充到我们定义的变量Dt中,注意这里的 Dt 和 dt 是不一样的。这里是C#程序
Dt.Rows.Add(new object[] { dt.Rows[0][0], dt.Rows[0][1], dt.Rows[0][2], dt.Rows[0][3], dt.Rows[0][4], dt.Rows[0][5], dt.Rows[0][8], Convert.ToInt32(DB.GetFirstValue(tsql, DB.connWIP)), ‘2′, dt.Rows[0][6], dt.Rows[0][7] });
GridView1.DataSource = Dt;
GridView1.DataBind();
}
}

时间较紧,没有写注释,业内人应该都可以看懂