在ListView的编辑模板中使用DDL

本文关键字:DDL ListView 编辑 | 更新日期: 2023-09-27 18:02:46

首先,我对编程非常陌生,所以请原谅我。

通过执行以下操作,我能够在插入模板中获得一个下拉列表。

<InsertItemTemplate>
<asp:DropDownList ID="ddlAprt" runat="server" AutoPostBack="True">
                            <asp:ListItem>Check</asp:ListItem>
                            <asp:ListItem>Ok</asp:ListItem>
                            <asp:ListItem>Repaired</asp:ListItem>
                            <asp:ListItem>Replaced</asp:ListItem>
                </asp:DropDownList>
</InsertItemTemplate>


protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        DropDownList ddlProjectManager = (DropDownList)ListView1.EditItem.FindControl("ddlAprt");
        if (ddlProjectManager != null)
            e.Values["Aprt"] = ddlProjectManager.SelectedValue;
    }

供参考,我的数据库将有列数(20+)。为了限制选择,除了一个键(主键)外,所有键都像上面那样使用ddl。所以我一遍又一遍地玩这个代码。

所以我需要相同的功能,但在EditItemTemplete。

我对此有些担心。当他们第一次插入项目时,他们不会更改所有的ddl。因此,我有检查项在ddl工作像一个空字段。当他们去编辑时,那些已经被移出的复选框,需要留在那里。这样他们就不会不小心把他们没有做的项目更新回去检查(什么也做不成)。

用简单的英语来说,他们将要更新他们所处理的行。他们将访问网页。选择Edit,并找到尚未完成的一行。使用ddl编辑该行,然后单击更新。他们没有工作的行回到数据库,因为他们在哪里。他们工作的那个被更新为其他东西,而不是"检查"。

那么我该怎么做呢?我检查了以下网站。这里有一些类似的例子http://www.eggheadcafe.com/community/aspnet/17/10251820/how-to-bound-values-to--dropdownlist-in-gridview--edit-item-template.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewdataitem%28v=vs.90%29.aspx

最后一件事,我使用SQL数据源。我真的很想保持这种状态。越简单越好

谢谢,乔

在ListView的编辑模板中使用DDL

您应该能够直接绑定到下拉列表:

<EditItemTemplate> 
<asp:DropDownList ID="ddlAprt" runat="server" AutoPostBack="True" SelectedValue='<%# Bind("Aprt")%>'>
<asp:ListItem>Check</asp:ListItem>
<asp:ListItem>Ok</asp:ListItem>
<asp:ListItem>Repaired</asp:ListItem>
<asp:ListItem>Replaced</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate> 

你也应该能够在你的插入模板中使用相同的bind语句,那么你就不需要在你的codebehind页面中使用这些代码了。