Updating a Repeater Element in UpdatePanel Using ASP.NET

I am trying to encode a page where you can post a comment without reloading the entire page. Comments are displayed using the Repeater control. The template is as follows:

<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, by clicking Submit, you will receive a message. However, I need to completely reload the page to see the new comments - the message I just made until it appears. I Databind the Repeater in Page_Load () after checking (! IsPostBack).

The postCommentButton_Click () function looks like this:

 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 see that postCommentFeedback.Text is updated, but again not the content of the relay, which should have another message.

What am I missing?

+6
c # updatepanel repeater
source share
5 answers

You need a DataBind in the Page_Load inside! IsPostBack, like you. You should also use data binding in your Click event.

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

Instead of making your MySqlDataReader data source, ask your reader to fill out a BindingList or something like that. Store this in a session and do your data binding each without postback and click. When your users post messages, you can either add them to the list or wait for something to tell him to save it, but it makes sense in the context of posting comments to save their message in your db and repeat your datapull and trample BindingList and re-databind.

Also a personal peeve: I don't like <% # Eval ....%>. The code on 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

0
source share

It seems to me that a quick fix is ​​to bind to page loading regardless of postback. Alternatively, you can regroup from postCommentButton_Click.

0
source share
 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"*/ } 
0
source share

try putting the update panel between the tags, and if you have already done this, check if the div tag closes correctly

0
source share

All Articles