在ListView InsertItemTemplate的下拉列表中插入空选项

时间:2014-06-09 05:37:32

标签: c# asp.net listview drop-down-menu

根据标题,我有一个listview,在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.但是当返回{{1}时,它神奇地变成了9个项目。不知道那里发生了什么。

1 个答案:

答案 0 :(得分:0)

在您的BindAccountType方法中,首先将空项添加到您的下拉列表中并使用AppendDataBoundItems属性,然后像这样执行数据绑定:

  private void BindAccountType(DropDownList ddl, bool addEmptyRow = false)
    {
        ddl.Items.Clear(); //try this before you bind your list
        if (addEmptyRow) 
        {
            ListItem li = new ListItem("", "");
            ddl.Items.Insert(0, li);
            ddl.AppendDataBoundItems = true;
        }
        ddl.DataSource = (DataTable)ViewState[ACCTTYPE]; // this retrieves the account type that we already have after the Page_Load function.
        ...        
        ddl.DataBind(); // bind the data
    }

更新1 我刚测试了这个,它正常工作。

    private void BindAccountType(DropDownList ddl, bool addEmptyRow = false)
    {
        ddl.Items.Clear();
        ddl.AppendDataBoundItems = addEmptyRow;

        if (addEmptyRow) 
           DropDownList1.Items.Add(new ListItem("", ""));

        ddl.DataSource = (DataTable)ViewState[ACCTTYPE]; 
        ddl.DataValueField = "your table value";
        ddl.DataTextField = "your table text";
        ddl.DataBind();
    }