C# 使用ASP.NET刷新UpdatePanel中的Repeater控件
我正在尝试编写一个页面,您可以在不重新加载整个页面的情况下发布评论。注释使用中继器控件显示。模板如下所示:C# 使用ASP.NET刷新UpdatePanel中的Repeater控件,c#,asp.net,updatepanel,repeater,C#,Asp.net,Updatepanel,Repeater,我正在尝试编写一个页面,您可以在不重新加载整个页面的情况下发布评论。注释使用中继器控件显示。模板如下所示: <asp:UpdatePanel runat="server" ID="commentsUpdatePanel" UpdateMode="Conditional"> <ContentTemplate> <!-- Comments block --> <div class="wrapper bl
<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>
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
}
}
评论
邮递
评论
请保持礼貌
BBCode已启用。用法:
粗体:[b]粗体[/b]
斜体:[i]斜体[/i]
下划线:[u]下划线[/u]
链接:[网址]=http://...]链接名称[/url]
Quote:[Quote=username]胡说八道
postCommentButton_Click()函数工作正常-单击“提交”将生成帖子。然而,我需要完全重新加载页面才能看到新的评论-用户刚刚发表的帖子在此之前不会显示。在(!isPostBack)检查后,我将中继器数据绑定到第_Load()页
postCommentButton_Click()函数如下所示:
<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>
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
}
}
protectedvoid postCommentButton\u单击(对象发送者,事件参数e)
{
//我们检查用户是否经过身份验证
if(User.Identity.IsAuthenticated)
{
//尝试运行查询
if(Wb.Posts.DoPost(postCommentContent.Text,Request.QueryString[“imageid”].ToString(),User.Identity.Name,Request.UserHostAddress))
{
postCommentFeedback.Text=“您的帖子很成功。”;
postCommentContent.Text=“”;
}
其他的
{
postCommentFeedback.Text=“您的帖子有问题。
”;
}
}
//用户未经身份验证时的验证码处理
其他的
{
//验证码
}
}
在我的例子中,我们确实看到postCommentFeedback.Text刷新了,但是,同样地,没有看到转发器的内容,转发器应该还有一个帖子
我缺少的是什么?你应该在一周内加载页面中的数据绑定!我回到你现在的样子。您还应该在单击事件中进行数据绑定
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
}
不要让数据源成为MySqlDataReader,而是让您的读取器填充一个BindingList或类似的东西。将其保持在会话中,并在每次非回发时进行数据绑定,然后单击。当您的用户发布时,您可以将其添加到列表中,然后等待消息通知它保存该消息,但在发布评论的上下文中,将其发布保存到数据库并重新执行数据拉取和踩踏绑定列表并重新进行数据绑定更有意义 还有,个人的愤怒:我不喜欢。页面中的代码通常是一个不好的标志。尝试使用Repeater.ItemDataBound事件
在我看来,快速修复方法是在页面加载时绑定,而不考虑回发。或者,您可以从postCommentButton_单击中重新绑定。尝试将更新面板放在标记之间,如果您已经这样做了,则检查div标记的关闭是否正确。我很确定这是因为我的中继器的数据源是MySqlDataReader,并且在用户发布帖子后我不会更新它。它没有更新,因为我仅在(!IsPostBack)…时进行数据绑定。。。那么正确的做法是什么呢?我应该在每个页面上进行数据绑定,\u Load(),但我需要在发表评论后进行数据绑定。
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"*/
}