以下是我对asp.net的认识以及心得总结,比较基础的东西,当然本人认为要学好asp.net需要具有良好的web基础。(小磊)
< %@ Page Language = " vb " AutoEventWireup = " false " Codebehind = " WebHtmlcontrol2.aspx.vb " Inherits = " WebApplication1.WebHtmlcontrol2 " % >
< !DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
< HEAD >
< title > WebHtmlcontrol2 </ title >
< meta name = " GENERATOR " content = " Microsoft Visual Studio .NET 7.1 " >
< meta name = " CODE_LANGUAGE " content = " Visual Basic .NET 7.1 " >
< meta name = " vs_defaultClientScript " content = " JavaScript " >
< meta name = " vs_targetSchema " content = " http://schemas.microsoft.com/intellisense/ie5 " >
<script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> </script> <script runat="server" type="text/javascript"></script>
<script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script>
</ HEAD >
< body MS_POSITIONING = " GridLayout " >
< form id = " Form1 " method = " post " runat = " server " >
< asp:Button id = " Button1 " style = " Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 112px " runat = " server "
Text = " Button1 " ></ asp:Button >
< asp:TextBox id = " TextBox1 " style = " Z-INDEX: 104; LEFT: 264px; POSITION: absolute; TOP: 112px "
runat = " server " AutoPostBack = " True " ></ asp:TextBox >
< asp:DropDownList id = " DropDownList1 " style = " Z-INDEX: 105; LEFT: 472px; POSITION: absolute; TOP: 112px "
runat = " server " AutoPostBack = " True " ></ asp:DropDownList >
//对于web的text和drop需要设置 AutoPostBack="True"否则将不会执行后台的服务器事件,
这样每次执行后都会提交一次页面,这时就需要我们在后台Page_Load加入If Not Me.IsPostBack Then...
将一次执行代码写入,否则就会重复执行。
< INPUT style = " Z-INDEX: 102; LEFT: 160px; POSITION: absolute; TOP: 256px " type = " submit " value = " Submit " name = " sumbit1 " id = " sumbit1 " οnclick="sumbit1_onclick();" >
// web端的button与html中的sumbit是一个效果,都会提交本页面
< INPUT style = " Z-INDEX: 103; LEFT: 264px; POSITION: absolute; TOP: 264px " type = " text " id = " text1 " name = " text1 " >
//对于Html控件需要手动增加其id和name,web控件就不需要了 < SELECT style = " Z-INDEX: 106; LEFT: 472px; WIDTH: 72px; POSITION: absolute; TOP: 264px " name = " select1 " id = " select1 " >
< OPTION value = " aaa " > aaa </ OPTION >
< OPTION value = " bbb " > bbb </ OPTION >
< OPTION value = " ccc " > ccc </ OPTION >
< OPTION value = " ddd " selected > ddd </ OPTION >
</ SELECT >
< asp:Label id = " Label1 " style = " Z-INDEX: 107; LEFT: 136px; POSITION: absolute; TOP: 72px " runat = " server " > Web </ asp:Label >
< asp:Label id = " Label2 " style = " Z-INDEX: 108; LEFT: 136px; POSITION: absolute; TOP: 208px " runat = " server " > Html </ asp:Label >
<input id="Button2" style="Z-INDEX: 109; LEFT: 168px; POSITION: absolute; TOP: 312px"type="button" value="Button2" name="Button2" runat="server " onserverclick="onserver_Click">
//这是将html控件转换为web控件,这样就可以在后台将其当服务器控件一样使用,当然需要设置id
因为我们在前台指定了onserverclick或者onclick事件,所以如果onserver_Click事件放在后台执行的就不需要委托,否则需要委托事件,见下
</ form >
</ body >
</ HTML >
后台代码:
Public Class WebHtmlcontrol2
Inherits System.Web.UI.Page
#Region " Web フォーム デザイナで生成されたコード "
'この呼び出しは Web フォーム デザイナで必要です。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents text1 As System.Web.UI.HtmlControls.HtmlInputText
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
'メモ : 次のプレースホルダ宣言は Web フォーム デザイナで必要です。
'削除および移動しないでください。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
' CODEGEN: このメソッド呼び出しは Web フォーム デザイナで必要です。
' コード エディタを使って変更しないでください。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack Then
Call AddDropDownList()
Button1.Attributes.Add("onclick", "return ggg();") //这就是web控件执行客户端事件的方法
TextBox1.Attributes.Add("onkeyup", "textbox1_onkeyup();")
End If
Response.Write(Request.Form("text1"))//这是在服务器端取得Html控件的值得方法
’Call StartupScript() //这是在服务器段增加客户端脚本的方法,两者的区别是页面完整读取完后显示
Call ClientScriptBlock() //这是在服务器段增加客户端脚本的方法,该方法是页面读取前显示
'AddHandler Button2.ServerClick, AddressOf onserver_Click
//就是前面所说的如果没有指定 onserverclick="onserver_Click"就需要在后台委托,同事将该 onserver_Click事 件放在后台执行
End Sub
Private Sub AddDropDownList()
DropDownList1.Items.Add("aaa")
DropDownList1.Items.Add("bbb")
DropDownList1.Items.Add("ccc")
DropDownList1.Items.Add("ddd")
End Sub
Private Sub StartupScript()
Dim script As String
script = "<script language=""javascript"">" & vbCrLf
script = script & "function ggg(){" & vbCrLf
script = script & "if(!confirm('aaa'))return false;" & vbCrLf
script = script & "}" & vbCrLf
script = script & "</script>"
If Not Page.IsStartupScriptRegistered("123") Then
Page.RegisterStartupScript("123", script)
End If
End Sub
Private Sub ClientScriptBlock()
Dim script1 As String
script1 = "<script language=""javascript"">" & vbCrLf
script1 = script1 & "function ggg(){" & vbCrLf
script1 = script1 & "if(!confirm('aaa'))return false;" & vbCrLf
script1 = script1 & "}" & vbCrLf
script1 = script1 & "</script>"
If Not Page.IsClientScriptBlockRegistered("clientScript") Then
Page.RegisterClientScriptBlock("clientScript", script1)
End If
End Sub
Public Sub Html() //这就是在服务器端向Html控件传值得方法
Dim str As String
str = "Html Web"
Page.RegisterStartupScript("example", "<script language='JavaScript'>document.getElementById('text1').value='" + str + "';</script>")
End Sub
End Class
< %@ Page Language = " vb " AutoEventWireup = " false " Codebehind = " WebHtmlcontrol2.aspx.vb " Inherits = " WebApplication1.WebHtmlcontrol2 " % >
< !DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
< HEAD >
< title > WebHtmlcontrol2 </ title >
< meta name = " GENERATOR " content = " Microsoft Visual Studio .NET 7.1 " >
< meta name = " CODE_LANGUAGE " content = " Visual Basic .NET 7.1 " >
< meta name = " vs_defaultClientScript " content = " JavaScript " >
< meta name = " vs_targetSchema " content = " http://schemas.microsoft.com/intellisense/ie5 " >
<script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> </script> <script runat="server" type="text/javascript"></script>
<
script runat
=
"
server
"
>
//
这块是服务器代码,同样可以写在后台
Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs)
// 这是当Html控件转换为Web控件后,调用的服务器事件过程(同样可以写在后台),
注意该事件的参数一定要与实际事件的参数一致
Response.Write( " onserver_click " )
End Sub
Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.Write( " button1_Click " )
End Sub
Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Response.Write("TextBox1_TextChanged") End Sub
</ script >
< script language = " javascript " >
// 这块是客户端脚本,提出这点是因为Web控件的id很容易发生变化,所以需要利用clientid来准确判断该元素
function sumbit1_onclick() {
alert(document.getElementById("<%=textbox1.clientid%>").value)
}
function textbox1_onkeyup(){
/ /这段代码的意思是在客户端脚本可以调用服务器控件textbox1的TextBox1_TextChanged事件,我们从源代码中经常会看到 __doPostBack这是系统自己写的,意思就是服务器的回发事件也就是执行服务器控件的事件
__doPostBack('textbox1','');
}
</ script >
Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs)
// 这是当Html控件转换为Web控件后,调用的服务器事件过程(同样可以写在后台),
注意该事件的参数一定要与实际事件的参数一致
Response.Write( " onserver_click " )
End Sub
Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.Write( " button1_Click " )
End Sub
Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Response.Write("TextBox1_TextChanged") End Sub
</ script >
< script language = " javascript " >
// 这块是客户端脚本,提出这点是因为Web控件的id很容易发生变化,所以需要利用clientid来准确判断该元素
function sumbit1_onclick() {
alert(document.getElementById("<%=textbox1.clientid%>").value)
}
function textbox1_onkeyup(){
/ /这段代码的意思是在客户端脚本可以调用服务器控件textbox1的TextBox1_TextChanged事件,我们从源代码中经常会看到 __doPostBack这是系统自己写的,意思就是服务器的回发事件也就是执行服务器控件的事件
__doPostBack('textbox1','');
}
</ script >
<script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script> <script runat="server" type="text/javascript"> Sub onserver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("onserver_click") End Sub Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("button1_Click") End Sub </script>
</ HEAD >
< body MS_POSITIONING = " GridLayout " >
< form id = " Form1 " method = " post " runat = " server " >
< asp:Button id = " Button1 " style = " Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 112px " runat = " server "
Text = " Button1 " ></ asp:Button >
< asp:TextBox id = " TextBox1 " style = " Z-INDEX: 104; LEFT: 264px; POSITION: absolute; TOP: 112px "
runat = " server " AutoPostBack = " True " ></ asp:TextBox >
< asp:DropDownList id = " DropDownList1 " style = " Z-INDEX: 105; LEFT: 472px; POSITION: absolute; TOP: 112px "
runat = " server " AutoPostBack = " True " ></ asp:DropDownList >
//对于web的text和drop需要设置 AutoPostBack="True"否则将不会执行后台的服务器事件,
这样每次执行后都会提交一次页面,这时就需要我们在后台Page_Load加入If Not Me.IsPostBack Then...
将一次执行代码写入,否则就会重复执行。
< INPUT style = " Z-INDEX: 102; LEFT: 160px; POSITION: absolute; TOP: 256px " type = " submit " value = " Submit " name = " sumbit1 " id = " sumbit1 " οnclick="sumbit1_onclick();" >
// web端的button与html中的sumbit是一个效果,都会提交本页面
< INPUT style = " Z-INDEX: 103; LEFT: 264px; POSITION: absolute; TOP: 264px " type = " text " id = " text1 " name = " text1 " >
//对于Html控件需要手动增加其id和name,web控件就不需要了 < SELECT style = " Z-INDEX: 106; LEFT: 472px; WIDTH: 72px; POSITION: absolute; TOP: 264px " name = " select1 " id = " select1 " >
< OPTION value = " aaa " > aaa </ OPTION >
< OPTION value = " bbb " > bbb </ OPTION >
< OPTION value = " ccc " > ccc </ OPTION >
< OPTION value = " ddd " selected > ddd </ OPTION >
</ SELECT >
< asp:Label id = " Label1 " style = " Z-INDEX: 107; LEFT: 136px; POSITION: absolute; TOP: 72px " runat = " server " > Web </ asp:Label >
< asp:Label id = " Label2 " style = " Z-INDEX: 108; LEFT: 136px; POSITION: absolute; TOP: 208px " runat = " server " > Html </ asp:Label >
<input id="Button2" style="Z-INDEX: 109; LEFT: 168px; POSITION: absolute; TOP: 312px"type="button" value="Button2" name="Button2" runat="server " onserverclick="onserver_Click">
//这是将html控件转换为web控件,这样就可以在后台将其当服务器控件一样使用,当然需要设置id
因为我们在前台指定了onserverclick或者onclick事件,所以如果onserver_Click事件放在后台执行的就不需要委托,否则需要委托事件,见下
</ form >
</ body >
</ HTML >
后台代码:
Public Class WebHtmlcontrol2
Inherits System.Web.UI.Page
#Region " Web フォーム デザイナで生成されたコード "
'この呼び出しは Web フォーム デザイナで必要です。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents text1 As System.Web.UI.HtmlControls.HtmlInputText
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
'メモ : 次のプレースホルダ宣言は Web フォーム デザイナで必要です。
'削除および移動しないでください。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
' CODEGEN: このメソッド呼び出しは Web フォーム デザイナで必要です。
' コード エディタを使って変更しないでください。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack Then
Call AddDropDownList()
Button1.Attributes.Add("onclick", "return ggg();") //这就是web控件执行客户端事件的方法
TextBox1.Attributes.Add("onkeyup", "textbox1_onkeyup();")
End If
Response.Write(Request.Form("text1"))//这是在服务器端取得Html控件的值得方法
’Call StartupScript() //这是在服务器段增加客户端脚本的方法,两者的区别是页面完整读取完后显示
Call ClientScriptBlock() //这是在服务器段增加客户端脚本的方法,该方法是页面读取前显示
'AddHandler Button2.ServerClick, AddressOf onserver_Click
//就是前面所说的如果没有指定 onserverclick="onserver_Click"就需要在后台委托,同事将该 onserver_Click事 件放在后台执行
End Sub
Private Sub AddDropDownList()
DropDownList1.Items.Add("aaa")
DropDownList1.Items.Add("bbb")
DropDownList1.Items.Add("ccc")
DropDownList1.Items.Add("ddd")
End Sub
Private Sub StartupScript()
Dim script As String
script = "<script language=""javascript"">" & vbCrLf
script = script & "function ggg(){" & vbCrLf
script = script & "if(!confirm('aaa'))return false;" & vbCrLf
script = script & "}" & vbCrLf
script = script & "</script>"
If Not Page.IsStartupScriptRegistered("123") Then
Page.RegisterStartupScript("123", script)
End If
End Sub
Private Sub ClientScriptBlock()
Dim script1 As String
script1 = "<script language=""javascript"">" & vbCrLf
script1 = script1 & "function ggg(){" & vbCrLf
script1 = script1 & "if(!confirm('aaa'))return false;" & vbCrLf
script1 = script1 & "}" & vbCrLf
script1 = script1 & "</script>"
If Not Page.IsClientScriptBlockRegistered("clientScript") Then
Page.RegisterClientScriptBlock("clientScript", script1)
End If
End Sub
Public Sub Html() //这就是在服务器端向Html控件传值得方法
Dim str As String
str = "Html Web"
Page.RegisterStartupScript("example", "<script language='JavaScript'>document.getElementById('text1').value='" + str + "';</script>")
End Sub
End Class