作者归档 孟繁永

通过孟繁永

DataGrid的自动编号问题

序号 内容
1 Taye
2 BOx
3 Glass
4 StarCraf

一、正序

A、AllowPaging=False情况下

<asp:DataGrid id=”DataGrid1″ runat=”server”>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# Container.ItemIndex + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

就可以实现

不过更有趣的方法是使用这个方法

<asp:DataGrid id=”DataGrid1″ runat=”server”>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# this.DataGrid1.Items.Count + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

也许有些人会觉得很奇怪为什么Items.Count会这样,而不是出来全部总合..但如果你了解绑定的过程时就容易理解.
[从上面来看就是在ItemCreated事件中进行绑定所以得到的Items.Count刚好是当前的序号]

B、AllowPaging=”True”下

如果你DataGrid支持分页则可以如下

<asp:DataGrid id=”DataGrid1″ runat=”server” AllowPaging=”True”>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# this.DataGrid1.CurrentPageIndex * this.DataGrid1.PageSize + Container.ItemIndex + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

二、倒序的方法

序号 内容
4 Taye
3 BOx
2 Glass
1 StarCraft

由上面可以知道使用
this.DataGrid1.Items.Count – Container.ItemIndex + 1方法是不可能实现的,得到值而且全会为1
分页的情况下更是一样.所以一开始我们就要取得数据源的行数

.cs

private int rowscount = 0;
protected int RowsCount
{
get{ return rowscount;}
set{ this.rowscount = value; }
}

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
this.BindData();
}

private void BindData()
{
SqlConnection cn = new SqlConnection(“server=(local);database=NorthWind;uid=sa;pwd=”);
string str=@”SELECT Employees.EmployeeID, Orders.EmployeeID
FROM Employees INNER JOIN
Orders ON Employees.EmployeeID = Orders.EmployeeID “;

SqlDataAdapter sqlda = new SqlDataAdapter(str,cn);
DataSet ds = new DataSet();

sqlda.Fill(ds);

this.RowsCount = ds.Tables[0].Rows.Count;

this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();

}

.aspx

<asp:DataGrid id=”DataGrid1″ runat=”server” AllowPaging=”True”>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# RowsCount – DataGrid1.CurrentPageIndex * DataGrid1.PageSize – Container.ItemIndex %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

当然如果是不是分页的情况一下更容易实现了.

转自:http://blog.csdn.net/mike1983/archive/2006/06/27/841725.aspx

通过孟繁永

续上篇

4.\App_GlobalResources Folder

资源文件(.resx) 是一个在你的应用程序中依据不同文化来改变页面内容的可以作为数据字典的字串表。除字串外,还可添加image等其它文件。
例如添加两个资源文件到此文件夹:
第一个资源文件是Resource.resx这是默认语言使用英语。
  Name     Value
  Answer     Hello there
  PageTitle    Sample Page
  Question    What is your name?
第二个资源文件是Resource.zh-cn.resx)使用中文。
  Name     Value
  Answer     你好
  PageTitle    示例页面
  Question     你的名字叫什么?

Listing 3-18: A simple ASP.NET page that uses resource files
VB

<%@ Page Language=”VB” Culture=Auto” UICulture=Auto” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http:
//www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
Page.Title 
= Resources.Resource.PageTitle
End Sub

Protected Sub Button1_Click()Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text 
= Resources.Resource.Answer & “ “ & Textbox1.Text
End Sub

</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title></title>
</head>
<body>
<form id=”Form1” runat=”server”>
<p><%= Resources.Resource.Question %></p><br />
<asp:TextBox ID=”Textbox1” Runat=”server”></asp:TextBox><br />
<asp:Button ID=”Button1” Runat=”server” Text=”Submit”
OnClick
=”Button1_Click” />
<p><asp:Label ID=”Label1” Runat=”server”></asp:Label></p>
</form>
</body>
</html>
c#

