Implementing events and automatically saving values in Web custom controls

Source: Internet
Author: User
Tags bool constructor empty unique id
web| control using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.Collections.Specialized;
Namespace Mywebcontrols
{
<summary>
Create a class that derives from WebControl
Implements a public constructor that calls the base class constructor to specify that the server control should output an INPUT element
Overrides the AddAttributesToRender method, which is invoked to allow derived classes to add attributes to the root element input
We will add a Name property whose value is derived from the UniqueID attribute, which asp.net use to store a unique ID for each control.
</summary>
[Defaultproperty ("Text"),
ToolBoxData ("<{0}:mytextbox runat=server></{0}:mytextbox>")]
public class Mytextbox:system.web.ui.webcontrols.webcontrol,ipostbackdatahandler
{
Public MyTextBox (): Base ("input")
{
}

Use the ViewState object to save the value, and the valid range for this object is accessible to the current page. Eventually saved to the client. Each time it's going to be loopback
ViewState is a StateBag class that can hold data types that have int bool string or array and other basic data types, and arraylist,hashtable,
or type with type converter, the type that can be serial
public string Text
{
Get
{
if (viewstate["value"]==null)
{
return String.Empty;
}
Return (String) viewstate["value"];
}
Set
{
viewstate["Value"]=value;
}
}

protected override void AddAttributesToRender (HtmlTextWriter writer)
{
Base. AddAttributesToRender (writer);
Writer. AddAttribute (Htmltextwriterattribute.name,uniqueid);
Writer. AddAttribute ("type", "text");
if (text!=null)
Writer. AddAttribute ("value", Text);
}

#region IPostBackDataHandler Members

To access the loopback data, the server control has two methods to implement the IPostBackDataHandler interface
public void RaisePostDataChangedEvent ()
{
event occurs if the data that the user is echoing has changed
if (onmytextchnaged!=null)
{
Onmytextchnaged (This,eventargs.empty);
}
}

This method is invoked when there is a loopback occurrence and a control has loopback data, which is called sequentially for all controls on the page that require access to the loopback data.
If this method returns True, the Raisepostdatachangedmethod will be invoked after the LoadPostData method has been called for all other controls with loopback data on the page.
If the return is false, it is not invoked. Because raising an event in this method can cause unpredictable results, you must raise an event in RaisePostDataChangedEvent.
//
public bool LoadPostData (string postdatakey, NameValueCollection postcollection)
{
BOOL Raiseevent=false; Flag to trigger an event
If the last text is different from the loopback text
if (Text!=postcollection[postdatakey])
{
Raiseevent=true;
text=postcollection[postdatakey];//to save the loopback value
}
return RaiseEvent;
}

#endregion

Register an event, text change event
public event EventHandler Onmytextchnaged;


}
}





Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.