2

当我只是尝试回发部分帖子时,更新面板会回发完整帖子。当我单击上一页和下一页的超链接时,我只想能够更新中继器而不是更新整个页面。

   protected void Page_Load(object sender, EventArgs e)
{
    PagedDataSource objpd = new PagedDataSource();

    string connStr = ConfigurationManager.ConnectionStrings["yafnet"].ConnectionString;
    SqlConnection mySQLconnection = new SqlConnection(connStr);
    if (mySQLconnection.State == ConnectionState.Closed)
    {
        mySQLconnection.Open();
    }

    SqlCommand mySqlSelect = new SqlCommand("SELECT * FROM [Comments]", mySQLconnection);
    mySqlSelect.CommandType = CommandType.Text;
    SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);
    DataTable table = new DataTable();
    mySqlAdapter.Fill(table);


    objpd.DataSource = table.DefaultView;
    objpd.AllowPaging = true;
    objpd.PageSize = 1;

    int currentPage;

    if (Request.QueryString["page"] != null)
    {
        currentPage = Int32.Parse(Request.QueryString["page"]);
    }
    else
    {
        currentPage = 1;
    }

    objpd.CurrentPageIndex = currentPage - 1;
    // Label1.Text = "Page " + currentPage + " of " + pds.PageCount;

    if (!objpd.IsFirstPage)
    {
        linkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1);



    }

    if (!objpd.IsLastPage)
    {
        linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1);



    }

    Repeater1.DataSource = objpd;
    Repeater1.DataBind();

}

这是我的 HTML

<div class="comment">

    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">

        <ContentTemplate>
            <asp:Repeater ID="Repeater1" runat="server">

                <HeaderTemplate>
                    <table class="commentsx" cellspacing="0">
                </HeaderTemplate>

                <ItemTemplate>

                    <tr>
                        <td class="name">
                            <div class="right">
                                <%# Eval("CommentUserName") %>
                            </div>
                            <div class="left">
                                <%# Eval("CommentDateTime") %>
                            </div>
                        </td>


                    </tr>
                    <tr>
                        <td>
                            <div class="mess">
                                <%# Eval("CommentMessage") %>
                            </div>
                        </td>
                    </tr>


                </ItemTemplate>


                <FooterTemplate>
                    </table>
                </FooterTemplate>


            </asp:Repeater>

            <asp:HyperLink ID="linkPrev" runat="server">Previous Page</asp:HyperLink>
            <asp:HyperLink ID="linkNext" runat="server">Next Page</asp:HyperLink>
        </ContentTemplate>



        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="DataBinding" />
        </Triggers>



    </asp:UpdatePanel>

</div>
4

2 回答 2

2

Hyperlink控件根本不进行回发,它们评估为将执行 GET 而不是 POST 的常规链接。

由于您似乎想要一些看起来像链接的东西,但执行的是回发而不是 GET,所以您需要的是一个LinkButton. 它看起来像一个链接,但它就像一个按钮并执行回发。

于 2012-11-13T18:33:42.543 回答
0

图我会发布我自己的问题的答案,因为我是在 Servy 的帮助下弄明白的。超链接不会发布帖子,这就是我之前的代码无法正常工作的原因。所以我修改了我的代码以包含以下内容。

  1. 我添加了两个按钮
  2. 为两个按钮制作了事件处理程序以创建 POST

这是我的代码可能会帮助某人

public int CurrentPage
{
    get
    {
        // look for current page in ViewState
        object o = this.ViewState["_CurrentPage"];
        if (o == null)
            return 0;   // default to showing the first page
        else
            return (int)o;
    }

    set
    {
        this.ViewState["_CurrentPage"] = value;
    }
}
private void updateMessage()
{
    PagedDataSource objpd = new PagedDataSource();
    string connStr = ConfigurationManager.ConnectionStrings["yafnet"].ConnectionString;
    SqlConnection mySQLconnection = new SqlConnection(connStr);
    if (mySQLconnection.State == ConnectionState.Closed)
    {
        mySQLconnection.Open();
    }

    SqlCommand mySqlSelect = new SqlCommand("SELECT * FROM [Comments]", mySQLconnection);
    mySqlSelect.CommandType = CommandType.Text;
    SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);
    DataTable table = new DataTable();
    mySqlAdapter.Fill(table);


    objpd.DataSource = table.DefaultView;
    objpd.AllowPaging = true;
    objpd.PageSize = 1;
    objpd.CurrentPageIndex = CurrentPage;
    //disable pre or next buttons if necessary
    cmdPrev.Enabled = !objpd.IsFirstPage;
    cmdNext.Enabled = !objpd.IsLastPage;

    Repeater1.DataSource = objpd;
    Repeater1.DataBind();


}

protected void Page_Load(object sender, EventArgs e)
{


    updateMessage();

}



protected void cmdPrev_Click(object sender, EventArgs e)
{
    // Set viewstate variable to the previous page
    CurrentPage -= 1;

    // Reload control
    updateMessage();
}
protected void cmdNext_Click(object sender, EventArgs e)
{
    // Set viewstate variable to the next page
    CurrentPage += 1;

    // Reload control
    updateMessage();

}

这是我的html

  <div  class="comment">

                     <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">

                         <ContentTemplate>
                             <asp:Repeater ID="Repeater1" runat="server">

                                 <HeaderTemplate>
                                     <table class="commentsx" cellspacing="0">
                                 </HeaderTemplate>

                                 <ItemTemplate>

                                     <tr>
                                         <td class="name">
                                             <div class="right">
                                                 <%# Eval("CommentUserName") %>
                                             </div>
                                             <div class="left">
                                                 <%# Eval("CommentDateTime") %>
                                             </div>
                                         </td>


                                     </tr>
                                     <tr>
                                         <td>
                                             <div class="mess">
                                                 <%# Eval("CommentMessage") %>
                                             </div>
                                         </td>
                                     </tr>


                                 </ItemTemplate>


                                 <FooterTemplate>
                                     </table>
                                 </FooterTemplate>


                             </asp:Repeater>

                              <asp:button id="cmdPrev" runat="server" text=" << " onclick="cmdPrev_Click"></asp:button>
                              <asp:button id="cmdNext" runat="server" text=" >> " onclick="cmdNext_Click"></asp:button></td>
                         </ContentTemplate>

                     </asp:UpdatePanel>


                 </div>
于 2012-11-13T20:46:55.747 回答