<%@ Page Language=”C#” Culture=”Auto” UICulture=”Auto” %>
<!DOCTYPE html PUBLIC “//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
protected void Page_Load(object sender, System.EventArgs e)
{
Page.Title 
= Resources.Resource.PageTitle;
}

protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text 
= Resources.Resource.Answer + “ “ + Textbox1.Text;
}

</script>

当这个程序运行时,会根据浏览器语言设定而选择使用不同的资源文件。如果语言设定为中文则会显示中文,否则为显示默认英文。

5.\App_LocalResources Folder
你也可以把资源文件添加到\App_LocalResources文件夹,只不过\App_GlobalResources文件夹是应用程序级别,而\App_LocalResources文件夹是页面级别。

6.\App_WebReferences Folder
你可以使用\App_WebReferences文件夹自动访在你的应用程序中引用的远程Web services。

7.\App_Browsers Folder
存贮在 \App_Browsers文件夹中的.browser文件,你也可以在\Windows\Microsoft.NET\Framework\v2.0xxxxx\
CONFIG\Browsers文件夹中看得到它,它主要是用来判断浏览器的兼容性的。

转自:http://www.cnblogs.com/zhhui/archive/2006/03/21/354545.html

通过孟繁永

Professional ASP.NET 2.0之应用程序文件夹-Application Folders

1.\App_Code Folder
  \App_Code文件夹是存贮classes,.wsdl文件和typed datasets的地方。你的解决方案中的所有页面可以自动访问存贮在这个文件夹的任何一个项目。如果这些项目是一个class (.vb or .cs),则Visual Studio 2005会自动检测并编译它;也会自动地创建源于.wsdl文件的XML Web service proxy class;或者一个源于.xsd文件的一个typed dataset。
  下面看一个在你解决方案中使用\App_Code文件夹的简单class的例子:
首先在\App_Code文件夹中建立一个Calculator.vb 或 Calculator.cs文件,如下所示:

Listing 3-14: The Calculator class
VB

Imports Microsoft.VisualBasic
Public Class CalculatorClass Calculator
Public Function Add()Function Add(ByVal a As IntegerByVal b As IntegerAs Integer
Return (a + b)
End Function

End Class
C#

using System;
public class Calculator
{
public int Add(int a, int b)
{
return (a + b);
}

}
其次是对这个class的使用,看下面的一个简单的.aspx文件:
Listing 3-15: An .aspx page that uses the Calculator class
VB

<%@ Page Language=”VB” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http:
//www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
Protected Sub Page_Load()Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs)
Dim myCalc As New Calculator
Label1.Text 
= myCalc.Add(1212)
End Sub

</script>
c#

<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
protected void Page_Load(object sender, System.EventArgs e)
{
Calculator myCalc 
= new Calculator();
Label1.Text 
= myCalc.Add(1212).ToString();
}

</script>
那么如何在\App_Code文件夹使用两个不同语言的class呢?
首先在\App_Code文件夹中添加两个子文件夹:一个 \VB 文件夹和一个 \CS 文件夹。
\App_Code
  \VB
    Add.vb
  \CS
    Subtract.cs
其次修改web.config文件如下:
Listing 3-17: Structuring the web.config file so that classes in the \App_Code folder can use different languages
<compilation>
  <codeSubDirectories>
    <add directoryName=”VB”></add>
    <add directoryName=”CS”></add>
  </codeSubDirectories>
</compilation>

2.\App_Data Folder
\App_Data文件夹是应用程序存贮数据的地方,可以包括Microsoft SQL Express 文件(.mdf files), Microsoft Access 文件(.mdb files), XML 文件等。

3.\App_Themes Folder

\App_Themes文件夹是存贮asp.net 2.0新特性主题需要使用的 .skin 文件, CSS文件和images文件的地方。

通过孟繁永

asp.net中使用基于角色role的Forms验证

asp.net中使用基于角色role的Forms验证,大致经过几下四步:
1.配置系统web.config

