UpdatePanel有时刷新整个页面

时间:2013-05-08 19:32:08

标签: asp.net ajax updatepanel asp.net-3.5 page-refresh

即使我使用的是更新面板,有时我的整个页面也会刷新,但我无法弄清楚原因。

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

1 个答案:

答案 0 :(得分:2)

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

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

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