问题描述
我有一个的DropDownList
是与在ASPX页面中的数据源
相关联。我需要在页面加载增加一个项目。
I have a DropDownList
that is associated with a DataSource
in the aspx page. I need to add one more item when the page is loaded.
我的code:
<asp:LabelDropDownList ID="ddlVisualTemplate" runat="server" LabelText="Visual Template:" DataSourceID="VisualTemplateDataSource" DataTextField="Name" DataValueField="Id" AutoPostBack="true" OnSelectedIndexChanged="ddlVisualTemplate_SelectedIndexChanged"/>
<asp:EntityDataSource ID="VisualTemplateDataSource" runat="server"
ConnectionString="name=Entities"
DefaultContainerName="Entities" EnableFlattening="False"
EntitySetName="tbEmailVisualTemplates">
和我想一个附加项目,给它:
And I am trying to an extra item to it:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlVisualTemplate.Items.Add(new ListItem("None", string.Empty));
}
}
如果我调试code,它通过它。但是,当显示的页面下拉列表中不包含无
。
If I debug the code, it goes through it. But When the page is displayed dropdown doesn't contain "None"
.
推荐答案
这是最有可能是因为你之前加入该项目的的DataBind()
。如果您想要添加项目以
It's most probably because you're adding the item before the DataBind()
. If you want to add an item with
ddlVisualTemplate.Items.Add()
那么你必须做的 下拉被绑定后。
如果你看一下http://msdn.microsoft.com/en-us/library/ms178472.aspx随后的DataBind正在做preRenderComplete。所以,你必须增加后preRenderComplete发生一些事件的元素。或者你可以做到这一点的 ddlVisualTemplate.DataBound
事件。
If you look at http://msdn.microsoft.com/en-us/library/ms178472.aspx then DataBind is being done in PreRenderComplete. So you have to add the element in some event that occurs after PreRenderComplete.Or you could do it on the ddlVisualTemplate.DataBound
event.
这篇关于从C#DROPDOWNLIST数据源,并增加额外的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!