Design idea of a custom asp.net checkbox

Source: Internet
Author: User
Asp.net| design someone wants to ask, you crazy, CheckBox control also write you rewrite it? No way, recently to do a asp.net project, write development template, you need to do so! My templates need to be initialized with JavaScript control values and state in the foreground, require a lot of special HTML attribute, record some control parameters, but the control provided by Ms does not have these properties, so even the checkbox has to do it yourself! The requirements for my checkbox are:

1: Contains all Ms CheckBox property methods and Events

2: In addition, you want to add custom attributes such as Enableflag,witowner to your own defined checkbox

The problems and solutions are as follows:

Problem: If you inherit the checkbox for Ms directly, add a custom property to the control using This.attributes.add ("Witowner", "true"), and the property is added to the <span> on the control's production. This property is not added to the checkbox to control the checkbox at the foreground and cannot get the property in JavaScript.

Solution: If inherit WebControl, then cover render method, in render, with similar Chkcheckbox.rendercontrol (writer); Statement to add HtmlInputCheckBox, plus a text label, you should be able to simulate the MS checkbox in a way that conforms to the control, and you can define the function of the property yourself. However, this time the problem is: unable to put the control in the <span></span>, set the control background color and other properties, can not be effective for the control! Solution:

To meet the above requirements, the control inherits WebControl, overrides RenderContents, and the syntax is as follows

<summary>
To draw the contents of a control, that is, to draw a span containing part, written in the RenderContents function
Render of a child control, you can allow a composite control span to contain child controls to write foreground script operations.
</summary>
<param name= "Writer" ></param>
protected override void RenderContents (HtmlTextWriter writer)
{
Setgroupattributes ();

if (this.textalign==textalign.right)
{
Chkcheckbox.id=this.clientid+ "_checkbox";

Chkcheckbox.rendercontrol (writer);
 
Writer.write ("<label for= '" +chkcheckbox.id+ "' >" +this.text+ "</label>");
}
Else
{
Writer.write ("<label for= '" +chkcheckbox.id+ "' >" +this.text+ "</label>");
Chkcheckbox.id=this.clientid+ "_checkbox";
Chkcheckbox.rendercontrol (writer);
}
}

private void Setgroupattributes ()
{
All Cswit controls need to have this property
Chkcheckbox.attributes.add ("Witowner", "true");
}

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.