技巧:ASP.NET热点问题解答14个

来源:岁月联盟 编辑:zhu 时间:2007-01-31
  ASP.NET热点问题解答14个

  1、ASP.NET能在那些系统中运行?

  目前,ASP.NET还只能奔跑在微软的Windows 2000、Windows XP和Windows 2003的系统中,并且需要微软Internet Information Server(IIS)的支持,微软原计划要让Windows NT4.0也支持ASP.NET,但可能微软是有些技术问题或市场考虑,还没有实现NT下的ASP.NET的支持。

  2、在一个ASPX文件中是否可以使用一种以上的语言?

  答案让你有点失望,虽然微软的提供了公共语言运行环境(CLR,Common Laguage Runtime),实现了多种编程语言间的紧密集成,可以允许你从一个VB对象中导出C#所需的对象来,但一个ASPX文件中只能用一种语言,正如你不能在VB.NET中使用C#的语法一样。

  3、ASPX文件的服务器端脚本支持那些语言?

  目前,ASPX文件只支持C#、Visual Basic.NET、Jscript.NET和J#,但是你使用code—behind(代码分离)的方法创建一个独立代码文件,你就可以使用任何.NET编译器支持的语言来实现功能了。

  4、在Global.asax文件中能使用code—behind(代码分离)技术吗?

  当然可以了,例如:
  Global.asax:
  
  和使用code—behind(代码分离)技术
  Global.asax:
  MyApp.vb:


以下是引用片段:
  Imports System.Web
  Imports System.Web.SessionState
  Public Class MyApp
  Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
  Application("online_session") = 0
  End Sub

  Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
  Application.Lock()
  Application("online_session") = CInt(Application("online_session")) + 1
  Application.UnLock()
  End Sub
  Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
  Application.Lock()
  Application("online_session") = CInt(Application("online_session")) - 1
  Application.UnLock()
  End Sub
  End Class


  5、我能否看到ASPX文件在ASP.NET中生成的代码吗?

  可以看到的,当你的ASPX文件中包含命令或Web.config中声明了时,你就可以在系统目录下的Microsoft.NET/Framework/v1.0.nnnn/Temporary ASP.NET Files中找到ASPX文件在ASP.NET下生成的文件。   

  6、在ASPX文件中如何注释呢?

  同ASP文件中的方法一样。   

  7、ASPX文件中是否可以存在一个以上服务器端 Form 标记?

  不可以

  8、我可以在Web窗体中使用自定义数据类型吗

可以,你可以把包含自定义数据类型的DLL文件放在程序根目录下的BIN目录中,ASP.NET会在数据类型引用时,装载DLL文件的。

  9、我能在Global.asax文件中触发那些事件?
  Application对象创建和结束时所触发的事件有
  ·Application_Start
  ·Application_End
  Session对象创建和结束时所触发的事件有
  ·Session_Start
  ·Session_End
  对程序有请求发生时触发的事件有 (按发生顺序排列)
  ·Application_BeginRequest
  ·Application_AuthenticateRequest
  ·Application_AuthorizeRequest
  ·Application_ResolveRequestCache
  ·Application_AcquireRequestState
  ·Application_PreRequestHandlerExecute
  ·Application_PostRequestHandlerExecute
  ·Application_ReleaseRequestState
  ·Application_UpdateRequestCache
  ·Application_EndRequest
  当有程序有错误发生时触发的事件有
  ·Application_Error
  ·Application_Disposed
  10、Web控件是否支持样式表(CSS)呢?

  Yes. All Web controls inherit a property named CssClass from the base class System.Web.UI.WebControls.WebControl. The following example defines a CSS class named Input and uses it to modify a TextBox control to display text in red 10-point Verdana type:

  支持,所有的Web控件都从基类System.Web.UI.WebControls.WebControl中继承了一个叫做CssClass的属性。

例如:

以下是引用片段:
<html>
<head>
<style>
.Input { font: 10pt verdana; color: red; }
</style>
</head>
<body>
<form runat="server">
<asp:TextBox CssClass="Input" RunAt="server" />
</form>
</body>
</html>


  11、在ASPX文件中默认导入那些名称空间?

  ASPX默认导入的名称空间可以直接引用了,使用其它的名称空间就的自行导入了。

  默认名称空间
  ·System
  ·System.Collections
  ·System.Collections.Specialized
  ·System.Configuration
  ·System.Text
  ·System.Text.RegularExpressions
  ·System.Web

  ·System.Web.Caching
  ·System.Web.Security
  ·System.Web.SessionState
  ·System.Web.UI
  ·System.Web.UI.HtmlControls
  ·System.Web.UI.WebControls   

  12、我是否可以自己创建服务器控件呢?

  可以,创作您自己的 ASP.NET 服务器控件很容易。创建简单的自定义控件时,您所要做的只是定义从 System.Web.UI.Control 派生的类并重写它的 Render 方法。Render 方法采用 System.Web.UI.HtmlTextWriter 类型的参数。控件要发送到客户端的 HTML 作为字符串参数传递到 HtmlTextWriter 的 Write 方法。

  例如:

  服务器控件代码(简单显示字符串):Simple.vb:


以下是引用片段:
Imports System
Imports System.Web
Imports System.Web.UI

Namespace SimpleControlSamples

Public Class SimpleVB : Inherits Control

Protected Overrides Sub Render(Output As HtmlTextWriter)
Output.Write("<H2>欢迎使用控件开发!</H2>")
End Sub
End Class
End Namespace

引用文件Simple.aspx:
<%@ Register TagPrefix="SimpleControlSamples" Namespace="SimpleControlSamples" Assembly="SimpleControlSamplesVB" %>

<html>
<body>
<form method="POST" action="Simple.aspx" runat=server>
<SimpleControlSamples:SimpleVB id="MyControl" runat=server/>
</form>
</body>
</html>



  13、如何在ASP.NET程序中发送邮件呢?

  在ASP.NET程序中发送邮件不再象ASP中那样需要组件的支持了,在.NET的框架基类的System.Web.Mail名称空间内包含的MailMessage和SmtpMail类可以实现这个功能。

  例如:

以下是引用片段:
  Dim message As new Mail.MailMessage
  message.From = "web3@163.com"
  message.To = "web3@163.com"
  message.Subject = "测试"
  message.Body = "内容"
  Mail.SmtpMail.SmtpServer = "localhost"
  Mail.SmtpMail.Send(message)

  14、我将如何通过ADO.NET读取数据库中的图片并显示它呢?
下面举一个从Microsoft SQL Server的PUB数据库读取图片并显示它的例子:
  下面举一个从Microsoft SQL Server的PUB数据库读取图片并显示它的例子:

以下是引用片段:
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_load(Sender as Object, E as EventArgs)
dim stream as new MemoryStream
dim connection as SqlConnection
connection=new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=")
try
connection.Open()
dim command as SqlCommand
command = new SqlCommand ("select logo from pub_info where pub_id='0736'", connection)
dim image as byte()
image = command.ExecuteScalar ()
stream.Write (image, 0, image.Length)
dim imgbitmap as bitmap
imgbitmap = new Bitmap (stream)
Response.ContentType = "image/gif"
imgbitmap.Save (Response.OutputStream, ImageFormat.Gif)
Finally
connection.Close()
stream.Clse()
End Try
End Sub
</script>



【责任编辑 徐洋】