0

我的网页表单页面上有一个用户定义的下拉列表(DDL)类型,它继承自 System.Web.UI.WebControls.DropDownList。我像这样在 Page 的代码文件 (.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";

当 Page 的代码文件 (aspx.cs) 中的 Fill 方法时,初始化工作。当 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。我在这里想念什么?

4

1 回答 1

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()));
        }
    }
于 2014-10-20T15:04:28.847 回答