【发布时间】:2014-06-09 05:37:32
【问题描述】:
根据标题,我有一个列表视图,并且在 InsertItemTemplate 我有一个下拉列表。我想在下拉列表中添加空选项作为第一个选项,但似乎没有成功。
我的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListAccountType(); // This retrieves the available account types and save into a ViewState
...
BindAccountCode(true);
}
}
// This function binds the listview containing the insert item template
private void BindAccountCode(bool QueryDB = false)
{
// Retrieve the account code and save into a ViewState
...
// Bind the listview
listviewAccountCode.DataSource = (DataTable)ViewState[ACCT];
listviewAccountCode.DataBind();
....
}
protected void listviewAccountCode_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.InsertItem)
BindAccountType((DropDownList)listviewAccountCode.InsertItem.FindControl("ddlAccountType"), true); // ddlAccountType is dropdownlist in the InsertItemTemplate that I want to add the empty option.
}
// This will populate the dropdownlist
private void BindAccountType(DropDownList ddl, bool addEmptyRow = false)
{
ddl.DataSource = (DataTable)ViewState[ACCTTYPE]; // this retrieves the account type that we already have after the Page_Load function.
...
ddl.DataBind(); // bind the data
if (addEmptyRow) // here I want to add the empty option as the first option in the dropdownlist.
{
ListItem li = new ListItem("", "");
ddl.Items.Insert(0, li);
}
}
当我跟踪代码时,当进入listviewAccountCode.DataBind(); 时,这将调用listviewAccountCode_ItemCreated 函数,该函数将在下拉列表顶部添加一个空选项。但是在完成功能并返回listviewAccountCode.DataBind();后,空选项就消失了。我不太清楚为什么会发生这种情况。
有什么办法吗?
[更新]:如果我只是删除检查if(addEmptyRow),那么下拉列表将包含空选项。所以我想知道BindAccountType 是否被其他函数以某种方式调用,并且由于默认值为false,因此它不会添加空选项。但这种情况什么时候发生?
我在此页面中检查了我的所有代码,它当前通过true 获取addEmptyRow。
[2014/06/11 更新]: aspx代码如下:
<asp:ListView id="listviewAccountCode" runat="server" OnItemCreated="listviewAccountCode_ItemCreated"
OnItemDataBound="listviewAccountCode_ItemDataBound" OnItemInserting="listviewAccountCode_ItemInserting"
OnItemEditing="listviewAccountCode_ItemEditing" OnItemUpdating="listviewAccountCode_ItemUpdating"
OnItemCanceling="listviewAccountCode_ItemCanceling"
InsertItemPosition="LastItem" OnPagePropertiesChanging="listviewAccountCode_PagePropertiesChanging"
>
<LayoutTemplate>
<table style="width:100%">
<tr>
// List of columns
...
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
// List of columns
..
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr>
// List of columns
..
<td>
<asp:DropDownList ID="ddlAccountType" runat="server"></asp:DropDownList>
</td>
..
</tr>
</InsertItemTemplate>
<EditItemTemplate>
<tr>
// List of columns
..
<td>
<asp:DropDownList ID="ddlAccountType" runat="server"></asp:DropDownList>
</td>
..
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table style="width:100%">
<tr>
// List of columns
..
</tr>
<tr style="height:20px"><td colspan="14"></td></tr>
</table>
</EmptyDataTemplate>
</asp:ListView>
<asp:DataPager ID="datapagerResult" runat="server" PagedControlID="listviewAccountCode" OnPreRender="datapagerResult_PreRender">
<Fields>
<asp:NumericPagerField ButtonCount="10"/>
</Fields>
</asp:DataPager>
更新[2014/06/11-2]:
在这种情况下,我有 4 个项目,所以它将是 5 个项目,顶部有空选项。
在调用BindAccountCode(true); 时,它会进入listviewAccountCode.DataBind();,然后它会调用listviewAccountCode_ItemCreated 事件和BindAccountType((DropDownList)listviewAccountCode.InsertItem.FindControl("ddlAccountType"), true);,将项目数设置为5。但是当返回listviewAccountCode.DataBind(); 时,它神奇地变成了9 个项目。不知道那里发生了什么。
【问题讨论】:
标签: c# asp.net listview drop-down-menu