<system.web> 
<authentication mode=”Forms” > 
 
<forms name=”.yaoCookies” loginUrl=”/duan/Manage/login.aspx” protection=”All”
  timeout
=”20″ path=”/” />
 
</authentication>
</system.web>

其中<authentication mode= “forms”> 表示本应用程序采用Forms验证方式。
1). <forms>标签中的name表示指定要用于身份验证的 HTTP Cookie。默认情况下,name 的值是 .ASPXAUTH。采用此种方式验证用户后,以此用户的信息建立一个FormsAuthenticationTicket类型的身份验证票,再加密序列化为一个字符串,最后将这个字符串写到客户端的name指定名字的Cookie中.一旦这个Cookie写到客户端后,此用户再次访问这个web应用时会将连同Cookie一起发送到服务端,服务端将会知道此用户是已经验证过的.

2). <forms>标签中的loginUrl指定如果没有找到任何有效的身份验证 Cookie,为登录将请求重定向到的 URL。默认值为 default.aspx。loginUrl指定的页面就是用来验证用户身份的,一般此页面提供用户输入用户名和密码,用户提交后由程序来根据自己的需要来验证用户的合法性(大多情况是将用户输入信息同数据库中的用户表进行比较),如果验证用户有效,则生成同此用户对应的身份验证票,写到客户端的Cookie,最后将浏览器重定向到用户初试请求的页面.一般是用FormsAuthentication.RedirectFromLoginPage 方法来完成生成身份验证票,写回客户端,浏览器重定向等一系列的动作.

public static void RedirectFromLoginPage( string userName, bool createPersistentCookie, string strCookiePath );

其中:
userName: 就是此用户的标示,用来标志此用户的唯一标示,不一定要映射到用户账户名称.
createPersistentCookie: 标示是否发出持久的 Cookie。
若不是持久Cookie,Cookie的有效期Expiration属性有当前时间加上web.config中timeout的时间,每次请求页面时,在验证身份过程中,会判断是否过了有效期的一半,要是的话更新一次cookie的有效期;若是持久cookie,Expiration属性无意义,这时身份验证票的有效期有cookie的Expires决定,RedirectFromLoginPage方法给Expires属性设定的是50年有效期。
strCookiePath: 标示将生成的Cookie的写到客户端的路径,身份验证票中保存这个路径是在刷新身份验证票Cookie时使用(这也是生成Cookie的Path),若没有strCookiePath 参数,则使用web.config中 path属性的设置。

这里可以看到,此方法参数只有三个,而身份验证票的属性有七个,不足的四个参数是这么来的:
IssueDate:Cookie发出时间由当前时间得出,
Expiration:过期时间由当前时间和<forms>标签中的timeout参数算出。此参数对非持久性cookie有意义。
UserData:这个属性可以用应用程序写入一些用户定义的数据,此方法没有用到这个属性,只是简单的将此属性置为空字符串,请注意此属性,在后面我们将要使用到这个属性。
Version: 版本号由系统自动提供。

RedirectFromLoginPage方法生成生成身份验证票后,会调用FormsAuthentication.Encrypt 方法,将身份验证票加密为字符串,这个字符串将会是以.ASPXAUTH为名字的一个Cookie的值。
这个Cookie的其它属性的生成:
Domain,Path属性为确省值,Expires视createPersistentCookie参数而定,若是持久cookie,Expires设为50年以后过期;若是非持久cookie,Expires属性不设置。
生成身份验证Cookie后,将此Cookie加入到Response.Cookies中,等待发送到客户端。
最后RedirectFromLoginPage方法调用FormsAuthentication.GetRedirectUrl 方法获取到用户原先请求的页面,重定向到这个页面。

3). <forms>标签中的timeout和path,是提供了身份验证票写入到Cookie过期时间和默认路径。

以上就是基于Forms身份验证的过程,它完成了对用户身份的确认。

