gpt4 book ai didi

asp.net - 访问 ListView 的 edititemtemplate 中的控件

转载 作者:行者123 更新时间:2023-12-02 16:50:18 25 4
gpt4 key购买 nike

我正在使用 ListView 控件。默认情况下,我显示带有编辑按钮的项目模板。当按下编辑按钮时, ListView 切换到编辑项目模板。我需要根据正在编辑的项目填充 edititemtemplate 中的控件之一 - 我尝试在 ItemEditing 事件(以及几乎所有其他事件)中访问该控件(通过 FindControl),但是控件只是不这样做似乎不存在。我可以正常访问 itemtemplate 中的控件,但不能访问 edititemtemplate。

任何人都可以让我知道如何访问 ListView 的 edititemtemplate 中保存的控件,以及我应该从哪个事件执行此操作?

编辑我正在尝试使用以下方法访问该控件:

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>

编辑:我已经使用类似于下面的代码迭代了 ListView 中的所有控件,但该控件仍然不可见。 ItemEditing 事件是否在编辑模板显示之前触发?如果是这样,我可以使用什么事件来访问编辑模板控件?

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

**编辑:**我可以在 ListView 的 ItemCreated 事件中访问 edititemtemplate 中的控件,但是它们都没有内容(我假设数据尚未绑定(bind)),所以我无法获取我需要执行的键值查找以获取填充控件所需的数据。

最佳答案

我已经找到了一种方法来完成我需要做的事情,尽管我对此并不是很满意。

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

关于asp.net - 访问 ListView 的 edititemtemplate 中的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/825048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com