CSTrüter HomeArticlesDownloadsAbout meContact me
How to enable the filestream feature in SQL 2008 - Alternative way to store blobs(files) via SQL 2010-08-21 19:31:56
How to create a Singleton Pattern in C# 2010-08-10 22:52:52
How to prevent that threads access shared resources concurrently via Monitor. 2010-08-06 15:31:15
A quick review of the book PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide written by Larry Ullman 2010-08-04 21:48:58
How to prevent that threads access shared resources concurrently via Mutex. 2010-08-03 14:42:36
How to stop propagation of javascript events 2010-07-25 21:59:29
Post about how Pete the web developer fixed his sitemap 2010-07-17 15:12:02
How to setup an out of process session service 2010-07-08 17:51:46
How to display/add images from/to a SQL Database 2010-07-04 23:15:15
How to register a custom URL protocol handler 2010-06-28 20:34:01
Creating a WYSIWYG textbox for your website is actually quite simple. 2007-02-01 12:00:00
Move items between two listboxes in ASP.net(C#, VB.NET) and PHP 2008-06-12 17:07:43
Firefox word wrapping issues 2008-06-09 09:51:21
Populate a TreeView control in a windows application. 2009-08-27 16:01:03
2007-02-22 12:00:00
Creating custom controls that expose events can potentially prove to be quite tedious if we're presented with examples like the following: IPostBackDataHandler IPostBackEventHandler Notice the following code, from the example you'll find on the MSDN links above: public class MyButton: Control, IPostBackEventHandler { public event EventHandler Click; protected virtual void OnClick(EventArgs e) { if (Click != null) { Click(this, e); } } public void RaisePostBackEvent(string eventArgument){ OnClick(new EventArgs()); } protected override void Render(HtmlTextWriter output) { output.Write("<INPUT TYPE = submit name = " + this.UniqueID + " Value = 'Click Me' />"); } } You will notice the implementation of the IPostBackEventHandler interface, which requires us to create the RaisePostBackEvent method. But more interestingly, observe the render method where raw HTML are passed to the HtmlTextWriter instance. Ideally something like the next snippet would make more sense, particularly in dealing with larger more complex controls - there is however a reason why the MSDN example was written in that manner. HtmlInputSubmit _control = new HtmlInputSubmit() { Name = this.UniqueID }; _control.RenderControl(output); One would find that all of a sudden the click event doesn't fire anymore, simply because the control would now contain a child control that needs to propagate its events to the parent control - since the MSDN example isn't using any child controls, the example should work just fine. (Personally I feel its a bit of a pointless example.) ASP.net provides a technique called event bubbling to aid us in the propagation of these events. Observe the following snippet: public class SearchBox : CompositeControl { public event EventHandler Click; TextBox _TextBox = new TextBox(); Button _Button = new Button() { CommandName = "Click", Text = "Search" }; [Browsable(true)] [Category("Appearance")] public string Text { get { return _TextBox.Text; } set { _TextBox.Text = value; } } protected virtual void OnClick(EventArgs e) { if (Click != null) { Click(this, e); } } protected override bool OnBubbleEvent(object source, EventArgs args) { CommandEventArgs _CommandEventArgs = args as CommandEventArgs; if (_CommandEventArgs != null) { if (_CommandEventArgs.CommandName == "Click") { OnClick(args); return true; } } return false; } protected override void CreateChildControls() { this.Controls.Add(_TextBox); this.Controls.Add(_Button); base.CreateChildControls(); } } In the preceding snippet: Inherit from the CompositeControl class; which is like a base class for controls that have child controls. Added two child controls, textbox and button, and assign a CommandName to the button, which will be used in our "bubble". Override the OnBubbleEvent, where we check if the appropriate CommandName was raised - in this case "Click", which raises any event handlers attached to the button. To read more about "Bubbling an Event" click here.
public class MyButton: Control, IPostBackEventHandler { public event EventHandler Click; protected virtual void OnClick(EventArgs e) { if (Click != null) { Click(this, e); } } public void RaisePostBackEvent(string eventArgument){ OnClick(new EventArgs()); } protected override void Render(HtmlTextWriter output) { output.Write("<INPUT TYPE = submit name = " + this.UniqueID + " Value = 'Click Me' />"); } }
HtmlInputSubmit _control = new HtmlInputSubmit() { Name = this.UniqueID }; _control.RenderControl(output);
public class SearchBox : CompositeControl { public event EventHandler Click; TextBox _TextBox = new TextBox(); Button _Button = new Button() { CommandName = "Click", Text = "Search" }; [Browsable(true)] [Category("Appearance")] public string Text { get { return _TextBox.Text; } set { _TextBox.Text = value; } } protected virtual void OnClick(EventArgs e) { if (Click != null) { Click(this, e); } } protected override bool OnBubbleEvent(object source, EventArgs args) { CommandEventArgs _CommandEventArgs = args as CommandEventArgs; if (_CommandEventArgs != null) { if (_CommandEventArgs.CommandName == "Click") { OnClick(args); return true; } } return false; } protected override void CreateChildControls() { this.Controls.Add(_TextBox); this.Controls.Add(_Button); base.CreateChildControls(); } }
The company I am currently working for as software developer.
Collection of C# snippets 2010-05-22 01:06:19
Collection of MS SQL snippets 2010-05-22 00:55:15
Collection of JavaScript snippets 2010-05-22 00:37:57
Collection of ASP.net snippets 2010-05-22 00:29:56
Collection of PHP snippets 2010-05-22 00:06:45
a Parallel reference of programming languages 2009-09-10 12:48:23
a tutorial explaining how to develop a simple login using PHP and MySQL 2009-09-05 18:26:47
An article looking at adding some kind of event driven model to PHP 5 2008-07-28 12:48:09
It is very simple creating your own rss reader, the following article looks at a few methods of doing this. 2008-06-23 13:18:25
A quick reference about working with dropdown boxes (select element) in javascript. 2007-02-17 16:36:41
Collection of funny programming articles 2006-10-08 14:23:43