2.在受保护的文件夹如Manage下创建一web.config文件,内容如

<configuration>
  <!–指定对整个Manage目录的访问权限–>
  
<system.web>
    
<authorization>
           <!–多个角色用,分隔–>
          
<allow roles=”admin,user”/>
           
<deny users=”*” />
      
</authorization>
  
</system.web>

  
<!–也可控制某个页的权限

  <location path=”AnnounceList.aspx”>
     <system.web>
        <authorization>
           <allow roles=”admin”/>
           <deny users=”*” />
        </authorization>
     </system.web>
  </location>

  <location path=”ConfigInfo.aspx”>
     <system.web>
        <authorization>
           <allow roles=”users”/>
           <deny users=”*” />
        </authorization>
     </system.web>
  </location>

  –>
</configuration>

注:此配置内容也可以加入到系统的web.config文件中,注意加入位置:

……..
    </system.web>

    <location path=”Manage/AnnounceList.aspx”>
    
<system.web>
     
<authorization>
      
<allow roles=”admin”/>
      
<deny users=”*” />
     
</authorization>
     
</system.web>
    </location>

</configuration>

<allow>标签表示允许访问,其中的属性
1). users:一个逗号分隔的用户名列表,这些用户名已被授予对资源的访问权限。问号 (?) 允许匿名用户;星号 (*) 允许所有用户。
2). roles:一个逗号分隔的角色列表,这些角色已被授予对资源的访问权限。
3). verbs:一个逗号分隔的 HTTP 传输方法列表,这些 HTTP 传输方法已被授予对资源的访问权限。注册到 ASP.NET 的谓词为 GET、HEAD、POST 和 DEBUG。

<deny>标签表示不允许访问。其中的属性同上面的。

在运行时,授权模块迭代通过 <allow> 和 <deny> 标记,直到它找到适合特定用户的第一个访问规则。然后,它根据找到的第一项访问规则是 <allow> 还是 <deny> 规则来允许或拒绝对 URL 资源的访问。Machine.config 文件中的默认身份验证规则是 <allow users=”*”/>,因此除非另行配置,否则在默认情况下会允许访问。

那么这些user 和roles又是如何得到的呢?下面看一下授权的详细过程:

1). 一旦一个用户访问这个网站,就行登录确认了身份,身份验证票的cookie也写到了客户端。之后,这个用户再次申请这个web的页面,身份验证票的cookie就会发送到服务端。在服务端,asp.net为每一个http请求都分配一个HttpApplication对象来处理这个请求,在HttpApplication.AuthenticateRequest事件后,安全模块已建立用户标识,就是此用户的身份在web端已经建立起来,这个身份完全是由客户端发送回来的身份验证票的cookie建立的。
2). 用户身份在HttpContext.User 属性中,在页面中可以通过Page.Context 来获取同这个页面相关的HttpContext对象。对于Forms验证,HttpContext.User属性是一个GenericPrincipal类型的对象,GenericPrincipal只有一个公开的属性Identity,有个私有的m_role属性,是string[]类型,存放此用户是属于哪些role的数组,还有一个公开的方法IsInRole(string role),来判断此用户是否属于某个角色。
由于身份验证票的cookie中根本没有提供role这个属性,就是说Forms身份验证票没有提供此用户的role信息,所以,对于Forms验证,在服务端得到的GenericPrincipal 用户对象的m_role属性永远是空的。
3). GenericPrincipal. Identity 属性是一个FormsIdentity类型的对象,这个对象有个Name属性,就是此用户的标示,访问授权就是将此属性做为user来进行授权验证的。FormsIdentity还有一个属性,就是Ticket属性,此属性是身份验证票FormsAuthenticationTicket类型,就是之前服务器写到客户端的身份验证票。
服务器在获取到身份验证票FormsAuthenticationTicket对象后,查看这个身份验证票是不是非持久的身份验证,是的话要根据web.config中timeout属性设置的有效期来更新这个身份验证票的cookie(为避免危及性能,在经过了超过一半的指定时间后更新该 Cookie。这可能导致精确性上的损失。持久性 Cookie 不超时。)
4). 在HttpApplication.ResolveRequestCache事件之前,asp.net开始取得用户请求的页面,建立HttpHandler控制点。这就意味着,在HttpApplication.ResolveRequestCache事件要对用户访问权限就行验证,看此用户或角色是否有权限访问这个页面,之后在这个请求的生命周期内再改变此用户的身份或角色就没有意义了。

