日期:2014-05-18  浏览次数:20396 次

自定义控件如何在生成控件外套一层div
我想生成一个dropdownlist自定义控件,然后在外面套层div
C# code

 protected override void OnLoad(EventArgs e)
        {
            _DropDownList = new DropDownList();
            _DropDownList.DataSource = _ddlSource;
            
            _DropDownList.DataTextField = _textKeyName;//可用属性来获取
            _DropDownList.DataValueField = _valueKeyName;//可用属性来获取
            _DropDownList.DataBind();
            base.OnLoad(e);
        }

 #region RenderContents
        protected override void RenderContents(HtmlTextWriter output)
        {
            string dwidth = _divWidth.ToString() + "px";
            output.Write("<div  class=\"ui-widget\">");
            _DropDownList.Width = Unit.Parse(dwidth);
            _DropDownList.RenderControl(output);
            output.Write("</div>");
        }
        #endregion





这样写回生成这样东西
<select name="ctl00$MainContent$auroList" id="MainContent_auroList" onkeydown="javascript:if (event.keyCode == 13) {event.returnValue = false;}">
<div class="ui-widget"><select style="width:175px;">
<option value="1">项 1</option>
<option value="2">项 2</option>
<option value="3">项 3</option>
<option value="4">项 4</option>
<option value="5">项 5</option>
<option value="6">项 6</option>
<option value="7">项 7</option>
<option value="8">项 8</option>
<option value="9">项 9</option>

</select></div>
</select>

我想生成的是

<div class="ui-widget">
<select name="ctl00$MainContent$auroList" id="MainContent_auroList" style="width:175px;">
<option value="1">项 1</option>
<option value="2">项 2</option>
<option value="3">项 3</option>
<option value="4">项 4</option>
<option value="5">项 5</option>
<option value="6">项 6</option>
<option value="7">项 7</option>
<option value="8">项 8</option>
<option value="9">项 9</option>

</select></div>

这个自定义控件内要怎么写啊?





------解决方案--------------------
protected override void Render(HtmlTextWriter output)
{
string dwidth = _divWidth.ToString() + "px";
output.Write("<div class=\"ui-widget\">");
_DropDownList.Width = Unit.Parse(dwidth);
base.Render(output);
output.Write("</div>");
}