Optimizing ASP.net controls with control adapters

Source: Internet
Author: User
Asp.net| Controls | optimizing

Sometimes the HTML code generated by the default state of the ASP.net control does not meet some specific needs. Like what

We want the user to make some choices that can be easily implemented with the following code

<asp:checkboxlist runat= "Server" >
<asp:listitem text= "one"/>
<asp:listitem text= "two"/>
<asp:listitem text= "Three"/>
</asp:CheckBoxList>

By default, CheckBoxList will put these options in a Table tab, but there may be cases where a table is not suitable for use, and a un-ordered list (UL) is required. Of course we can write a new control that inherits from CheckBoxList, but it's easier to use the controls Adpater, and there are some additional benefits.

First look at the implementation:

1, write a class that inherits from WebControlAdapter, as follows

Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.Adapters;
public class Radiobuttonlistadapter:webcontroladapter
{
protected override void Render (HtmlTextWriter writer)
{
ListControl Targetcontrol = this. Control as ListControl;
If the control so this adapter be pointing to isn't
A ListControl (RadioButtonList or CheckBoxList) then
We don ' t want to change the rendering.
if (Targetcontrol = null | | Targetcontrol is irepeatinfouser = = False)
{
Base. Render (writer);
Return
}
Writer. WriteBeginTag ("ul");
if (TargetControl.CssClass.Length > 0)
{
Writer. WriteAttribute ("Class", Targetcontrol.cssclass);
}
Writer. Write (">");
IRepeatInfoUser repeaterinfo = (irepeatinfouser) this. control;
for (int i = 0; i < TargetControl.Items.Count; i++)
{
Writer. WriteFullBeginTag ("Li");
Repeaterinfo.renderitem (ListItemType.Item, I, New RepeatInfo (), writer);
Writer. Writeendtag ("Li");
}
Writer. Writeendtag ("ul");
}
}

2, create a new ASP.net folder app_browsers in which to add a. browser file, add the following

<browsers>
<browser refid= "Default" >
<controlAdapters>
<adapter controltype= "System.Web.UI.WebControls.CheckBoxList"
Adaptertype= "Radiobuttonlistadapter"/>
<adapter controltype= "System.Web.UI.WebControls.RadioButtonList"
Adaptertype= "Radiobuttonlistadapter"/>
</controlAdapters>
</browser>
</browsers>

Okay, everything's OK. Did you notice? We haven't changed the previous ASP.net code. This is a very important benefit.



Related Article

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.