通过示例看页面处理中的事件
来源:互联网 发布:高薪网络销售招聘 编辑:程序博客网 时间:2024/05/01 21:27
这是一个用来演示ASP.NET页面处理过程时事件发挥的作用的示例,该示例通过动态(有点AJAX味道,但没有使用AJAX框架)的生成一张企业名片,讲述了几个常用事件的用法,更重要的是,里面的一些代码对于新手(比如当前的我)会很有帮助。
前台:EventDemo.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body style="font-size: 10pt; font-family: Arial">
- <form id="form1" runat="server">
- <div>
- <table style="width: 404px">
- <tr>
- <td style="width: 57px">
- <asp:Label ID="NameLabel" runat="server" Text="Name:"></asp:Label></td>
- <td style="width: 238px">
- <asp:TextBox ID="NameTextBox" runat="server" Width="146px" AutoPostBack="True"
- ontextchanged="NameTextBox_TextChanged"></asp:TextBox></td>
- </tr>
- <tr>
- <td style="width: 57px; height: 24px">
- <asp:Label ID="TelNumLabel" runat="server" Text="Tel Num:"></asp:Label></td>
- <td style="width: 238px; height: 24px">
- <asp:TextBox ID="TelNumTextBox" runat="server" Width="146px"
- AutoPostBack="True" ontextchanged="TelNumTextBox_TextChanged"></asp:TextBox></td>
- </tr>
- <tr>
- <td style="width: 57px; height: 24px;">
- <asp:Label ID="RegionLabel" runat="server" Text="Region:"></asp:Label></td>
- <td style="width: 238px; height: 24px;">
- <asp:DropDownList ID="RegionList" runat="server" Width="152px"
- AutoPostBack="True"
- onselectedindexchanged="RegionList_SelectedIndexChanged">
- </asp:DropDownList></td>
- </tr>
- <tr>
- <td style="width: 57px; height: 24px">
- <asp:Label ID="CountryLabel" runat="server" Text="Country:"></asp:Label></td>
- <td style="width: 238px; height: 24px">
- <asp:DropDownList ID="CountryList" runat="server" Width="152px"
- AutoPostBack="True"
- onselectedindexchanged="CountryList_SelectedIndexChanged">
- </asp:DropDownList></td>
- </tr>
- </table>
- </div>
- <asp:Button ID="UpdateButton" runat="server" Text="Update" />
- <br /><br />
- <asp:Panel ID="BusinessCardPanel" runat="server" Height="116px" Style="border-right: blue thin solid;
- padding-right: 3px; border-top: blue thin solid; padding-left: 3px; font-weight: bold;
- font-size: 14pt; padding-bottom: 3px; border-left: blue thin solid; color: navy;
- padding-top: 3px; border-bottom: blue thin solid; font-family: 'Century Gothic';
- background-color: lightblue" Width="660px">
- <asp:Label ID="BusinessCardNameLabel" runat="server"></asp:Label> <br /> <br />
- <asp:Label ID="BusinessCardTelNumLabel" runat="server"></asp:Label> <br /><br />
- <asp:Label ID="BusinessCardLocationLabel" runat="server"></asp:Label>
- --<asp:Label ID="BusinessCardCountryLabel" runat="server"></asp:Label>
- </asp:Panel>
- </form>
- </body>
- </html>
后台EventDemo.aspx.cs
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Collections.Generic;//使用Dictionary的必需
- public partial class _Default : System.Web.UI.Page
- {
- private Dictionary<string, ListItem[]> regionCountryData;//Dictionary 的用法,参看帮助;
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- regionCountryData = new Dictionary<string, ListItem[]>();
- regionCountryData["Americas"] = new ListItem[] {
- new ListItem("USA"),
- new ListItem("Canada"),
- new ListItem("Brazil")
- };
- regionCountryData["EMEA"] = new ListItem[] {
- new ListItem("Switzerland"),
- new ListItem("Dubai"),
- new ListItem("Nigeria")
- };
- regionCountryData["Asia Pacific"] = new ListItem[] {
- new ListItem("India"),
- new ListItem("Japan"),
- new ListItem("Australia")
- };
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- RegionList.Items.Add("---Please select---");
- foreach (string region in regionCountryData.Keys)
- {
- RegionList.Items.Add(region);
- }
- CountryList.Items.Add("---Please select---");
- }
- }
- protected void RegionList_SelectedIndexChanged(object sender, EventArgs e)
- {
- string region = RegionList.SelectedValue;
- BusinessCardLocationLabel.Text = region;
- CountryList.Items.Clear();
- CountryList.Items.Add("---Please select---");
- CountryList.Items.AddRange(regionCountryData[region]);
- }
- protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
- {
- BusinessCardCountryLabel.Text = CountryList.SelectedItem.ToString();
- }
- protected void NameTextBox_TextChanged(object sender, EventArgs e)
- {
- BusinessCardNameLabel.Text = NameTextBox.Text;
- }
- protected void TelNumTextBox_TextChanged(object sender, EventArgs e)
- {
- BusinessCardTelNumLabel.Text = TelNumTextBox.Text;
- }
- }
该示例来源于 TotalTraning 的讲座
- 通过示例看页面处理中的事件
- 【jQuery】jQuery中的事件处理 —— 示例
- ble协议栈cc2540x-1.3.2之通过key处理过程看任务、事件、消息机制
- ble协议栈cc2540x-1.3.2之通过key处理过程看任务、事件、消息机制
- android 初看事件处理
- 通过示例看JavaScript的闭包
- 使用webview加载本地html页面,并处理html页面中的javascript事件
- 通过模板生成静态页面示例
- wxWidgets中的事件处理
- C# 中的事件处理
- qt中的事件处理
- JavaScript中的事件处理
- JavaScript中的事件处理
- C# 中的事件处理
- jsp中的事件处理
- JavaScript中的事件处理
- HandleResourceChange中的事件处理
- JavaScript中的事件处理
- 用带有PTDD Partition Table Doctor的WinPE修复硬盘主引导记录
- -
- tomcat控制台输出信息时会卡住的原因
- 【LeetCode with Python】 LRU Cache
- QQ宠物打工学习对照表
- 通过示例看页面处理中的事件
- ACCESS集锦
- My csdn blog is setup today, welcome to be here.
- 【LeetCode with Python】 Permutations
- 【LeetCode with Python】 Remove Element
- NetAdvantage Infragistics WPF 2008 v1 *SERIALS*
- Eclipse plugin & RCP technology guide (1) - Introduction of Eclipse and Eclipse plugin
- 【LeetCode with Python】 Regular Expression Matching
- java去掉html标签 必须首先去掉 "