我有一個ListView與創建OnItemDataBound的pdf縮略圖。每個縮略圖都有一個複選框來選擇我想要上傳的頁面。一切正常 - 迄今爲止。現在,我又複選框以選擇所有頁面的問題是:如果我選中複選框,在我的ListView所有預覽縮略圖消失cheche /取消選中列表視圖中的所有複選框與onItemDataBound
這是我的ListView:
<asp:ListView ID="pdfPagesListView" runat="server" OnItemDataBound="pdfPagesListView_ItemDataBound">
<ItemTemplate>
<div id="pdfFrameDiv" runat="server" class="pdfPage"><%# Container.DataItem %>
<div style="position:absolute;">
<asp:Image ID="pdfPreviewImage" runat="server" />
</div>
<div style="position:relative;height:188px;background-color:rgb(240,240,240)">
<asp:Panel ID="thumbnails" runat="server" />
</div>
<div style="position:relative; top:-14px; left:120px;">
<asp:CheckBox ID="selectPdfPageCheckbox" runat="server" />
</div>
</div>
</ItemTemplate>
</asp:ListView>
這是我的CheckBox
<asp:checkbox ID="selectAllPages" runat="server" AutoPostBack="true" OnCheckedChanged="selectAllPdfPages" />
這是ListView中的OnItemDataBound代碼:
protected void pdfPagesListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
if (byteArray.Length < 25000000)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
int i = dataItem.DisplayIndex;
if (Session["computedPages"] != null)
{
int[] computedPages = (int[])Session["computedPages"];
if (computedPages[i] == 1)
{
dataItem.Visible = false;
}
}
try
{
Panel thumbnailPanel = (Panel)e.Item.FindControl("thumbnails");
Thumbnail thumbnail = new Thumbnail();
thumbnail.SessionKey = unique;
thumbnail.Index = i + 1;
thumbnail.DPI = 17;
thumbnail.Width = 200;
thumbnailPanel.Controls.Add(thumbnail);
Image pdfPreviewImage = (Image)e.Item.FindControl("pdfPreviewImage");
pdfPreviewImage.Visible = false;
}
catch (Exception ex)
{
Image pdfPreviewImage = (Image)e.Item.FindControl("pdfPreviewImage");
pdfPreviewImage.ImageUrl = "~/img/pdfPreview.jpg";
}
}
else
{
Image pdfPreviewImage = (Image)e.Item.FindControl("pdfPreviewImage");
pdfPreviewImage.ImageUrl = "~/img/pdfPreview.jpg";
}
}
}
這是我檢查的代碼/取消選中該複選框事件:
protected void selectAllPdfPages(object sender, EventArgs e)
{
if (selectAllPages.Checked == true)
{
foreach (ListViewDataItem item in pdfPagesListView.Items)
{
CheckBox cb = (CheckBox)(item.FindControl("selectPdfPageCheckbox"));
cb.Checked = true;
}
}
else
{
foreach (ListViewDataItem item in pdfPagesListView.Items)
{
CheckBox cb = (CheckBox)(item.FindControl("selectPdfPageCheckbox"));
cb.Checked = false;
}
}
}
這是我的網頁怎麼看起來像OnItemDataBound事件之後:
的這個它的外觀檢查selectAllPages複選框後:
我想檢查/取消選中所有複選框而不會丟失所有縮略圖預覽。我希望有人能幫助我了...提前
感謝
我想我知道它發生的原因,但不知道如何解決這個問題。原因是您將動態添加的tumbnail控件添加到頁面中。動態添加的控件應該在每次回發後再次添加到控件樹中。在你的情況下,這不會發生,因爲你在數據綁定期間添加它們,只發生一次。爲了解決這個問題,可以切換到客戶端解決方案,或者在標記中聲明縮略圖,或者作爲最後的手段,想辦法在每個回帖中添加它們(但請不要) – Andrei 2014-10-20 12:50:42