ASP.NET 揭秘 第二章用Web Server Controls建立Forms

Chapter 2. Building Forms with Web Server Controls
一、Building Smart Forms
 Using Label Controls(标签)
  The Label control has one important property: Text,支持AutoPostBack属性
  The contents of a Label control are rendered within a <span> tag (you can see this if you click View Source on your browser).
  Note: The LiteralControl is very similar to the Label control except for the fact that it does not render <span> tags. You can use the LiteralControl within the <title> tag of a page.
 Using the TextBox Control(文本)
  The TextBox control can render any one of three different HTML tags,支持AutoPostBack属性
  要取消校验,在页头上添加指示<%@ ValidateRequest="False" %>,或在Concfig文件中加入<pages validateRequest="false" />
 Using Button Controls(按钮)
  Button— Displays a standard HTML form button
  默认按钮<input type="Submit">, 有一个灰边框.
  事件Click,Command,当Button按下后,同时发生Click和Command事件,两个事件的区别是Command事件有参数 CommandName and CommandArgs
   <asp:Button Text="Add 5"
  ImageButton— Displays an image form button
   Sub ImageButton_Click( s As Object, e As ImageClickEventArgs )
     Select Case e.X
       Case Is < 95
         lblMessage.Text = txtSomeText.Text.ToUpper()
       Case Is < 185
         lblMessage.Text = txtSomeText.Text.ToLower()
       Case Is < 289
         lblMessage.Text = ""
     End Select
   End Sub
  LinkButton— Displays a hypertext link.
  The LinkButton control does not work on browsers that do not support JavaScript (or browsers that have JavaScript disabled).
  Sub LinkButton_Click( s As Object, e As EventArgs )
    Dim RanNum As New Random
    Select Case RanNum.Next( 4 )
      Case 0
        lblFortune.Text = txtUsername.Text & ", good things are coming!"
      Case 1
        lblFortune.Text = txtUsername.Text & ", you are doomed!"
      Case 2
        lblFortune.Text = txtUsername.Text & ", invest in Microsoft!"
      Case 3
        lblFortune.Text = txtUsername.Text & ", this book will change your life!"
    End Select
  End Sub
 Using the RadioButton and RadioButtonList Controls(单选)
  The RadioButton Control
  你可以向页面上任意添加个别的RadioButton,它们通过GroupName进行分组,每一个分组在同一时间只能有一个被选中. 通过Checked属性,设置或检查是否选中.
  The RadioButtonList Control
  RadioButtonList是包含许多个独立的RadioButton.通过SelectedIndex, SelectedItem, SelectedValue进行识别选中项,通过SelectedIndex, SelectedValue设置默认选中项.RadioButtonList支持AutoPostBack属性.
  通过RepeatLayout, RepeatDirection,RepeatColumns对RadioButtonList的外观进行控制.
  *对于采用数据绑方式的RadioButtonList,可以使用DataTextFormatString对其进行格式化,用DataTextField ,DataValueField对Text和Value进行区分.
     DataTextFormatString="The color is {0}"
   Dim dtblStates As New DataTable
   Dim drowNewState As DataRow
   dtblStates.Columns.Add(New DataColumn( "StateName", GetType( String ) ) )
   dtblStates.Columns.Add(New DataColumn( "StateAbbr", GetType( String ) ) )
   drowNewState = dtblStates.NewRow()
   drowNewState( "StateName" ) = "California"
   drowNewState( "StateAbbr" ) = "CA"
   dtblStates.Rows.Add( drowNewState )
   drowNewState = dtblStates.NewRow()
   drowNewState( "StateName" ) = "Massachusetts"
   drowNewState( "StateAbbr" ) = "MA"
   dtblStates.Rows.Add( drowNewState )
   drowNewState = dtblStates.NewRow()
   drowNewState( "StateName" ) = "Ohio"
   drowNewState( "StateAbbr" ) = "OH"
   dtblStates.Rows.Add( drowNewState )
   radlStateOfResidence.DataSource = dtblStates
   radlStateOfResidence.DataTextField = "StateName"
   radlStateOfResidence.DataValueField = "StateAbbr"
   <Script Runat="Server">
   Sub Page_Load
      Dim intCounter As Integer
      Dim strSomeListItem As String
      If Not IsPostBack Then
        For intCounter = 1 To 50
          strSomeListItem = "Radio Button " & intCounter
          radlRadioButtons.Items.Add( strSomeListItem )
      End If
   End Sub
 Using the CheckBox and CheckBoxList Controls(复选)
  The CheckBox Control(同Radio)
  The CheckBoxList Controls (同Radio)
 Using the DropDownList Control(下拉框)
 Using the ListBox Control(列表框)
