2

即使我使用的是更新面板,有时我的整个页面都会刷新,我不知道为什么。

(编辑考虑到 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();
    }
}
4

1 回答 1

2

不确定这是否是整个问题,但<Triggers>元素应该与元素处于同一级别<ContentTemplate>,而不是子元素:

<asp:UpdatePanel>
    <Triggers>
        ...
    <Triggers>
    <ContentTemplate>
        ...
    <ContentTemplate>
</asp:UpdatePanel>

另外,我相信在这种情况下,Repeater 应该是触发器,而不是 LinkBut​​ton。尝试从 LinkBut​​ton 中删除 OnCommand 事件并将其添加到中继器,并更改 UpdatePanel 的触发器。

于 2013-05-08T19:42:35.110 回答