您可以编写一个自定义服务器控件,并使用包含以 | 分隔的文本和区域的数据源 。 然后在使用时将其拆分。
[ToolboxData("<{0}:CustomDropDownList runat=server></{0}:CustomDropDownList>")]public class CustomDropDownList : DropDownList{ protected override void RenderContents(HtmlTextWriter writer) { if (this.Items.Count > 0) { bool selected = false; bool optGroupStarted = false; string lastOptionGroup = string.Empty; for (int i = 0; i < this.Items.Count; i++) { ListItem item = this.Items[i]; if (item.Enabled) { if (lastOptionGroup != item.Text.Split("|")[1]) { if (optGroupStarted) { writer.WriteEndTag("optgroup"); } lastOptionGroup = item.Text.Split("|")[1]; writer.WriteBeginTag("optgroup"); writer.WriteAttribute("label", lastOptionGroup); writer.Write('>'); writer.WriteLine(); optGroupStarted = true; } writer.WriteBeginTag("option"); if (item.Selected) { if (selected) { this.VerifyMultiSelect(); } selected = true; writer.WriteAttribute("selected", "selected"); } writer.WriteAttribute("value", item.Value, true); if (item.Attributes.Count > 0) { item.Attributes.Render(writer); } if (this.Page != null) { this.Page.Clientscript.RegisterForEventValidation(this.UniqueID, item.Value); } writer.Write('>'); HttpUtility.HtmlEnpre(item.Text.Split("|")[0], writer); writer.WriteEndTag("option"); writer.WriteLine(); } } if (optGroupStarted) { writer.WriteEndTag("optgroup"); } } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)