在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文实例为大家分享了Asp.net单选、复选框控件的具体实现代码,供大家参考,具体内容如下 将常用的jquery插件封装成控件也是个不错的选择。 先看看效果: 1.新建类库项目,创建数据源类 [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.创建控件类CheckList,继承与WebControl,并定义 public List<Select2Item> Items数据项属性。 3.引入脚本文件及样式文件
b.需要在namespace上添加标记 [assembly: WebResource("命名空间.文件夹名.文件名", "mime类型")] 如果css文件里面存在图片的话,同样将图片设置为嵌入的资源,在css中的写法为<%=WebResource("命名空间.文件夹名.文件名")%> 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.重写控件的protected override void Render(HtmlTextWriter writer)方法 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(多选以,号隔开) 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.保存状态 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.单选和复选的设置,在js中控制 效果图: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持极客世界。 |
请发表评论