通过示例看页面处理中的事件

来源:互联网 发布:高薪网络销售招聘 编辑:程序博客网 时间:2024/05/01 21:27

 

 

这是一个用来演示ASP.NET页面处理过程时事件发挥的作用的示例,该示例通过动态(有点AJAX味道,但没有使用AJAX框架)的生成一张企业名片,讲述了几个常用事件的用法,更重要的是,里面的一些代码对于新手(比如当前的我)会很有帮助。

前台:EventDemo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>Untitled Page</title>
  6. </head>
  7. <body style="font-size: 10pt; font-family: Arial">
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <table style="width: 404px">
  11.             <tr>
  12.                 <td style="width: 57px">
  13.                     <asp:Label ID="NameLabel" runat="server" Text="Name:"></asp:Label></td>
  14.                 <td style="width: 238px">
  15.                     <asp:TextBox ID="NameTextBox" runat="server" Width="146px" AutoPostBack="True" 
  16.                         ontextchanged="NameTextBox_TextChanged"></asp:TextBox></td>
  17.             </tr>
  18.             <tr>
  19.                 <td style="width: 57px; height: 24px">
  20.                     <asp:Label ID="TelNumLabel" runat="server" Text="Tel Num:"></asp:Label></td>
  21.                 <td style="width: 238px; height: 24px">
  22.                     <asp:TextBox ID="TelNumTextBox" runat="server" Width="146px" 
  23.                         AutoPostBack="True" ontextchanged="TelNumTextBox_TextChanged"></asp:TextBox></td>
  24.             </tr>
  25.             <tr>
  26.                 <td style="width: 57px; height: 24px;">
  27.                     <asp:Label ID="RegionLabel" runat="server" Text="Region:"></asp:Label></td>
  28.                 <td style="width: 238px; height: 24px;">
  29.                     <asp:DropDownList ID="RegionList" runat="server" Width="152px" 
  30.                         AutoPostBack="True" 
  31.                         onselectedindexchanged="RegionList_SelectedIndexChanged">
  32.                     </asp:DropDownList></td>
  33.             </tr>
  34.             <tr>
  35.                 <td style="width: 57px; height: 24px">
  36.                     <asp:Label ID="CountryLabel" runat="server" Text="Country:"></asp:Label></td>
  37.                 <td style="width: 238px; height: 24px">
  38.                     <asp:DropDownList ID="CountryList" runat="server" Width="152px" 
  39.                         AutoPostBack="True" 
  40.                         onselectedindexchanged="CountryList_SelectedIndexChanged">
  41.                     </asp:DropDownList></td>
  42.             </tr>
  43.         </table>
  44.     
  45.     </div>
  46.         <asp:Button ID="UpdateButton" runat="server" Text="Update" />
  47.         <br /><br />
  48.         
  49.         <asp:Panel ID="BusinessCardPanel" runat="server" Height="116px" Style="border-right: blue thin solid;
  50.              padding-right: 3px; border-top: blue thin solid; padding-left: 3px; font-weight: bold;
  51.              font-size: 14pt; padding-bottom: 3px; border-left: blue thin solid; color: navy;
  52.              padding-top: 3px; border-bottom: blue thin solid; font-family: 'Century Gothic';
  53.              background-color: lightblue" Width="660px">
  54.            <asp:Label ID="BusinessCardNameLabel" runat="server"></asp:Label> <br /> <br />
  55.            <asp:Label ID="BusinessCardTelNumLabel" runat="server"></asp:Label> <br /><br />
  56.            <asp:Label ID="BusinessCardLocationLabel" runat="server"></asp:Label> 
  57.             --<asp:Label ID="BusinessCardCountryLabel" runat="server"></asp:Label>
  58.        </asp:Panel>
  59.     </form>
  60. </body>
  61. </html>

后台EventDemo.aspx.cs

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections.Generic;//使用Dictionary的必需
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     private Dictionary<string, ListItem[]> regionCountryData;//Dictionary 的用法,参看帮助;
  14.     protected override void OnInit(EventArgs e)
  15.     {
  16.         base.OnInit(e);
  17.         regionCountryData = new Dictionary<string, ListItem[]>();
  18.         regionCountryData["Americas"] = new ListItem[] { 
  19.             new ListItem("USA"), 
  20.             new ListItem("Canada"), 
  21.             new ListItem("Brazil"
  22.         };
  23.         regionCountryData["EMEA"] = new ListItem[] { 
  24.             new ListItem("Switzerland"), 
  25.             new ListItem("Dubai"), 
  26.             new ListItem("Nigeria"
  27.         };
  28.         
  29.         regionCountryData["Asia Pacific"] = new ListItem[] { 
  30.             new ListItem("India"), 
  31.             new ListItem("Japan"), 
  32.             new ListItem("Australia"
  33.         };
  34.     }
  35.     protected void Page_Load(object sender, EventArgs e)
  36.     {
  37.         if (!IsPostBack)
  38.         {
  39.             RegionList.Items.Add("---Please select---");
  40.             foreach (string region in regionCountryData.Keys)
  41.             {
  42.                 RegionList.Items.Add(region);
  43.             }
  44.             CountryList.Items.Add("---Please select---");
  45.         }
  46.     }
  47.     protected void RegionList_SelectedIndexChanged(object sender, EventArgs e)
  48.     {
  49.         string region = RegionList.SelectedValue;
  50.         BusinessCardLocationLabel.Text = region;
  51.         CountryList.Items.Clear();
  52.         CountryList.Items.Add("---Please select---");
  53.         CountryList.Items.AddRange(regionCountryData[region]);
  54.     }
  55.     protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
  56.     {
  57.         BusinessCardCountryLabel.Text = CountryList.SelectedItem.ToString();
  58.     }
  59.     protected void NameTextBox_TextChanged(object sender, EventArgs e)
  60.     {
  61.         BusinessCardNameLabel.Text = NameTextBox.Text;
  62.     }
  63.     protected void TelNumTextBox_TextChanged(object sender, EventArgs e)
  64.     {
  65.         BusinessCardTelNumLabel.Text = TelNumTextBox.Text;
  66.     }
  67.    
  68. }

该示例来源于 TotalTraning 的讲座

原创粉丝点击