以上是Forms验证的全过程,可以看出,这个Forms验证是基于用户的,没有为角色的验证提供直接支持。身份验证票FormsAuthenticationTicket 中的Name属性是用户标示,其实还有一个属性UserData,这个属性可以由应用程序来写入自定义的一些数据,我们可以利用这个字段来存放role的信息,从而达到基于角色验证的目的。

3.登录页

//登录按钮
private void Button1_Click(object sender, System.EventArgs e)
{
            //实体类AdminUserVO对应AdminUser用户表。
            AdminUserVO adminUserVO 
= new AdminUserVO();

            adminUserVO.Uname = UserName.Text.Trim();
            adminUserVO.Upwd 
= UserPwd.Text.Trim();
            adminUserVO.LastIP 
= HttpContext.Current.Request.UserHostAddress;
            adminUserVO.LastTime 
= DateTime.Now;

            bool flag = (new LoginDAO()).Chk(adminUserVO);

            if (flag)
            {
                
//非角色验证时可以用这句:
                
//System.Web.Security.FormsAuthentication.SetAuthCookie(UserName.Text.Trim(),false);

                //创建角色验证信息,把role信息写入到UserData中
                SetLoginCookie(adminUserVO,adminUserVO.Roles.ToLower());

                HttpContext.Current.Response.Redirect(
Main.aspx);
            }
            
else
            {
                HttpContext.Current.Response.Write(
登录失败);
            }
}

//SetLoginCookie方法
public static void SetLoginCookie(AdminUserVO u, string roles)
  {
   
//建立身份验证票对象
   FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1,u.Uname, DateTime.Now, DateTime.Now.AddMinutes(30), false,roles,/);
   
//加密序列化验证票为字符串
   string hashTicket = FormsAuthentication.Encrypt (ticket) ;
   HttpCookie userCookie 
= new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
   HttpContext.Current.Response.Cookies.Add(userCookie);
  }

FormsAuthenticationTicket参数说明:
FormsAuthenticationTicket(
int version, //设为1,版本号由系统自动提供
string name, //用户标示,获取与身份验证 Cookie 关联的用户名
DateTime issueDate, //Cookie 的发出时间, 设置为 DateTime.Now
DateTime expiration, //获取 Cookie 过期的日期/时间
bool isPersistent, //是否持久性(根据需要设置,若是设置为持久性,在发出cookie时,cookie的Expires设置一定要设置),如果已发出持久的 Cookie,则返回 true。否则,身份验证 Cookie 将限制在浏览器生命周期范围内。
string userData, //获取存储在 Cookie 中的应用程序定义字符串,这里用上面准备好的用逗号分割的role字符串
string cookiePath // 返回发出 Cookie 的路径。注意,窗体的路径设置为”/”,这要同发出cookie的路径一致,因为刷新cookie要用这个路径。由于窗体区分大小写,这是为了防止站点中的 URL 的大小写不一致而采取的一种保护措施。
);

