簡體   English   中英

在ListView InsertItemTemplate的下拉列表中插入空選項

[英]Inserting empty option in the dropdownlist of ListView InsertItemTemplate

根據標題,我有一個列表視圖,在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,因此它不會添加空選項。 但是什么時候發生? 我檢查了我在此頁面中的所有代碼,當前它們是否為addEmptyRow通過true

[更新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>

Update [2014/06 / 11-2] :在這種情況下,我有4個項目,因此它將是5個項目,頂部為空選項。 在調用BindAccountCode(true); ,它進入listviewAccountCode.DataBind(); 然后它將調用listviewAccountCode_ItemCreated事件和BindAccountType((DropDownList)listviewAccountCode.InsertItem.FindControl("ddlAccountType"), true); 將項目數設置為5。但是返回listviewAccountCode.DataBind(); ,它神奇地變成9件。 不知道那里發生了什么。

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 } 

UPDATE 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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM