【问题标题】:Refreshing a Repeater control in an UpdatePanel with ASP.NET使用 ASP.NET 刷新 UpdatePanel 中的 Repeater 控件
【发布时间】:2009-07-14 11:40:59
【问题描述】:

我正在尝试编写一个页面,您可以在其中发表评论而无需重新加载整个页面。 cmets 使用Repeater 控件显示。模板如下所示:

    <asp:UpdatePanel runat="server" ID="commentsUpdatePanel" UpdateMode="Conditional">
        <ContentTemplate>
        <!-- Comments block -->
        <div class="wrapper bloc content">
            <h3><img src="img/comments.png" alt="Comments" />&nbsp;Comments</h3>                                     
            <p><asp:Label ID="viewImageNoComments" runat="server" /></p>
            <asp:Repeater ID="viewImageCommentsRepeater" runat="server">
                <HeaderTemplate>
                    <div class="float_box marge wrapper comments">
                </HeaderTemplate>
                <ItemTemplate>
                    <div class="grid_25">
                        <span class="user"><%#Eval("username")%></span><br />
                        <span style="font-size:x-small; color:#666"><%#Eval("datetime") %></span>
                    </div>
                    <div class="grid_75">
                        <p align="justify"><%#Eval("com_text") %></p>
                    </div>
                </ItemTemplate>
                <FooterTemplate>
                    </div>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        <!-- Post comment block -->
        <div class="wrapper bloc content">
            <h3><a id="post_comment" name="post_comment"><img src="img/comment_edit.png" alt="Comments" /></a>&nbsp;Post 
                a comment</h3>
            <p class="description">Please be polite.</p>
            <p>
                <asp:Label ID="postCommentFeedback" runat="server" />
            </p>
            <table border="0">
                <tr>
                    <td valign="top">
                    <asp:TextBox id="postCommentContent" runat="server" TextMode="MultiLine" 
                    MaxLength="600" Columns="50" Rows="15" Width="400px" />
                    </td>
                    <td valign="top">
                    <span style="font-size:x-small">BBCode is enabled. Usage :<br />
                    <b>bold</b> : [b]bold[/b]<br />
                    <i>italic</i> : [i]italic[/i]<br />
                    <span class="style1">underline</span> : [u]underline[/u]<br />
                    Link : [url=http://...]Link name[/url]<br />
                    Quote : [quote=username]blah blah blah[/quote]</span>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                    <asp:Button ID="postCommentButton" runat="server" Text="Submit" 
                    onclick="postCommentButton_Click" />    
                    </td>
                </tr>
            </table>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>

postCommentButton_Click() 函数工作正常 - 单击“提交”将发布帖子。但是,我需要完全重新加载页面才能看到新的 cmets - 用户刚刚发布的帖子直到那时才会显示。在 (!isPostBack) 检查之后,我在 Page_Load() 中对中继器进行数据绑定。

postCommentButton_Click() 函数如下所示:

protected void postCommentButton_Click(object sender, EventArgs e)
{
        // We check if user is authenticated
        if (User.Identity.IsAuthenticated)
        {
            // Attempt to run query
            if (Wb.Posts.DoPost(postCommentContent.Text, Request.QueryString["imageid"].ToString(), User.Identity.Name, Request.UserHostAddress))
            {
                postCommentFeedback.Text = "Your post was sucessful.";
                postCommentContent.Text = "";

            }
            else
            {
                postCommentFeedback.Text = "There was a problem with your post.<br />";
            }

        }
        // CAPTCHA handling if user is not authenticated
        else
        {
            // CAPTCHA
        }
}

在我的例子中,我们确实看到 postCommentFeedback.Text 刷新了,但是,再一次,不是应该有更多帖子的转发器的内容。

我错过了什么?

【问题讨论】:

  • 我很确定这是因为我的Repeater的DataSource是一个MySqlDataReader,并且在用户发布他的帖子后我没有更新它。它没有更新,因为我只在(!IsPostBack)时才进行数据绑定......那么正确的方法是什么?我应该在每个 Page_Load() 上进行 DataBind,但在发表评论后我需要 DataBind。

标签: c# asp.net updatepanel repeater


【解决方案1】:

您应该照原样在 !IsPostBack 内的 Page_Load 中进行 DataBind。您还应该在 Click 事件中进行数据绑定。

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.DataBind();
        }
    }
    protected void MyButton_Click(object sender, EventArgs e)
    {
        //Code to do stuff here...

        //Re DataBind
        this.DataBind();
    }
    public override void DataBind()
    {
        //Databinding logic here
    }

【讨论】:

    【解决方案2】:

    不要让你的数据源成为 MySqlDataReader,而是让你的阅读器填充一个 BindingList 或类似的东西。将其保持在会话中,并为每个非回发和点击进行数据绑定。当您的用户发帖时,您可以将其添加到列表中并等待某些内容告诉它保存,但在发布 cmets 以将其帖子保存到您的数据库并重做数据拉取并踩到您的BindingList 和重新Databind。

    另外,个人的烦恼:我不喜欢 。您页面中的代码通常是一个不好的迹象。尝试使用 Repeater.ItemDataBound 事件 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

    【讨论】:

      【解决方案3】:

      在我看来,快速解决方法是在页面加载时绑定,而不管回发如何。或者,您可以从 postCommentButton_Click 中重新绑定。

      【讨论】:

        【解决方案4】:
        protected void Timer1_Tick(object sender, EventArgs e)   
        {
        
               Repeater1.DataBind();
                   /*This is all I did for it to work.*/
        }
        
        protected void Buttontextbox_Click(object sender, EventArgs e)
        
        {
        
               this.DataBind();
            /*Leave sql connection to your database above "this.databind"*/
        
        }
        

        【讨论】:

          【解决方案5】:

          尝试将更新面板放在标签之间,如果你已经这样做了,然后检查 div 标签的关闭是否正确

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-08-30
            • 2021-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-06
            • 1970-01-01
            相关资源
            最近更新 更多