English 中文(简体)
访问控制的edititemtemplate listview
原标题:
  • 时间:2009-05-05 14:12:20
  •  标签:

我的工作列表视图控件。我默认显示itemtemplate edit按钮。当编辑按钮被按下listview edititemtemplate开关。我需要填充控件之一edititemtemplate基于正在编辑的项目,我已经尝试访问控制(通过FindControl) ItemEditing事件(和几乎所有其他事件),然而,控制似乎不存在。我可以在itemtemplate访问控制好,但不是edititemtemplate。

谁能让我知道我可以访问控制列表视图edititemtemplate内举行,从这事件我应该这样做吗?

EDIT I m trying to access the control using this:

protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
    ListViewItem item = UnitsLV.Items[e.NewEditIndex];
    ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}

我也在ItemDataBound ItemCreated。

listview宣言是:

<asp:Content ID="Content1" ContentPlaceHolderID="ColumnA" runat="server">
    <asp:Panel ID="Panel1" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Panel ID="SummaryPnl" runat="server">
                    <asp:ListView ID="UnitsLV" runat="server" DataSourceID="DataLDS" DataKeyNames="u_uid"
                        InsertItemPosition="LastItem" OnItemInserting="UnitsLV_ItemInserting" OnItemDataBound="UnitsLV_ItemDataBound"
                        OnItemCreated="UnitsLV_ItemCreated" onitemediting="UnitsLV_ItemEditing">
                        <ItemTemplate>
                            <tr class="rowA">
                                <td>
                                    <asp:Label runat="server" ID="UnitIDLbl" Text= <%# Eval("u_uid")%> ></asp:Label>
                                </td>
                                <td>
                                    <%# Eval("u_Title")%>
                                </td>
                                <td>
                                    <asp:LinkButton ID="EditBtn" runat="server" CommandName="Edit" CommandArgument= <%#Eval("u_uid") %> 
                                    Text="Edit" />
                                </td>
                                <td>
                                    <asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete" CommandArgument= <%#Eval("u_uid") %> 
                                    Text="Delete" />
                                </td>
                            </tr>
                        </ItemTemplate>
                        <InsertItemTemplate>
                            <tr class="rowB">
                                <td>
                                    <br />
                                    &nbsp;
                                </td>
                                <td>
                                    <br />
                                    <asp:TextBox ID="TitleTB" runat="server" Text= <% #Bind("u_Title")%> ></asp:TextBox>
                                </td>
                                <td>
                                    <br />
                                    <asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
                                    <asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
                                </td>
                                <td>
                                    <asp:LinkButton ID="InsertBtn" runat="server" CommandName="Insert" Text="Insert" />
                                </td>
                                <td>
                                    <asp:LinkButton ID="CancelBtn" runat="server" CommandName="Cancel" Text="Cancel" />
                                </td>
                            </tr>
                        </InsertItemTemplate>
                        <EditItemTemplate>
                            <tr class="rowB">
                                <td>
                                    <br />
                                    <asp:Label runat="server" ID="UnitIDLbl" Text= <%# Bind("u_uid")%> ></asp:Label>
                                </td>
                                <td>
                                    <br />
                                    <asp:TextBox ID="TitleTB" runat="server" Text= <% #Bind("u_Title")%> ></asp:TextBox>
                                </td>
                                <td>
                                    <br />
                                    <asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
                                    <asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
                                </td>
                                <td>
                                    <asp:LinkButton ID="UpdateBtn" runat="server" CommandName="Update" Text="Update" />
                                </td>
                                <td>
                                    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" Text="Cancel" />
                                </td>
                            </tr>
                        </EditItemTemplate>
                        <LayoutTemplate>
                            <table id="Table2" runat="server" width="100%">
                                <tr id="Tr1" runat="server">
                                    <td id="Td1" runat="server">
                                        <table id="itemPlaceholderContainer" runat="server" border="0" style="" width="100%">
                                            <tr id="itemPlaceholder" runat="server"></tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr id="Tr2" runat="server">
                                    <td id="Td2" runat="server" style=""></td>
                                </tr>
                            </table>
                        </LayoutTemplate>
                    </asp:ListView>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>
</asp:Content>

EDIT: I ve iterated over all the controls in the listview using code similar to below, and the control is still not visible. Does the ItemEditing event fire before the edit template is shown? If so, what event can I use to access the edit template controls?

foreach (Control child in control.Controls)
{
   Control result = Find(child, id);
   if (result != null)
   {
      return result;
   }
}

**EDIT: ** I can access the controls in the edititemtemplate in the listview s ItemCreated event, however none they have no content (I d assume the data hasn t been bound yet), so I can t get the key-value I need to do a lookup to get the data I need to populate the control.

最佳回答

我已经想出了一个办法我需要做什么,虽然我不是非常满意。

protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (UnitsLV.EditIndex > -1)
    {
        // Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
    }
}
问题回答

我不知道以前的版本,但是在Visual Studio 2010中你可以很容易地访问编辑项trhough”列表视图。EditItem”属性如下:

private void myListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
   myListView.EditIndex = e.NewEditIndex;
   myListView.DataBind();

   ListViewItem lvwItem = lvwLista.EditItem;
   ListBox tempLB = (ListBox) lvwItem.FindControl("ListBox3");
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{        
        ListBox myListBox = (ListBox)(((ListView)sender).EditItem.FindControl("ListBox1")); 
}
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e) 
{ 
    ListViewItem item = UnitsLV.Items[e.NewEditIndex]; 
    ListBox tempLB = (ListBox)e.item.FindControl("ListBox3"); 
} 

我相信我找到了一个错误在上面的函数。第二行

ListBox tempLB = (ListBox)item.FindControl("ListBox3"); 

我所做的是取代“e。项”与“项目”

我通常使用ItemDataBound事件……检查ListItemType Enum的其他选项

    protected void UnitLV_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.EditItem)
        {
                ListBox myListBox = (ListBox) e.Item.FindControl("ListBox3");
        }
    }

这类似于公认的答案,但我认为作者是真正推动向:

protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewDataItem listViewDataItem = e.Item as ListViewDataItem;
    if (UnitsLV.EditIndex == listViewDataItem.DataItemIndex)
    {
        // Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
    }
}




相关问题
热门标签