我的Web表单页面上有一个用户定义的类型下拉列表(DDL),它继承自System.Web.UI.WebControls.DropDownList。我在页面的代码文件(.aspx.cs)和AppCode中填写我的DDL而不会出现错误。
public static void FillDropDownList(ListControl drp)
{
drp.Items.Clear();
drp.Items.Add(new ListItem("Please select", ""));
drp.Items.Add(new ListItem("False", false.ToString()));
drp.Items.Add(new ListItem("True", true.ToString()));
}
然后我正在初始化我的DDL:
drp.SelectedValue = "False";
初始化在页面的代码文件(aspx.cs)中的填充方法时有效。在AppCode中的Fill方法初始化不起作用,我收到以下错误。
Cannot have multiple items selected in a DropDownList. Please send a copy of this message to the Support.
at System.Web.UI.WebControls.DropDownList.VerifyMultiSelect()
at System.Web.UI.WebControls.ListControl.RenderContents(HtmlTextWriter writer)
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at ASP.fleetnetnew_masterpages_fleetpopuppage_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\fleetintlpdvl\204737b8\cd934b62\App_Web_jw12vu5i.2.cs:line 0
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
我需要使用AppCode来填充DDL,这样我就可以从中心点填充我的DDL。我在这里缺少什么?
答案 0 :(得分:0)
1)在aspx页面中添加下拉列表(使用AppendDataBoundItems)
<asp:DropDownList runat="server" ID="drpTest" AppendDataBoundItems="true" />
2)在页面后面的c#代码中
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillDropDownList(drpTest);
// select False after filling drop down list
drpTest.Items.FindByValue("False").Selected = true;
}
public static void FillDropDownList(ListControl drp)
{
drp.Items.Clear();
drp.Items.Add(new ListItem("Please select", ""));
drp.Items.Add(new ListItem("False", false.ToString()));
drp.Items.Add(new ListItem("True", true.ToString()));
}
}