显示评论而不重新加载页面

时间:2014-11-07 11:28:38

标签: c# asp.net ajax

下面的代码给我发表评论,将其放入数据库,然后等待页面重新加载以显示评论。如何将提交的评论与新评论一起发布后立即显示?这是完成这些工作的代码: 布局:

<table>
                    <tr>
                        <td colspan="2">All Comments</td>
                        <td>
                            &nbsp
                        </td>
                    </tr>
                    <tr> <td colspan="2"> </td></tr>
                    <tr>
                        <td>
                            <asp:Repeater ID="Repeater1" runat="server" ItemType="SiteStiri.Models.Comments" SelectMethod="GetComments" >
                                <ItemTemplate>
                                    <table>
                                        <tr>
                                            <td colspan="2">
                                                <asp:Label id="lblDate" runat="server" Text='<%# Item.ReleaseDate  %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                <asp:Label ID="lblComment" runat="server" Text='<%# Item.Comment %>'>

                                                </asp:Label>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                <SeparatorTemplate>
                    <hr />
                </SeparatorTemplate>
                <AlternatingItemTemplate>
                    <table>
                                        <tr>
                                            <td colspan="2">
                                                <asp:Label id="Label1" runat="server" Text='<%# Item.ReleaseDate  %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                <asp:Label ID="Label2" runat="server" Text='<%# Item.Comment %>'>

                                                </asp:Label>
                                            </td>
                                        </tr>
                                    </table>
                </AlternatingItemTemplate>
                </asp:Repeater>
              </td>
            </tr>
            </table>

            </td>
            </tr>
            <td>
                <table>
                    <tr>
                        <td colspan="2" >Add Your Comment</td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:Label ID="lblStatus" runat="server" Visible="False"></asp:Label>
                        </td>
                        <td>
                            &nbsp
                        </td>
                    </tr>

                    <tr>
                        <td colspan="2">
                            <asp:TextBox ID="txtcomment" runat="server" TextMode="MultiLine">

                            </asp:TextBox>
                        </td>
                        <td>
                            &nbsp
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                        </td>
                        <td>
                            &nbsp
                        </td>
                    </tr>
                    </table>
                </table>
            </td>
        </tr>
    </table>

代码背后:

 public void fnSubmitComment()
        {
            AddComment cmt = new AddComment();
            String x = Request.QueryString["newsID"].ToString();
            int y = Convert.ToInt32(x);
            bool addSucces = cmt.Addcomment( y , txtcomment.Text);

            if (addSucces)
            {
                lblStatus.Text = "Your Comment has been Added Successfully.";
            }
            else
            {
                lblStatus.Text = "Your Comment has not been Added.";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            fnSubmitComment();
        }

        public IQueryable<Comments> GetComments()
        {
            var _db = new SiteStiri.Models.CommentsContext();
            IQueryable<Comments> query = _db.Comments;
            String x = Request.QueryString["newsID"].ToString();
            int y = Convert.ToInt32(x);
            query = query.Where(p => p.NewsID == y);

            return query;
        }

我听说我可以用ajax做到这一点? (我对ajax的了解目前是0)关于我如何做到这一点的任何提示?

1 个答案:

答案 0 :(得分:1)

您必须在更新面板中包装转发器,  设置UpdateMode =&#34;条件&#34;并在btnSubmit上添加一个触发器,以便在单击按钮时更新具有正常页面生命周期的转发器

请记住在代码隐藏中添加commet之后绑定转发器,并且必须在页面中出现ScriptManager控件

或多或少

<asp:ScriptManager runat="server" />
  <table>
            <tr>
                <td colspan="2">All Comments</td>
                <td>&nbsp
                </td>
            </tr>
            <tr>
                <td colspan="2"></td>
            </tr>
            <tr>
                <td>
                    <asp:UpdatePanel runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:Repeater ID="Repeater1" runat="server" ItemType="SiteStiri.Models.Comments" SelectMethod="GetComments">
                                <ItemTemplate>
                                    <table>
                                        <tr>
                                            <td colspan="2">
                                                <asp:Label ID="lblDate" runat="server" Text='<%# Item.ReleaseDate  %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                <asp:Label ID="lblComment" runat="server" Text='<%# Item.Comment %>'>

                                                </asp:Label>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                                <SeparatorTemplate>
                                    <hr />
                                </SeparatorTemplate>
                                <AlternatingItemTemplate>
                                    <table>
                                        <tr>
                                            <td colspan="2">
                                                <asp:Label ID="Label1" runat="server" Text='<%# Item.ReleaseDate  %>'></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                <asp:Label ID="Label2" runat="server" Text='<%# Item.Comment %>'>

                                                </asp:Label>
                                            </td>
                                        </tr>
                                    </table>
                                </AlternatingItemTemplate>
                            </asp:Repeater>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="btnSubmit" />
                        </Triggers>
                    </asp:UpdatePanel>

                </td>
            </tr>

            <tr>

                <td>
                    <table>
                        <tr>
                            <td colspan="2">Add Your Comment</td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:Label ID="lblStatus" runat="server" Visible="False"></asp:Label>
                            </td>
                            <td>&nbsp
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2">
                                <asp:TextBox ID="txtcomment" runat="server" TextMode="MultiLine">

                                </asp:TextBox>
                            </td>
                            <td>&nbsp
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                            </td>
                            <td>&nbsp
                            </td>
                        </tr>
                    </table>

                </td>
            </tr>
        </table>