ASP.NET 学习手记

Asp.net


1.ASP.net中如何在二个页面传递数据

 

一、提交数据
(1) 用服务器端控件后,再利用Server.Execute("send4.aspx"); 或者Server.Transfer("receive1.aspx"); 提交。前者仍然保持原有的web控件,后者不保存。
(2) 写在URL参数里Response.Redirect("receive3.aspx?name=" + this.TextBox1.Text);
(3) 通过HTML控件,加入带有action的form。

二、接收数据
(1) 按类接收数据
  if(Context.Handler is MyWebExample.send4)
  {
    send4 send = (send4)Context.Handler;
    Response.Write("Name:"+((TextBox)send.FindControl("TextBox1")).Text+"<p>");
  }
(2) 从URL中接收

  if(!IsPostBack)
  {
    Request.QueryString["text1"]

  }
(3) 从Form中接收Request.Form["text1"]
(4) 作为参数笼统接收Request.Params["text1"]

 

<返回>

 


2.如何添加用户自定义标签、自定义控件、定制控件

 

 <1>定制标签较容易,把HTML代码保存到一个文件。然后用二句话在需要的地方引用:

    <%@ Register TagPrefix="mycontrol" TagName="myc" Src="myUserControl.ascx"%>
    <mycontrol:myc runat="server" id="Myc1"></mycontrol:myc>

 <2>自定义控件

    和定制标签类似,只是可以自己添加属性。

   

 

<返回>

 

 

3.添加客户端验证脚本
 

(1)添加JavaScript
   <script language="javascript">
   function ClientCheck(source,arguments)
   {
      if(arguments.Value<1 || arguments.Value>100 )
      {
         arguments.IsValid=false;
         return false;
      }
      else
      {
         arguments.IsValid=true;
         return true;
      }
   }
   </script>

(2)添加CustomValidator控件,并且把ClientValidateFunction设为该函数

(3)设置ControlToValidate

 

<返回>

 

 

4.如何使得DataGrid有分页输出数据功能

(1).对DataGrid按右键,在弹出菜单的“属性生成器”中可以设置分页。
(2).接着对控件添加PageIndexChanged事件。并且输入以下代码
   private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
   {
      DataGrid1.EditItemIndex = -1;
      DataGrid1.CurrentPageIndex = e.NewPageIndex;
      DataGrid1.DataBind();
   }
 

<返回>
 

5.如何写广告控件的XML


<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
   <ImageUrl></ImageUrl>
   <NavigateUrl>http://www.neusoft.com</NavigateUrl>
   <AlternateText>欢迎</AlternateText>
   <Keyword>Keyword</Keyword>
   <Impressions>50</Impressions>
</Ad>
</Advertisements>

 

<返回>


 

6.如何利用Session和Application

 

使用Session和Application不要初始化,和普通的asp一样

   if(Application["userCount"]==null)
      Application["userCount"]=0;
   else
      Application["userCount"]=Convert.ToInt32(Application["userCount"])+1;

session用法和Application一样。

 

<返回>

 


7.如何利用Cookie

 

   if(Request.Cookies["cookie"]==null)
   {
      HttpCookie cookie = new HttpCookie("cookie","1");
      Response.Cookies.Add(cookie);

      this.Label3.Text="Cookies is 1";
   }
   else
   {
      HttpCookie cook = Request.Cookies["cookie"];

      Response.Cookies["cookie"].Value=(Int32.Parse(cook.Value)+1).ToString();

      this.Label3.Text=string.Format("Cookies is {0}",cook.Value);
   }

 

<返回>

8.利用ASP.net上传文件

  1.从HTML控件栏中拖入一个文件浏览控件
  2.设置该控件在服务器端运行
  3.给它的上传添加代码
   string fileName=this.File1.PostedFile.FileName;
   string UploadFileName=Request.MapPath(Request.ApplicationPath+"//"+ System.IO.Path.GetFileName(fileName));
   this.File1.PostedFile.SaveAs(UploadFileName);
 

<返回>

9.在Asp.net中自定义异常页面

  按异常处理优先级排序
  (0)在Global.asax的Application_Error添加代码
    protected void Application_Error(Object sender, EventArgs e)
    {
        Context.ClearError();
        Response.Write("Error");
        Response.Redirect("errorpage.htm");
    }

  (1)后台代码中的WebForm1_Error
     aspx页面的属性中UI.Page的Error事件中添加异常处理代码
     private void WebForm1_Error(object sender, System.EventArgs e)
     {
        Exception ex=Server.GetLastError();
        Session["error"]=ex.Message;
        Server.ClearError();
        Response.Redirect("error.aspx");
     }
  (2)在html代码中加入ErrorPage
     ErrorPage="http://www.21cn.com"
  (3)在Web.config中添加异常处理的页面
    <customErrors mode="On" defaultRedirect="error.aspx">
    <error statusCode="401" redirect="error.aspx"/>
    <error statusCode="404" redirect="http://www.sina.com.cn"/>
    </customErrors>
  (4)在IIS中设置异常处理页

<返回>

