访问控件ListView的EditItemTemplate的 [英] Accessing controls in the edititemtemplate of a listview

查看:154
本文介绍了访问控件ListView的EditItemTemplate的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与ListView控件工作。默认情况下我显示与编辑按钮ItemTemplate中。当编辑按钮被pressed列表视图切换到EditItemTemplate里。我需要填充正在编辑基于项目的EditItemTemplate中的控件之一 - 我已经试过在ItemEditing事件访问控制(通过的FindControl)(和pretty每隔多少其它事件一样),但是控制只是似乎并不存在。我可以访问控制在ItemTemplate确定,但不是EditItemTemplate中。

I am working with the listview control. By default I am showing the itemtemplate with an edit button. When the edit button is pressed the listview switches to the edititemtemplate. I need to populate one of the controls in the edititemtemplate based on the item being edited - I've tried accessing the control (via FindControl) in the ItemEditing event (and pretty much every other event as well), however the controls just don't seem to exist. I can access controls in the itemtemplate ok, but not the edititemtemplate.

任何人都可以让我知道我怎么可以访问ListView的EditItemTemplate模板内举行了控制,并从其中的事件我应该这样做?

Can anyone let me know how I can access a control held within the edititemtemplate of a listview, and from which event I should do so?

修改
我试图用这个访问控制:

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。

I've also tried in ItemDataBound and ItemCreated.

ListView的声明是:

The listview declaration is:

<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>

编辑:
我遍历所有ListView中使用类似下面code中的控制,且该控制仍然不可见。是否显示在编辑模板前ItemEditing事件火?如果是这样,我可以用什么事件来访问编辑模板控件?

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;
   }
}

*的编辑:*
我可以访问控件在ListView控件的ItemCreated事件的EditItemTemplate中,但是没有他们没有内容(我假设数据尚未绑定),所以我不能得到键值我需要做查找,获取数据,我需要填充控制。

* * 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.

推荐答案

我想出了一个办法做我需要做的,虽然我不与它非常高兴。

I've figured out a way to do what I need to do, though I'm not terribly happy with it.

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

这篇关于访问控件ListView的EditItemTemplate的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