4.Global.asax.cs

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {
   HttpApplication app 
= (HttpApplication) sender;  
   HttpContext ctx 
= app.Context ; //获取本次Http请求的HttpContext对象  
   if (ctx.User != null)
   {
    
if (ctx.Request.IsAuthenticated == true//验证过的一般用户才能进行角色验证  
    {  
     System.Web.Security.FormsIdentity fi 
= (System.Web.Security.FormsIdentity)ctx.User.Identity ;  
     System.Web.Security.FormsAuthenticationTicket ticket 
= fi.Ticket ; //取得身份验证票  
     string userData = ticket.UserData;//从UserData中恢复role信息
     
string[] roles = userData.Split (,) ; //将角色数据转成字符串数组,得到相关的角色信息  
     ctx.User 
= new System.Security.Principal.GenericPrincipal (fi, roles) ; //这样当前用户就拥有角色信息了
    } 
   }
  }

注:如果使用HttpModule的话,此处代码应该加入在AuthenticateRequest事件中。

转自:http://www.cnblogs.com/yao/archive/2006/06/24/434783.html

通过孟繁永

用户控件中引用图片时的路径设置——相对路径

我把一堆用户控件放在一个子目录中,但当这些控件放在页面上时,它们之内引用的图片却依然是相对于这个控件所在目录的路径出现,所以不得不加上~来从根目录往下找。

通过孟繁永

用CSS+div控制字符串显示长度

<DIV   STYLE=”width:200px;   height:50px;   border:0px   solid   red;  overflow:hidden;   text-overflow:ellipsis”><NOBR>测试   test   测试   test   测试   test   测试   test   测试</NOBR></DIV>
通过孟繁永

日程安排提前五分钟提醒

日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff(‘minute’,f开始时间,getdate())>5
通过孟繁永

ASP.NET视图状态验证MAC失败

 “/”应用程序中的服务器错误。


验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Web.HttpException: 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。

源错误:

只有在调试模式下进行编译时,生成此未处理异常的源代码才会显示出来。若要启用此功能,请执行以下步骤之一,然后请求 URL:

1. 在产生错误的文件的顶部添加一条“Debug=true”指令。例如:

  <%@ Page Language="C#" Debug="true" %>

或:

2. 将以下的节添加到应用程序的配置文件中:

<configuration>
   <system.web>
       <compilation debug="true"/>
   </system.web>
</configuration>

请注意,第二个步骤将使给定应用程序中的所有文件在调试模式下进行编译;第一个步骤仅使该特定文件在调试模式下进行编译。

重要事项: 以调试模式运行应用程序一定会产生内存/性能系统开销。在部署到生产方案之前,应确保应用程序调试已禁用。

堆栈跟踪:

[HttpException (0x80004005): 无法验证数据。]
System.Web.Configuration.MachineKeySection.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength) +2546764
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +214

[ViewStateException: 无效的视图状态。
Client IP: 127.0.0.1
Port: 1678
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
ViewState: +DHKNyze8Q6fZlW8T3kqrOkcvI1Y6qlhKi0bcpHnBmZdO7ftgMjL4U8x0qsEdbVEp1FmjzezRsCnvvvwXObQqhrwVRRTCc1IAwzNbZeKE3qjqSDfHk3HmK6xBgHm1sTsmWj/0KFgL6qwstm3dOjvGIHahto1iI3T4lH6YU+tjyEms7Fxdv8JZgkux2ghCHWGOmfTVM7C+audblvkaTrfRRlruUsmC1EApq0uAn72UsyW5k9ZdRESK8oZXJsF0149LXunb4FC/bQ96YxtVGE9sQEpZC6mVWBN6/1yK6cuI/sKaNz+ObMvILwEO4uCEKIY4eIRfcIYSG5x9gPTczsWGCfpr7vUTo8FSsXcA/hFkazRtI2MUiCRXo05ISKKhgxpRSYcm50cvpOKryPsre0hxe6Nr5fT17LFm0Y7BmZbh4zEM925/LBwgXRb8aOIMJxkTxIFk787JPF/JBMYZzmoXLacAko5/pUv/M6Cliy3vE4ex0PK5avyPbepMllTtOvfYvmiYCARrElK2zNV3836FEY9k+9898tBijFDCjpDZvXAxNsWFcdYFpUAS+w5elGBmtSIqVJlz7aQikjR9LePHmvKwTCsUjRAGjhFqu+3wa8nj64ySYTL1LYK+YL+dDaQlPF9MT1FQjHnnmHuLdtRIfnZXbv3O0l4biN5eY4Rmovq9ATRjxYcfRN0yw8sf1Gpm29IGO4VZCyJ0xWd4SyOoYrKcq4AVIRK21drbjRQuEKp9BwAgka8xTltFtLTCCKoUZmMbdvxfS233lrEdEGQBg7Dyczt1McCmxkHyI7FOPWIKB16ZCJIG7fVxW3pOFAwL4u8g271kC2o+xERTXCZFjBX9bkwD9...]

