即使我使用的是更新面板,有时我的整个页面都会刷新,我不知道为什么。
(编辑考虑到 Jason P 的回答。)
母版代码:
<form runat="server" id="form1">
<asp:ScriptManager ID="ScriptManager1" runat="server" enablePartialRendering="true" />
<h1>Title</h1>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional" childrenAsTriggers="true">
<ContentTemplate>
<ul class="letterLinks">
<asp:Repeater runat="server" ID="Letter_Repeater">
<ItemTemplate>
<li id='<%#Eval("Letter")%>'>
<asp:LinkButton runat="server" ID="UpdateLink" Text="My Text"
CommandName="BookLetter" CommandArgument='<%#Eval("Letter")%>' OnCommand="LoadBookTitles"/>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<ul class="BookList">
<asp:Repeater runat="server" ID="Title_Repeater">
<ItemTemplate>
<li><a href='<%#Eval("URL")%>'><%#Eval("Title")%></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ContentTemplate>
</asp:UpdatePanel>
</form>
代码背后:
public partial class MyBooks : System.Web.UI.MasterPage
{
private GetAllBookInfo setMyInfo = new GetAllBookInfo();
private DataTable allTitleInfo = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
List<string> lettersWithBooks = new List<string>();
DataTable letterLinks = new DataTable();
// Get all letters that have Books
lettersWithBooks = setMyInfo.LettersWithBooks();
// get properties for ABC links
letterLinks = GetLetterLinkProperties(lettersWithBooks);
// Bind abc link data to the ABC repeater
Letter_Repeater.DataSource = letterLinks;
Letter_Repeater.DataBind();
allTitleInfo = setMyInfo.GetBookTitles(lettersWithBooks[0]);
// Bind title info data to title repeater
Title_Repeater.DataSource = allTitleInfo;
Title_Repeater.DataBind();
}
private DataTable GetLetterLinkProperties(List<string> letters)
{
DataTable letterLinks = new DataTable();
letterLinks.Columns.Add("Letter", typeof(string));
letterLinks.Columns.Add("Class", typeof(string));
letterLinks.Columns.Add("Enabled", typeof(bool));
for (char c = 'A'; c <= 'Z'; c++)
{
if (letters.Any(letter => letter == c.ToString()))
{
letterLinks.Rows.Add(c, "hasGuides", true);
}
else
{
letterLinks.Rows.Add(c, "noGuides", false);
}
}
return letterLinks;
protected void LoadBookTitles(object sender, EventArgs e)
{
LinkButton letterLink = (LinkButton)sender;
allTitleInfo = setMyInfo.GetBookTitles(letterLink.CommandArgument as String);
// Bind title info data to title repeater
Title_Repeater.DataSource = allTitleInfo;
Title_Repeater.DataBind();
}
}