10.Asp.net的安全认证及Web.config的配置

 (1)在Web.config的配置   
   在<system.web>中修改选项
     验证模式设为Form,并且验证页为
      <authentication mode="Forms">
        <forms loginUrl="Login.aspx" />
      </authentication>
     不允许匿名用户
      <authorization>
        <deny users="?" />
      </authorization>

   在</system.web>后加入不要验证就能使用数据库的页面,用于在该页访问数据库,察看是否存在该用户。
     <location path="Reg.aspx">
       <system.web>
         <authorization>
           <allow users="*"/>
         </authorization>
       </system.web>
     </location>

  (2)在代码中按普通方式,例如要求对方输入信息查找数据库或者XML进行验证,验证通过后,执行这句就表示验证通过同时跳会开始进入的页面
      System.Web.Security.FormsAuthentication.RedirectFromLoginPage(userName,true);
      注销用 System.Web.Security.FormsAuthentication.SignOut();  
      如果不想跳回原处,可以先授权再redirect到其他页面  System.Web.Security.FormsAuthentication.SetAuthCookie();

<返回>

11.Asp网页的EnableViewState属性对网页性能的影响

    ViewState主要是在提交以后回显用的,它只有在页面中的数据是提交到本页时才有用,在这个时候,比如Textbox,你用EnableViewState="false",后台同样可以得到数据,但由于你提交到本页,所以提交以后此Textbox中为空;而如果用EnableViewState="true",则提交以后返回时页面中Textbox中为你提交以前的数据。
    另外,除了页面Page中的EnableViewState,每个可以提交的控件,Textbox、Dropdownlist都有EnableViewState属性。实际上,回发的数据并不依赖于ViewState。回发的控件都实现了IPostBackDataHandler接口,该接口的LoadPostData方法中,会对返回的值和ViewState中的值进行判断,如果改变了的话,调用RaisePostDataChangedEvent方法触发相应的事件(对于TextBox来说就是TextChanged事件)。
    如果你把EnableViewState="False",LoadPostData方法中返回的值始终会和文本框的默认值比较大小,也就是说,如果你在页面TextBox中改变值以后,每次你点按钮提交窗口都会触发TextBox的TextChanged事件LoadPostData中如果返回的值和ViewState中的值不同的话,将把TextBox的值设置成返回的值这就是你看到的结果 。
    在很多情况下,把EnableViewState设为false,可以提高应用程序的性能。特别在等待后台把数据填充到DataGrid的情况下。如果这个时候设为true,那么cpu的时间都浪费 在序列化数据到 ViewState 中。
     每个控件(在标记上):sp:datagrid EnableViewState="false" ?/>
     每个页面(在指令中): <%@ Page EnableViewState="False" ?%>
     每个应用程序(在 web.config 中): <Pages EnableViewState="false" ?/>
    更多请查看微软中国


<返回>
 

12.Web打印文档

<!--语言无关 保存成 .HTML-->
<html>
<head>
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<title>网络打印模板页</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!--media=print 这个属性可以在打印时有效-->
<style media=print>
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>

<style>
.tdp
{
border-bottom: 1 solid #000000;
border-left: 1 solid #000000;
border-right: 0 solid #ffffff;
border-top: 0 solid #ffffff;
}
.tabp
{
border-color: #000000 #000000 #000000 #000000;
border-style: solid;
border-top-width: 2px;
border-right-width: 2px;
border-bottom-width: 1px;
border-left-width: 1px;
}
.NOPRINT {
font-family: "宋体";
font-size: 9pt;
}
</style>

</head>

<body >
<center class="Noprint" >
<p>
<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0>
</OBJECT>
<input type=button value=打印 οnclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button value=直接打印 οnclick=document.all.WebBrowser.ExecWB(6,6)>
<input type=button value=页面设置 οnclick=document.all.WebBrowser.ExecWB(8,1)>
</p>
<p> <input type=button value=打印预览 οnclick=document.all.WebBrowser.ExecWB(7,1)>
<br/>
</p>
<hr align="center" width="90%" size="1" noshade>
</center>

<table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="tabp">
<tr>
<td colspan="3" class="tdp">第1页</td>
</tr>
<tr>
<td width="29%" class="tdp"> </td>
<td width="28%" class="tdp"> </td>
<td width="43%" class="tdp"> </td>
</tr>
<tr>
<td colspan="3" class="tdp"> </td>
</tr>
<tr>
<td colspan="3" class="tdp"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" class="tdp"><p>这样的报表</p>
<p>对一般的要求就够了。</p></td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
<hr align="center" width="90%" size="1" noshade class="NOPRINT" >
<!--分页-->
<div class="PageNext"></div>
<table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="tabp">
<tr>
<td class="tdp">第2页</td>
</tr>
<tr>
<td class="tdp">看到分页了吧</td>
</tr>
<tr>
<td class="tdp"> </td>
</tr>
<tr>
<td class="tdp"> </td>
</tr>
<tr>
<td class="tdp"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" class="tdp"><p>这样的报表</p>
<p>对一般的要求就够了。</p></td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>


在基于框架的网页打印时,用如下函数可以打印某个框架内的网页
<input type=button οnclick="printweb(this)">
<script>
function printweb()
{
this.focus();
window.print();
}
</script>
 

<返回>


13.将Web表格输出为word或者Excel格式的文件保存在客户端

   Response.Clear();
   Response.Buffer= true;
   if (Session["Language"]!=null && Session["Language"].ToString()!="EN")
   {
      Response.Charset="GB2312";
   }
   Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
   if (Session["Language"]!=null && Session["Language"].ToString()!="EN")
   {
      Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
   }//设置输出流为简体中文
   Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
   //application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档

   this.EnableViewState = false;
   System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
   System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
   System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
   DataGrid1.RenderControl(oHtmlTextWriter); //DataGrid1为DataGrid控件,也可以是动态生成的HtmlTable  <---唯一需要修改的地方
   Response.Write(oStringWriter.ToString());
   Response.End();
 

<返回>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值