进来看文章的各位先对你们说声对不起了,我是带着疑问来发表这篇文章的,标题可能说的还不够明确,因为我也不知道该用什么词语来形容。如果有更好的方法,请各个大大不吝赐教!
首先我想知道,在禁用ViewState的时候,只要存在<form id="form1" runat="server">的时候,就会存在ViewState,可是很多做的比较好的网站,首页都没有ViewState,就像我们的园子一样,首页连个ViewState的影子都看不到,是怎么实现的?
别和我说什么模板引擎,那完应不是很清楚,也不了解,我问过我朋友,他说只要不用控件,就不会有ViewState产生,比如GridView,DataList等等,可是不用这些东东的话,又要像Asp和Jsp一样,在页面中写乱七八糟的的代码了,维护起来超难受,可是用了吧生成的|ViewState更难受,长的吓人。。 我真不知道该怎么形容才好。
下面我就把代码贴出来,大家看看有什么好方法可以解决这个头疼的问题呢?
先来一个类,用于填充DataTable的
using
System.Data;
namespace Test
{
public class DataSource
{
public static DataTable GetList()
{
DataTable result = new DataTable();
result.Columns.Add( new DataColumn( " Text " , typeof ( string )));
result.Columns.Add( new DataColumn( " Url " , typeof ( string )));
DataRow objDR = result.NewRow();
objDR[ " Text " ] = " 红豆猥亵男童案下周将公布 " ;
objDR[ " Url " ] = " http://ent.163.com/11/0618/07/76QHOBR500034JAU.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 希腊为推动新紧缩政策重组内阁 " ;
objDR[ " Url " ] = " http://news.163.com/11/0618/02/76Q0OTS20001121M.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 学校为腾宿舍让大二学生提前毕业 " ;
objDR[ " Url " ] = " http://news.163.com/11/0618/10/76QV9OJ400014JB5.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 北京有望年内放松对高端项目的限购 " ;
objDR[ " Url " ] = " http://money.163.com/11/0617/19/76P92P0Q00253B0H.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 中国制造的山寨电子产品在中东蔓延 " ;
objDR[ " Url " ] = " http://discover.news.163.com/11/0618/11/76R0TT79000125LI.html " ;
result.Rows.Add(objDR);
return result;
}
}
}
namespace Test
{
public class DataSource
{
public static DataTable GetList()
{
DataTable result = new DataTable();
result.Columns.Add( new DataColumn( " Text " , typeof ( string )));
result.Columns.Add( new DataColumn( " Url " , typeof ( string )));
DataRow objDR = result.NewRow();
objDR[ " Text " ] = " 红豆猥亵男童案下周将公布 " ;
objDR[ " Url " ] = " http://ent.163.com/11/0618/07/76QHOBR500034JAU.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 希腊为推动新紧缩政策重组内阁 " ;
objDR[ " Url " ] = " http://news.163.com/11/0618/02/76Q0OTS20001121M.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 学校为腾宿舍让大二学生提前毕业 " ;
objDR[ " Url " ] = " http://news.163.com/11/0618/10/76QV9OJ400014JB5.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 北京有望年内放松对高端项目的限购 " ;
objDR[ " Url " ] = " http://money.163.com/11/0617/19/76P92P0Q00253B0H.html " ;
result.Rows.Add(objDR);
/**/
objDR = result.NewRow();
objDR[ " Text " ] = " 中国制造的山寨电子产品在中东蔓延 " ;
objDR[ " Url " ] = " http://discover.news.163.com/11/0618/11/76R0TT79000125LI.html " ;
result.Rows.Add(objDR);
return result;
}
}
}
先来一个有ViewState的代码:aspx页
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeBehind
=
"
ExistsViewstate.aspx.cs
"
Inherits = " Test.ExistsViewstate " EnableViewState = " false " %>
<! 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 >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:DataList ID ="DataList1" runat ="server" >
< HeaderTemplate >
< ul >
</ HeaderTemplate >
< ItemTemplate >
< li >< a href ='<%# Eval("Url")% > ' target="_blank">
<% # Eval ( " Text " ) %> > </ a ></ li >
</ ItemTemplate >
< FooterTemplate >
</ ul >
</ FooterTemplate >
</ asp:DataList >
</ form >
</ body >
</ html >
Inherits = " Test.ExistsViewstate " EnableViewState = " false " %>
<! 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 >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:DataList ID ="DataList1" runat ="server" >
< HeaderTemplate >
< ul >
</ HeaderTemplate >
< ItemTemplate >
< li >< a href ='<%# Eval("Url")% > ' target="_blank">
<% # Eval ( " Text " ) %> > </ a ></ li >
</ ItemTemplate >
< FooterTemplate >
</ ul >
</ FooterTemplate >
</ asp:DataList >
</ form >
</ body >
</ html >
后台代码:cs文件
using
System;
namespace Test
{
public partial class ExistsViewstate : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if ( ! this .IsPostBack)
{
this .DataList1.DataSource = DataSource.GetList();
this .DataList1.DataBind();
}
}
}
}
namespace Test
{
public partial class ExistsViewstate : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if ( ! this .IsPostBack)
{
this .DataList1.DataSource = DataSource.GetList();
this .DataList1.DataBind();
}
}
}
}
没有ViewState的代码:aspx页 需要导入两个命名空间; 这种写法的CS文件里没有任何多余的代码!
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeBehind
=
"
NoExistsViewstate.aspx.cs
"
Inherits = " Test.NoExistsViewstate " %>
<% @ Import Namespace = " System.Data " %>
<% @ Import Namespace = " Test " %>
<! 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 >
</ head >
< body >
< ul >
<%
foreach (DataRow objDR in DataSource.GetList().Rows)
{
Response.Write( String .Format( " <li><a href='{0}' target='_blank'>{1}</a></li> " , objDR[ " Url " ].ToString(), objDR[ " Text " ].ToString()));
}
%>
</ ul >
</ body >
</ html >
Inherits = " Test.NoExistsViewstate " %>
<% @ Import Namespace = " System.Data " %>
<% @ Import Namespace = " Test " %>
<! 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 >
</ head >
< body >
< ul >
<%
foreach (DataRow objDR in DataSource.GetList().Rows)
{
Response.Write( String .Format( " <li><a href='{0}' target='_blank'>{1}</a></li> " , objDR[ " Url " ].ToString(), objDR[ " Text " ].ToString()));
}
%>
</ ul >
</ body >
</ html >
小弟在此膜拜~ 求更好的写法,如果非得是用什么模板引擎,请提供一个案例,和“开源”易懂的,在此谢过……