C# 使用 ASP.NET 刷新 UpdatePanel 中的中继器控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1124856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:47:05  来源:igfitidea点击:

Refreshing a Repeater control in an UpdatePanel with ASP.NET

c#asp.netupdatepanelrepeater

提问by Astaar

I'm trying to code a page where you can post a comment without reloading the whole page. The comments are displayed using a Repeater control. The template looks like this:

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

    <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>

The postCommentButton_Click() function works just fine - clicking "Submit" will make the post. However, I need to completely reload the page in order to see new comments - the post the user just made will not show until then. I Databind the Repeater in Page_Load() after a (!isPostBack) check.

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

The postCommentButton_Click() function looks like this:

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
        }
}

In my case, we do see postCommentFeedback.Text refreshed, but, again, not the content of the repeater which should have one more post.

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

What is it I'm missing?

我错过了什么?

采纳答案by Robin Day

You should DataBind in the Page_Load within a !IsPostBack as you are. You should ALSO databind in your Click event.

您应该在 !IsPostBack 内的 Page_Load 中进行数据绑定。您还应该在 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
    }

回答by Andy_Vulhop

Instead of making your datasource a MySqlDataReader, have your reader populate a BindingList or something like that. Keep that in session and do your databind every non-postback and click. When your user posts, you can either add it to the list and wait for something to tell it to save that, but it makes more sense in the context of posting comments to save their post to your db and redo your datapull and stomp over your BindingList and re-Databind.

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

Also, personal peeve: I dislike <%#Eval....%>. Code in your page is usually a bad sign. Try using the Repeater.ItemDataBound Event http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

另外,个人的不满:我不喜欢 <%#Eval....%>。页面中的代码通常是一个不好的信号。尝试使用 Repeater.ItemDataBound 事件 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

回答by Bernhard Hofmann

It sounds to me like the quick fix is to bind on page load regardless of postback. Alternatively, you could rebind from within postCommentButton_Click.

在我看来,快速修复是绑定页面加载而不管回发。或者,您可以从 postCommentButton_Click 内重新绑定。

回答by Frederik R?nne

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"*/

}

回答by omprela

try putting the update panel between tags and if you have already done that then check if the closing of div tags is proper

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