我試圖編寫一個頁面,您可以在其中發佈註釋而不重新加載整個頁面。註釋使用Repeater控件顯示。模板看起來是這樣的:使用ASP.NET刷新UpdatePanel中的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" /> 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> 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()函數工作正常 - 單擊「提交」將使帖子。但是,我需要完全重新加載頁面才能看到新的評論 - 用戶剛纔創建的帖子在此之前不會顯示。我在(!isPostBack)檢查後,在Page_Load()中綁定Repeater。
的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刷新,但同樣,這應該有一個更後的中繼器的不是內容。
這是什麼我錯過了?
我敢肯定,這是因爲我的中繼器的數據源是一個了MySqlDataReader ,並且在用戶發佈帖子後我不更新它。它沒有更新,因爲我只在(!IsPostBack)時才做數據綁定... 那麼這樣做的正確方法是什麼?我應該在每個Page_Load()上使用DataBind,但在發佈評論後需要DataBind。 – Astaar 2009-07-14 11:51:02