【问题标题】:Inserting empty option in the dropdownlist of ListView InsertItemTemplate在 ListView InsertItemTemplate 的下拉列表中插入空选项
【发布时间】: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


    【解决方案1】:

    在您的 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();
        }
    

    【讨论】:

    • 还有一些问题。当我使用您提供的代码时,InsertItemTemplate 中的下拉列表包含两倍数量的要插入的项目,减去 1。例如,如果项目是 A,B,我们将在顶部插入一个空选项,那么它将有 5 个项目 `,A,A,B,B. So somehow it's like this function is called twice, and the second calling sets the addEmptyRow` 设置为 false。我认为罪魁祸首是为什么这个函数被调用了两次。您的特定代码可以添加空行,只是它仍然被调用两次。
    • 只需在代码中再添加一行,试试那一行,然后告诉我结果
    • 对不起,我先清除下拉列表没有区别。
    • 如果您可以为我提供您的 aspx 代码,那么我可能会为您提供帮助。只需您在下面使用它的代码就足够了:(DropDownList)listviewAccountCode.InsertItem.FindControl("ddlAccountType")
    • 刚刚添加了一个更新 + 最好将其中一个下拉控件的 ID 更改为:ID="ddlAccountType"ID="ddlAccountType2",因此它们彼此不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    相关资源
    最近更新 更多