Communication of Web Web Components--talking about events
Everyone currently developing asp.net programs often use usercontrols that is Pagelet (. ascx file). A lot of people now use it as an include in the ASP. It's natural, of course, but once you take into account the communication between user controls and the container Web form that he is in, or between other user controls, things seem more complicated. For example, I have such a page
User control (Image Button)
User control (left Hyperlink Menu) user control (Main Content) User Control (advertisments)
User control (Additional Links)
If you want to click on the left link, let container dynamically invoke a new user control to the main content location, which requires us to locate the two Web components:
1.Parent Web Form: You should know the layout and navigation messages that all of his load's user controls passed to him on the page
2.User control: Passing messages to parent Web form
Take the example above, how does the parent Web Form know when you clicked on a link in the left Hyperlink menu, or how does the parent Web Form know which link you clicked on? Obviously we should provide a communication mechanism, and the. NET framework already provides such a set of mechanisms--event. (Of course the above examples can be solved with Response.Redirect and request.params, but this is not an optimal solution because the mechanism is tightly coupled and a good component should be highly independent, as I've said before, that events are loosely coupled mechanisms)
Now we're going to look at a piece of code
Using System;
Using System.Web.UI.WebControls;
Namespace Michael.Communication.UserControls.Delegates
{
Using Evengargobjects;
Declaration delegate (Note that delegate is actually an object), typically in C # (object sender, EventArgs e), and sender is an example of the object that raised the event
For example, there is a button in the user control that triggers an event, and that button is sender, not user control.
Delegate can be declared outside of class, or within class, and generally declared outside of class
public delegate void Headereventhandler (object sender, Michaelimageeventargs e);
}
Namespace Michael.Communication.UserControls.Delegates.EventArgObjects
{
Inherit System.EventArgs, providing information when you click the Graphics button
public class MichaelImageEventArgs:System.EventArgs
{
private string imagetextclicked = null;
public string Imagetext
{
get {return imagetextclicked;}
set {imagetextclicked = value. Trim ();}
}
}
}
Note: The code above implements our own delegate and EventArgs
Header.ascx.cs
Namespace Michael.Communication.UserControls
{
Using System;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Michael.Communication.UserControls.Delegates;
Using Michael.Communication.UserControls.Delegates.EventArgObjects;
Public abstract class Header:System.Web.UI.UserControl
{
Declaring an event (triggering an event equivalent to a callback)
public event Headereventhandler Headereventhandler;
public void Image_click (object sender, ImageClickEventArgs e)
{
if (Headereventhandler!= null)
{
ImageButton IB = (ImageButton) sender;
Creating an instance of a Evenargs object
Michaelimageeventargs Michaelimageeventargs = new Michaelimageeventargs ();
michaelimageeventargs.imagetextclicked = Getimagebuttontext (Ib.id, E);
foreach (Headereventhandler h in Headereventhandler.getinvocationlist ())
{
Try
{
Event triggers
H (IB, Michaelimageeventargs);
}
catch (Exception er)
{
Handling accidents
}
}
}
Return
}
Return x,y with ImageButton ImageClickEventArgs to determine which part of the picture to click
private string Getimagebuttontext (String ID, ImageClickEventArgs e)
{
String returnvalue = null;
if (ID = = "Imgnewlogo")
returnvalue = "IBM";
if (ID = = "Imgnewhead")
{
if ((E.y > 0) && (E.y < 35))
returnvalue = "163.com";
Else
returnvalue = "Microsoft";
}
if (ID = = "ImgBanner1")
returnvalue = "8488";
if (ID = = "ImgBanner2" | | ID = = "ImgBanner3")
{
if ((E.y > 0) && (E.y < 21))
returnvalue = "Chinabyte";
if ((E.y >) && (e.y < 36))
returnvalue = "www";
if (E.y > 35)
ReturnValue = (ID = = "ImgBanner2"?) "sina.com.cn": "5460.net");
if (e.x > 160)
ReturnValue = (ID = = "ImgBanner2"?) ' My name is Michael ': ' What is your name ';
}
if (ID = = "ImgBanner4")
{
if ((E.y > 0) && (E.y < 36))
returnvalue = "@163.com";
if (E.y > 35)
returnvalue = "www.163.com";
}
Return returnvalue;
}
}
}
Main Page
Default.aspx
<%@ Page language= "C #" codebehind= "Default.aspx.cs" autoeventwireup= "false" inherits= " Michael.Communication.Default "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD><TITLE> Event Examples </TITLE></HEAD>
<BODY>
<form runat= "Server" >
<asp:table id= "tbllayout" runat= "server" cellspacing= "0" cellpadding= "0" >
<asp:tablerow id= "Row1" runat= "Server" >
<asp:tablecell id= "Cell11" runat= "Server" ></asp:TableCell>
</asp:TableRow>
<asp:tablerow id= "Row2" runat= "Server" >
<asp:tablecell id= "CellTest1" runat= "Server"
Text= "The text of the picture you selected is: (first event triggered)"
Font-name= "Verdana" font-size= "11pt" backcolor= "Gainsboro" borderstyle= "Solid"
Borderwidth= "1px" bordercolor= "Black" horizontalalign= "Center"/>
</asp:TableRow>
<asp:tablecell id= "CellTest2" runat= "Server"
Text= "The text of the picture you selected is: (second event is triggered)"
Font-name= "Verdana" font-size= "11pt" backcolor= "Gainsboro" borderstyle= "Solid"
Borderwidth= "1px" bordercolor= "Black" horizontalalign= "Center"/>
</asp:TableRow>
</asp:Table>
</FORM>
</BODY>
</HTML>
Default.aspx.cs
Using System;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Michael.Communication.UserControls;
Using Michael.Communication.UserControls.Delegates;
Using Michael.Communication.UserControls.Delegates.EventArgObjects;
Namespace Michael.communication
{
public class Default:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TableCell cell11;
protected System.Web.UI.WebControls.TableCell CellTest1;
protected System.Web.UI.WebControls.TableCell CellTest2;
Public Default ()
{
Page.Init + = new System.EventHandler (Page_Init);
}
Triggering events
public void SetText1 (object sender, Michaelimageeventargs e)
{
Celltest1.text = "The picture you choose corresponds to the text is: <b>{" + E.imagetext + "}</b>";
}
Triggering events
public void SetText2 (object sender, Michaelimageeventargs e)
{
Celltest2.text = "The picture you choose corresponds to the text is: <b>{" + E.imagetext + "}</b>";
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.