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.