Asp.net自定义控件之单选、多选控件
在ASP.NET中,我们可以使用自定义控件来实现复杂的功能和界面。其中单选和多选控件是非常常用的控件,我们可以通过自定义控件的方式来实现它们的功能。
创建自定义控件
我们可以通过继承WebControl类来创建自定义控件。以下是单选、多选控件的基础结构:
public class RadioButtonList : WebControl, INamingContainer
{
public RadioButtonList()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
//创建控件
}
protected override void Render(HtmlTextWriter writer)
{
//渲染控件
}
}
public class CheckBoxList : WebControl, INamingContainer
{
public CheckBoxList()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
//创建控件
}
protected override void Render(HtmlTextWriter writer)
{
//渲染控件
}
}
创建单选控件示例
下面是一个简单的单选控件示例:
public class RadioButtonList : WebControl, INamingContainer
{
public RadioButtonList()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
for (int i = 0; i < Items.Count; i++)
{
RadioButton radioButton = new RadioButton();
radioButton.ID = string.Format("RadioButton{0}", i);
radioButton.Text = Items[i].Text;
radioButton.GroupName = UniqueID;
radioButton.Attributes.Add("value", Items[i].Value);
if (SelectedIndex == i)
{
radioButton.Checked = true;
}
Controls.Add(radioButton);
LiteralControl space = new LiteralControl(" ");
Controls.Add(space);
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<div>");
for (int i = 0; i < Controls.Count; i++)
{
Controls[i].RenderControl(writer);
}
writer.Write("</div>");
}
public ListItemCollection Items
{
get
{
if (ViewState["Items"] == null)
{
ViewState["Items"] = new ListItemCollection();
}
return (ListItemCollection)ViewState["Items"];
}
}
public int SelectedIndex
{
get
{
if (ViewState["SelectedIndex"] == null)
{
ViewState["SelectedIndex"] = -1;
}
return (int)ViewState["SelectedIndex"];
}
set
{
ViewState["SelectedIndex"] = value;
}
}
}
使用方法:
<cc1:RadioButtonList ID="RadioButtonList1" runat="server">
<Items>
<asp:ListItem Text="RadioButton 1" Value="1" />
<asp:ListItem Text="RadioButton 2" Value="2" />
<asp:ListItem Text="RadioButton 3" Value="3" />
</Items>
<SelectedIndex>1</SelectedIndex>
</cc1:RadioButtonList>
创建多选控件示例
下面是一个简单的多选控件示例:
public class CheckBoxList : WebControl, INamingContainer
{
public CheckBoxList()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
for (int i = 0; i < Items.Count; i++)
{
CheckBox checkBox = new CheckBox();
checkBox.ID = string.Format("CheckBox{0}", i);
checkBox.Text = Items[i].Text;
checkBox.Attributes.Add("value", Items[i].Value);
if (SelectedIndices.Contains(i))
{
checkBox.Checked = true;
}
Controls.Add(checkBox);
LiteralControl space = new LiteralControl(" ");
Controls.Add(space);
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<div>");
for (int i = 0; i < Controls.Count; i++)
{
Controls[i].RenderControl(writer);
}
writer.Write("</div>");
}
public ListItemCollection Items
{
get
{
if (ViewState["Items"] == null)
{
ViewState["Items"] = new ListItemCollection();
}
return (ListItemCollection)ViewState["Items"];
}
}
public List<int> SelectedIndices
{
get
{
if (ViewState["SelectedIndices"] == null)
{
ViewState["SelectedIndices"] = new List<int>();
}
return (List<int>)ViewState["SelectedIndices"];
}
set
{
ViewState["SelectedIndices"] = value;
}
}
}
使用方法:
<cc1:CheckBoxList ID="CheckBoxList1" runat="server">
<Items>
<asp:ListItem Text="CheckBox 1" Value="1" />
<asp:ListItem Text="CheckBox 2" Value="2" />
<asp:ListItem Text="CheckBox 3" Value="3" />
</Items>
<SelectedIndices>
<asp:ListItem Value="1" />
</SelectedIndices>
</cc1:CheckBoxList>
以上就是单选、多选自定义控件的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Asp.net自定义控件之单选、多选控件 - Python技术站