二、Controlling Page Navigation
 Submitting a Form to a Different Page
  If you use the server-side version of the <form> tag, you cannot post a form to a different page (the server-side form tag does not have an Action property).
  You can, if you have a pressing need, post a form to a new page by using the standard HTML form tags instead of ASP.NET controls
  You also can use the Form collection of the HTTPRequest class to grab form data. The difference between the Params and Form collections is that the Params collection also represents QueryStrings, ServerVariables, and Cookies.
  <form Method="Post" Action="Results.aspx">
  <Script Runat="Server">
   Sub Page_Load
     lblUsername.Text = Request.Params( "Username" )
     lblComments.Text = Request.Params( "Comments" )
  End Sub
 Using the Redirect() Method
  So, the question remains, how do you transfer a user to a new page after the user has successfully completed a form?
  The best way to transfer the user is to use the Response.Redirect() method.
  <Script Runat="Server">
   Sub Button_Click( s As Object, e As EventArgs )
      ' Save Form Data to Database Table
      Response.Redirect( "ThankYou.aspx" )
   End Sub
 Working with the HyperLink Control
  You can link one ASP.NET page to another by adding a HyperLink control to a page.
  属性:ImageUrl,NavigateUrl,Target(The target window or frame for the link. Possible values are _top, _self, _parent, _search, or _blank.),Text
  <Script Runat="Server">
   Sub Page_Load
     If TimeOfDay > #5:00pm# Then
         lnkHelpLink.NavigateUrl = "AfterHoursHelp.aspx"
         lnkHelpLink.Text = "After Hours Help"
         lnkHelpLink.NavigateURL = "Normalhelp.aspx"
         lnkHelpLink.Text = "Help"
      End If
   End Sub
三、Applying Formatting to Controls
 Base Web Control Properties(基本属性)
  <%@ Import Namespace="System.Drawing" %>
  <Script Runat="Server">
   Sub dropBackColor_SelectedIndexChanged( s As Object, e As EventArgs )
    Dim strBackColor As Color
     strBackColor = Color.FromName( dropBackColor.SelectedItem.Text )
     txtTextBox.BackColor = strBackColor
   End Sub
   Sub dropFontSize_SelectedIndexChanged( s As Object, e As EventArgs )
     txtTextBox.Font.Size = FontUnit.Parse( dropFontSize.SelectedItem.Text )
   End Sub
 Applying Styles to Web Controls(应用样式)
     text-align: justify;
     font: 14pt Script;
   <form Runat="Server">
     Text="this is some text"
 The Style Class(样式类)
  <%@ Import Namespace="System.Drawing" %>
  <Script Runat="Server">
   Sub Page_Load( s As Object, e As EventArgs )
     Dim myStyle As New Style
     myStyle.BackColor = Color.Yellow
     myStyle.ForeColor = Color.Green
     myStyle.BorderStyle = BorderStyle.Dashed
     myStyle.BorderWidth = New Unit(4)
     txtTextBox1.ApplyStyle( myStyle )
     txtTextBox2.ApplyStyle( myStyle )
     txtTextBox3.MergeStyle( myStyle )
   End Sub
   <form Runat="Server">
     Text="Here is some text!"
     Text="Here is some text!"
     Text="Here is some text!"
总结:本章讨论了基本的Web Controls,学习了怎样向ASP.NET页面添加类似于TextBox,DropDownList的Web Controls等,同时要学习了,这些Web Controls的事件处理过程.接着学习了多个ASP.NET页面如何工作,通过Response.Redirect方法进行页面跳转,同时也学习了如何使用HyperLink Control进行多个页面的链接.最后学习了,运用这些基本Web Controls的格式化属性对它们进行格式化,同时也学习了通过Cascading Style Sheet classes 进行格式化.
个人分类: 读书笔记
想对作者说点什么? 我来说一句