[HttpException (0x80004005): 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。]
System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +116
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +251
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
System.Web.UI.HiddenFieldPageStatePersister.Load() +220
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +83
System.Web.UI.Page.LoadAllState() +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6953
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +154
System.Web.UI.Page.ProcessRequest() +86
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +18
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.offer_aspx.ProcessRequest(HttpContext context) +29
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +154
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64


版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42

通过孟繁永

.net文件上传代码实例

<%@ Import Namespace=”System.IO” %>
<%@ page Language=”C#” debug=”true” %>
<html>
<head>
<title>上传文件 , http://www.chinabs.net </title>
<script language=”C#” runat=”server”>
 //This method is called when the “upload” button id pressed
 public void UploadFile(object sender , EventArgs E)
 {
   //检查上传文件不为空
   if(myFile.PostedFile!=null)
   {     
  string nam = myFile.PostedFile.FileName ;
  //取得文件名(抱括路径)里最后一个”.”的索引
  int i= nam.LastIndexOf(“.”);
  //取得文件扩展名
  string newext =nam.Substring(i);
  //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
  DateTime now = DateTime.Now; 
  string newname=now.DayOfYear.ToString()+myFile.PostedFile.ContentLength.ToString(); 
  //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
  //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里”\”必须用”\\”代替
  myFile.PostedFile.SaveAs(Server.MapPath(“\\upload\\”+newname+newext)); 
  //得到这个文件的相关属性:文件名,文件类型,文件大小
  fname.Text=myFile.PostedFile.FileName;
  fenc.Text=myFile.PostedFile.ContentType ;
  fsize.Text=myFile.PostedFile.ContentLength.ToString();
   }
 }
</script>
</head>
<body>
<center>
<h3> 文件上传的实例, 来自<a href=”http://www.chinabs.net”>中国BS网</a></h3>
<form id=”uploderform” method=”post” action=”FileUpload.aspx” enctype=”multipart/form-data”  runat=”server” >
<table border=”1″ cellspacing=”2″ cellpadding=”2″ >
<tr> <td><h5>选择要上传的文件:</h5></td</tr>
<tr>
<td>
<input type=”file” id=”myFile” runat=”server” NAME=”myFile”>
</td>
</tr>
<tr><td>
<input type=”button”  value=”上 传” OnServerClick=”UploadFile” runat=”server” ID=”Button1″ NAME=”Button1″>
</td></tr>
</table>
</form>
<br>
<br>
<table border=”1″ cellspacing=”2″>
<tr><td><b>文件资料</b></td>
<td>&nbsp;</td> 
</tr>
<tr>
<td>文件名 :</td>
<td><asp:label id=”fname” text=”” runat=”server” /></td></tr>
<tr>
<td>文件类型 :</td>
<td><asp:label id=”fenc” runat=”server” /></td></tr>
<tr>
<td>文件大小 :(in bytes)</td>
<td><asp:label id=”fsize” runat=”server” /></td></tr>
</table>
<br>
<br>
<br>
</center>
</body>
</html>

通过孟繁永

input 按钮使用图片样式

<input  name=”submit” type=”image” src=”../image/submit.jpg” value=”提交”/>

需要注意的是添加src的时候,相应要把type改为image。