在后面的每个 asp:DropDownList 控件代码周围添加 DIV [英] add DIV around every asp:DropDownList control code behind
问题描述
我尝试简单地在全局级别(如果可能)围绕 <asp:DropDownList>
类型的每个控件添加一个 div 包装器.在这一点上,我已经用一个 asp 皮肤解决了这个问题,向所有 <asp:DropDownList>
添加了default-select"类,然后 jquery 只为它们每个添加一个 div.
$j(".default-select").wrap("<div class='myClass'></div>");
现在,我的问题.是否可以从代码隐藏中添加此 div 包装器,而不是依赖于 javascript.
控制适配器:我知道这应该可以通过编写一个控制适配器来覆盖 <asp:DropDownList>
渲染方法(如下所述:使用 ).但是,我只想添加一个包装 div,而不是重写 <asp:DropDownList>
控件的整个呈现(如果我覆盖该方法,我必须这样做?).有什么建议?也许有一种方法可以在现有的适配器中添加一些东西??
自定义用户控件:另一种解决方案是使用包装构建自定义 <mycustom:DropDownList>
,但是,这将迫使我替换
贯穿整个项目(大型项目).我宁愿只是更改原始控件的一些方式,以便我的样式适用于所有地方.
总结:是否有一种简单的方法可以让所有
呈现为:
<select><option...></select>
而不仅仅是:
我的第一次尝试(在页面加载时):我尝试在 Page_load 方法中添加此代码,但我找不到任何方法来渲染该 div?
var j = 0;foreach(Page.Controls.OfType()中的DropDownList控件){HtmlGenericControl div = new HtmlGenericControl();div.ID = "div" +j;div.TagName = "div";div.Attributes["class"] = "myClass";div.Controls.Add(控件);//或 control.Controls.Add(div);但这不会包装它.j++;}
您的解决方案几乎有效.服务器控件只能存在于服务器窗体的范围内,您将需要在页面上执行递归搜索或直接在窗体控件集合中查找.一旦您拥有 DropDownList 并将其包裹在 div 容器中,就需要将其添加到控件集合中.
此外,我认为最好在 OnPreInit 中执行此操作.
protected override void OnPreInit(EventArgs e){base.OnPreInit(e);无功j = 0;foreach(form1.Controls.OfType().ToList()中的DropDownList控件){var div = new HtmlGenericControl();div.ID = "div" + j;div.TagName = "div";div.Attributes["class"] = "myClass";div.Controls.Add(控件);//或 control.Controls.Add(div);但这不会包装它.j++;form1.Controls.Add(div);}
}
我希望这会有所帮助.让我知道你的进展如何.
I try to simply add a div wrapper around every control of type <asp:DropDownList>
at a global level (if possible). At this point I have solved it with a asp skin adding "default-select" class to all <asp:DropDownList>
and then jquery just adding a div for each of them.
$j(".default-select").wrap("<div class='myClass'></div>");
Now, my question. Is it possible to add this div wrapper from the code-behind instead of relying on a javascript.
Control Adapter:
I know this should be possible by writing a control adapter that override <asp:DropDownList>
render method (as described here: Dropdownlist control with <optgroup>s for asp.net (webforms)?). However, I just want to add a wrapping div, not rewrite the entire rendering of the <asp:DropDownList>
control (which I have to do if i override the method?). Any suggestions? Maybe there is a way to just add something to the existing adapter??
Custom User Control: Another solution would be to build a custom <mycustom:DropDownList>
with the wrapping, but, this would force me to replace every instance of <asp:DropDownList>
trough the whole project (large project). I rather just change the original control some how so that my styling applies everywhere.
So summary: Is there an easy way to just make all <asp:DropDownList>
render as:
<div class="myClass">
<select><option...></select>
</div>
instead of just:
<select><option...></select>
My first attempt (on Page load): I tried to add this code in the Page_load method but I don't find any way to render that div out?
var j = 0;
foreach (DropDownList control in Page.Controls.OfType<DropDownList>())
{
HtmlGenericControl div = new HtmlGenericControl();
div.ID = "div" +j;
div.TagName = "div";
div.Attributes["class"] = "myClass";
div.Controls.Add(control); // or control.Controls.Add(div); but this wouldn't wrap it.
j++;
}
Your solution just about works. Server control can only exist within the scope of a server form, you will need to perform a recursive search on the page or look directly in the form controls collection. Once you have the DropDownList and wrapped it around a div container it will need to be added to the controls collection.
Also, I think it better to perform this in OnPreInit.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
var j = 0;
foreach (DropDownList control in form1.Controls.OfType<DropDownList>().ToList())
{
var div = new HtmlGenericControl();
div.ID = "div" + j;
div.TagName = "div";
div.Attributes["class"] = "myClass";
div.Controls.Add(control); // or control.Controls.Add(div); but this wouldn't wrap it.
j++;
form1.Controls.Add(div);
}
}
I hope this is helpful. Let me know how you get on.
这篇关于在后面的每个 asp:DropDownList 控件代码周围添加 DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!