您的位置:首页 > 编程语言 > ASP

ASP.NET学习笔记

2009-04-15 09:49 344 查看
在学习ASP.NET的过程中,总是遇到不懂的地方,每次到网上查了之后,总会想过后能再温习下,为了方便自己温习,也为了能让大家一起分享,我将网上找到的一些资料帖到博客上。
首先,我将转载一些ASP.NET的类介绍。
1、按钮的command事件涉及到的类和属性。
①、 CommandEventArgs 类为 Command 事件提供数据。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)


语法

Visual Basic(声明)
Public Class CommandEventArgs
Inherits EventArgs


Visual Basic(用法)
Dim instance As CommandEventArgs


C#
public class CommandEventArgs : EventArgs


C++
public ref class CommandEventArgs : public EventArgs


J#
public class CommandEventArgs extends EventArgs


JScript
public class CommandEventArgs extends EventArgs
备注 当单击 ButtonImageButton 控件时,会引发 Command 事件。CommandEventArgs 对象包含有关可以在事件处理程序中访问的 Command 事件的数据。使用在 CommandEventArgs 类中可用的属性,可以确定要执行的命令的名称,例如 Sort;还可以确定对该命令加以补充的可选参数,例如 Ascending 示例下面的示例展示如何访问 CommandEventArgs 中的属性以确定要执行的命令。此示例使用 Button 控件的 OnClick 属性,以声明方式指定事件处理程序。C#代码:<%@ Page Language="C#" AutoEventWireup="True" %><html>
<head>   <script runat="server">      void CommandBtn_Click(Object sender, CommandEventArgs e)
{         switch(e.CommandName)
{
case "Sort":
// Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break;

case "Submit":
// Display a message for the Submit button being clicked.
Message.Text = "You clicked the Submit button";
// Test whether the command argument is an empty string ("").
if((String)e.CommandArgument == "")
{
// End the message.
Message.Text += ".";
}
else
{
// Display an error message for the command argument.
Message.Text += ", however the command argument is not recogized.";
}
break;
default:
// The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized.";
break;
}
}
void Sort_List(string commandArgument)
{

switch(commandArgument)
{

case "Ascending":

// Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button.";
break;

case "Descending":

// Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button.";
break;

default:

// The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized.";
break;

}
}
</script>
</head>

<body>

<form runat="server">
<h3>Button CommandName Example</h3>
Click on one of the command buttons.
<br><br>

<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>

 
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>

<br><br>
<asp:Button id="Button3"
Text="Submit"
CommandName="Submit"
OnCommand="CommandBtn_Click"
runat="server"/>

 
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>

 
<asp:Button id="Button5"
Text="Submit Unknown Command Argument"
CommandName="Submit"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>

<br><br>

<asp:Label id="Message" runat="server"/>

</form>
</body>
</html>

②、CommandEventArgs.CommandArgument 属性

获取命令的参数。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
CommandArgument 可以包含由程序员设置的任何字符串。CommandArgument 属性通过允许您为该命令提供任何附加信息,对 CommandName 属性加以补充。例如,可以将 CommandName 属性设置为 Sort 并将 CommandArgument 属性设置为 Ascending,以指定按升序排序的命令。

③、CommandEventArgs.CommandName 属性

获取命令的名称。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
使用 CommandName 属性确定要执行的命令。CommandName 属性可以包含由程序员设置的任何字符串。程序员然后可以在代码中标识该命令名并执行适当的任务。

2、图像热区ImageMap涉及到的类和属性。

①、ImageMapEventArgs 类,为 ImageMap 控件的 Click 事件提供数据。

命名空间: System.Web.UI.WebControls
程序集: System.Web(在 System.Web.dll 中)
示例
<%@ page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void VoteMap_Clicked (Object sender, ImageMapEventArgs e)
{
string coordinates;
string hotSpotType;
int yescount = ((ViewState["yescount"] != null)? (int)ViewState["yescount"] : 0);
int nocount = ((ViewState["nocount"] != null)? (int)ViewState["nocount"] : 0);
// When a user clicks the "Yes" hot spot,
// display the hot spot's name and coordinates.
if (e.PostBackValue.Contains("Yes"))
{
yescount += 1;
coordinates = Vote.HotSpots[0].GetCoordinates();
hotSpotType = Vote.HotSpots[0].ToString ();
Message1.Text = "You selected " + hotSpotType + " " + e.PostBackValue + ".<br />" +
"The coordinates are " + coordinates + ".<br />" +
"The current vote count is " + yescount.ToString() +
" yes votes and " + nocount.ToString() + " no votes.";
}
// When a user clicks the "No" hot spot,
// display the hot spot's name and coordinates.
else if (e.PostBackValue.Contains("No"))
{
nocount += 1;
coordinates = Vote.HotSpots[1].GetCoordinates();
hotSpotType = Vote.HotSpots[1].ToString ();
Message1.Text = "You selected " + hotSpotType + " " + e.PostBackValue + ".<br />" +
"The coordinates are " + coordinates + ".<br />" +
"The current vote count is " + yescount.ToString() +
" yes votes and " + nocount.ToString() + " no votes.";
}
else
{
Message1.Text = "You did not click a valid hot spot region.";
}
ViewState["yescount"] = yescount;
ViewState["nocount"] = nocount;
}
</script>
<html >
<head id="head1" runat="server">
<title>ImageMap Class Post Back Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ImageMap Class Post Back Example</h3>
<asp:imagemap id="Vote"
imageurl="Images/VoteImage.jpg"
width="400"
height="200"
alternatetext="Vote Yes or No"
hotspotmode="PostBack"
onclick="VoteMap_Clicked"
runat="Server">
<asp:RectangleHotSpot
top="0"
left="0"
bottom="200"
right="200"
postbackvalue="Yes"
alternatetext="Vote yes">
</asp:RectangleHotSpot>
<asp:RectangleHotSpot
top="0"
left="201"
bottom="200"
right="400"
postbackvalue="No"
alternatetext="Vote no">
</asp:RectangleHotSpot>
</asp:imagemap>
<br /><br />
<asp:label id="Message1"
runat="Server">
</asp:label>
</form>
</body>
</html>

②、ImageMapEventArgs.PostBackValue 属性 ,获取 String,它分配给单击的 HotSpot 对象的 PostBackValue 属性。

命名空间: System.Web.UI.WebControls
程序集: System.Web(在 System.Web.dll 中)
使用该属性以编程方式访问单击的 ImageMap 控件中 HotSpot 对象的 PostBackValue 属性。

此资料转载自http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.commandeventargs(VS.80).aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: