如何访问ListView EditItemTemplate中的控件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何访问ListView EditItemTemplate中的控件相关的知识,希望对你有一定的参考价值。
我有EditItemTemplate的问题。 我在asp listView中使用Telerik RadUpload。 在验证radUpload的事件时,我想检查一些需求,所以我需要findControl两个控件:radUpload和Label
为此,我使用listView的ItemDataBound事件,但问题在于此处,第一个ItemDataBound事件执行,并且在执行RadUpload Validating事件之后,因此radUpload控件和Label控件保持为空
我通过使用静态控制来处理这个问题,但我认为这不是一个好主意。
你有解决方案吗?
另一个问题是标签。但是,我使用静态标签和跟踪显示标签有文本但输出标签没有看到
<asp:ListView ID="LvAdminRing" runat="server" ItemPlaceholderID="ItemPlaceHolder" GroupPlaceholderID="GroupPlaceHolder"
OnItemEditing="LvAdminRing_ItemEditing" OnItemDataBound="onItemDataBound" OnItemUpdating="LvAdminRing_ItemUpdating">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="GroupPlaceHolder"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<GroupTemplate>
<div>
<asp:PlaceHolder runat="server" ID="ItemPlaceHolder"></asp:PlaceHolder>
</div>
</GroupTemplate>
<ItemTemplate>
<div class="RadRingTileDIV col-lg-12" runat="server">
<div class="row RadRingItemsDIV">
<asp:Label ID="lblPrice" CssClass="CustDispBlock CustItemFonts" runat="server" Text='<%# Eval("XPrice")%>'></asp:Label>
<asp:Label ID="lblCode" CssClass="CustDispBlock CustItemFonts" runat="server" Text='<%# Eval("XCode") %>'></asp:Label>
</div>
<div class="row RadRingItemsDIV ">
<asp:ImageButton ID="ImgRingEdit" runat="server" CommandName="Edit" ImageUrl="~/Image/Admin/CommandsPic/Edit.gif" />
<asp:ImageButton ID="ImgRingDel" runat="server" CommandName="Delete" ImageUrl="~/Image/Admin/CommandsPic/Delete.gif" />
</div>
</div>
</ItemTemplate>
<EditItemTemplate>
<div class="RadRingTileDIV col-lg-12" runat="server">
<div class="RadRingItemsDIV row">
<img src="ksdla" class="AdminImg CustDispBlock" />
<telerik:RadUpload ID="RupAdminRingPic" runat="server" AllowedFileExtensions=".png,.jpg,.jpeg,.jpe" AllowedMimeTypes="image/png,image/x-png,image/jpeg,image/pjpeg"
MaxFileInputsCount="1" MaxFileSize="52000" OverwriteExistingFiles="False" TargetFolder="~/Image/Products/Ring"
OnValidatingFile="RupAdminRingPic_ValidatingFile" ToolTip="انتخاب عکس جدید با پسوند های png،jpg،jpeg و jpe" ControlObjectsVisibility="CheckBoxes">
</telerik:RadUpload>
</div>
<div class="row RadRingItemsDIV">
<br />
<asp:Button ID="Button1" runat="server" Text="test" CommandName="Update" />
</div>
<div class="row RadRingItemsDIV">
<br />
<asp:Label ID="LblError" CssClass="CustDispBlock CustZ-Index" Visible="false" runat="server" Text=""></asp:Label>
</div>
</div>
</EditItemTemplate>
</asp:ListView>
public static Label lblError;
public static RadUpload RuEditPic;
public static Label lblError;
protected void RupAdminRingPic_ValidatingFile(object sender, Telerik.Web.UI.Upload.ValidateFileEventArgs e)
{
//Label test = (Label)LvItems.FindControl("LblError");
string[] AllowedFileExt = RuEditPic.AllowedFileExtensions;
foreach ( string AllowedExt in AllowedFileExt )
{
if (e.UploadedFile.ContentLength > RuEditPic.MaxFileSize)
{
lblError.Text = "some error";
lblError.Visible = true;
}
}
}
protected void onItemDataBound(object sender, ListViewItemEventArgs e)
{
int x = (e.Item as ListViewDataItem).DataItemIndex;
if (LvAdminRing.EditIndex == (e.Item as ListViewDataItem).DataItemIndex)
{
LvItems = LvAdminRing.Items as ListViewDataItem;
lblError = (e.Item.FindControl("LblError") as Label);
//errNoti = (e.Item.FindControl("LblError") as Label);
RuEditPic = (e.Item.FindControl("RupAdminRingPic") as RadUpload);
}
}
答案
你应该试试这个。
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (dataItem.DisplayIndex == ListView1.EditIndex)
{
TextBox tb = e.Item.FindControl("tbFK_MenuID") as TextBox;
}
}
}
以上是关于如何访问ListView EditItemTemplate中的控件的主要内容,如果未能解决你的问题,请参考以下文章