Asp.net自定義控件之單選、多選控件
本文實(shí)例為大家分享了Asp.net單選、復(fù)選框控件的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
將常用的jquery插件封裝成控件也是個(gè)不錯(cuò)的選擇。
先看看效果:

1.新建類(lèi)庫(kù)項(xiàng)目,創(chuàng)建數(shù)據(jù)源類(lèi)
[Serializable]
public class Select2Item
{
public bool Selected { get; set; }
public string Text { get; set; }
public string Value { get; set; }
public Select2Item() { }
public Select2Item(string text, string value)
{
this.Text = text;
this.Value = value;
}
public Select2Item(string text, string value, bool selected)
{
this.Text = text;
this.Value = value;
this.Selected = selected;
}
}
2.創(chuàng)建控件類(lèi)CheckList,繼承與WebControl,并定義 public List<Select2Item> Items數(shù)據(jù)項(xiàng)屬性。
3.引入腳本文件及樣式文件
a.創(chuàng)建腳本或樣式文件,設(shè)置文件的屬性-生成操作-嵌入的資源

b.需要在namespace上添加標(biāo)記 [assembly: WebResource("命名空間.文件夾名.文件名", "mime類(lèi)型")]
如:
[assembly: WebResource("Control.Style.checklist.css", "text/css",PerformSubstitution = true)]
[assembly: WebResource("Control.Scripts.checklist.js", "application/x-javascript")]
如果css文件里面存在圖片的話,同樣將圖片設(shè)置為嵌入的資源,在css中的寫(xiě)法為<%=WebResource("命名空間.文件夾名.文件名")%>
PerformSubstitution 表示嵌入式資源的處理過(guò)程中是否分析其他Web 資源 URL,并用到該資源的完整路徑替換。
c.重寫(xiě)protected override void OnPreRender(EventArgs e),引入嵌入的腳本或樣式文件
if(Page!=null) Page.Header.Controls.Add(LiteralControl),將<script><link>標(biāo)簽放到LiteralControl中,然后將LiteralControl添加到Page.Header中,最后在頁(yè)面里你就會(huì)看到引入的<script><link>標(biāo)簽。
protected override void OnPreRender(EventArgs e)
{
if (this.Page != null)
{
StringBuilder sbb = new StringBuilder();
sbb.Append(string.Format(STYLE_TEMPLATE, Page.ClientScript.GetWebResourceUrl(this.GetType(), "HandControl.Style.checklist.css")));
sbb.Append(string.Format(SCRIPT_TEMPLATE, Page.ClientScript.GetWebResourceUrl(this.GetType(), "HandControl.Scripts.checklist.js")));
bool hascss = false;
LiteralControl lcc = new LiteralControl(sbb.ToString());
lcc.ID = "lccheck";
foreach (Control item in Page.Header.Controls)
{
if (item.ID == "lccheck")
hascss = true;
}
if (!hascss)
Page.Header.Controls.Add(lcc);
}
base.OnPreRender(e);
}
4.重寫(xiě)控件的protected override void Render(HtmlTextWriter writer)方法
這里主要是渲染控件的html,根據(jù)你的控件而定。
protected override void Render(HtmlTextWriter writer)
{
if (Items.Count > 0)
{
writer.Write("<div id='div" + this.ClientID + "' class='c01-tag-div' mul='" + (Multiple == true ? "1" : "0") + "'>");
if (Multiple == false)
writer.Write("<input name='tb" + this.ClientID + "' type='hidden' value='" + Items[0].Value + "' />");
else
writer.Write("<input name='tb" + this.ClientID + "' type='hidden' />");
bool first = true;
foreach (var item in Items)
{
if (Multiple == false)
{
if (item.Selected && first)
{
writer.Write("<a title='" + item.Text + "' class='c01-tag-item c01-tag-select' val='" + item.Value + "' tag='Y'>" + item.Text + "</a>");
first = false;
}
else
{
writer.Write("<a title='" + item.Text + "' class='c01-tag-item' val='" + item.Value + "' tag='N'>" + item.Text + "</a>");
}
}
else
{
if (item.Selected)
writer.Write("<a title='" + item.Text + "' class='c01-tag-item c01-tag-select' val='" + item.Value + "' tag='Y'>" + item.Text + "</a>");
else
writer.Write("<a title='" + item.Text + "' class='c01-tag-item' val='" + item.Value + "' tag='N'>" + item.Text + "</a>");
}
}
writer.Write("</div>");
}
}
5.添加GetSelected方法,返回List<Select2Item>,添加GetSelectValue,返回String(多選以,號(hào)隔開(kāi))
public List<Select2Item> GetSelected()
{
if (Page != null)
{
var values = Page.Request.Form["tb" + this.ClientID].Split(',');
var res = Items.Where(t => values.Contains(t.Value)).ToList();
foreach (var item in Items)
{
if (res.Contains(item))
{
item.Selected = true;
}
else
{
item.Selected = false;
}
}
return res;
}
else
{
return null;
}
}
public string GetSelectValue()
{
if (Page != null)
{
return Page.Request.Form["tb" + this.ClientID];
}
return "";
}
6.保存狀態(tài)
你需要重寫(xiě)兩個(gè)方法protected override object SaveViewState() 、protected override void LoadViewState(object savedState),旨在將Items數(shù)據(jù)項(xiàng)屬性保存到ViewState
protected override object SaveViewState()
{
var valuestr = Page.Request.Form["tb" + this.ClientID];
if (!string.IsNullOrEmpty(valuestr))
{
var values = valuestr.Split(',');
var temp = Items.Where(t => values.Contains(t.Value)).ToList();
foreach (var item in temp)
{
item.Selected = true;
}
}
return new object[] { base.SaveViewState(), Items };
}
protected override void LoadViewState(object savedState)
{
object[] vState = (object[])savedState;
if (vState[0] != null)
base.LoadViewState(vState[0]);
if (vState[1] != null)
Items = (List<Select2Item>)vState[1];
}
7.單選和復(fù)選的設(shè)置,在js中控制
添加屬性
[Description("獲取和設(shè)置多選"), DefaultValue(true), Browsable(true), Category("雜項(xiàng)")]
public bool Multiple { get; set; }
在OnPreRender代碼中你會(huì)發(fā)現(xiàn)Multiple屬性會(huì)影響div的mul屬性值,從而判斷是否多選(默認(rèn)多選)
8.其它說(shuō)明
private static readonly string STYLE_TEMPLATE = "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />\r\n";
private static readonly string SCRIPT_TEMPLATE = "<script type=\"text/javascript\" src=\"{0}\"></script>\r\n";
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- asp.net自定義控件代碼學(xué)習(xí)筆記
- asp.net 自定義控件實(shí)現(xiàn)無(wú)刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
- Asp.net 動(dòng)態(tài)加載用戶(hù)自定義控件,并轉(zhuǎn)換成HTML代碼
- asp.net DropDownList自定義控件,讓你的分類(lèi)更清晰
- 關(guān)于asp.net 自定義分頁(yè)控件
- ASP.NET單選按鈕控件RadioButton常用屬性和方法介紹
- 淺談ASP.NET中最簡(jiǎn)單的自定義控件
- ASP.NET中 CheckBox復(fù)選框控件的使用
- ASP.NET中CheckBoxList復(fù)選框列表控件詳細(xì)使用方法
- ASP.NET中 RadioButtonList 單選按鈕組控件的使用方法
相關(guān)文章
.NET發(fā)送郵件的實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于.NET發(fā)送郵件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
ASP.NET 水晶報(bào)表打印功能實(shí)現(xiàn)代碼
ASP.NET下的水晶報(bào)表打印,據(jù)我所知有以下幾種辦法可以打印2008-11-11
HttpRequest的QueryString屬性 的一點(diǎn)認(rèn)識(shí)
我們開(kāi)發(fā)asp.net程序獲取QueryString時(shí),經(jīng)常性的遇到一些url編碼問(wèn)題2012-11-11
把a(bǔ)spx頁(yè)面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼
把a(bǔ)spx頁(yè)面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼,主要是利于搜索引擎的收錄。2011-10-10
.NET的動(dòng)態(tài)編譯與WS服務(wù)調(diào)用詳解
這篇文章介紹了.NET的動(dòng)態(tài)編譯與WS服務(wù)調(diào)用詳解,有需要的朋友可以參考一下,希望對(duì)你有所幫助2013-07-07
ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤(pán)之文件夾實(shí)現(xiàn)
ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤(pán)之文件夾實(shí)現(xiàn)...2006-09-09
簡(jiǎn)單Web service 身份驗(yàn)證解決方案
問(wèn)題提出:為了構(gòu)建一個(gè)安全的web服務(wù),需要對(duì)調(diào)用方進(jìn)行身份驗(yàn)證,只讓通過(guò)審核的用戶(hù)調(diào)用web服務(wù)。2009-05-05
如何利用擴(kuò)展方法來(lái)鏈?zhǔn)降膶?duì)MVC 3中的頁(yè)面進(jìn)行驗(yàn)證
雖然擴(kuò)展方法只是改變了我們寫(xiě)代碼的方式,但是如果我們使用得當(dāng),可以給我們帶來(lái)巨大的編碼效率的提升接下來(lái)介紹通過(guò)擴(kuò)展方法(鏈?zhǔn)椒椒ǎ镸VC 3視圖添加驗(yàn)證2013-01-01
Asp.net中使用文本框的值動(dòng)態(tài)生成控件的方法
這篇文章主要介紹了Asp.net中使用文本框的值動(dòng)態(tài)生成控件的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05

