2

Update panel does a full post back when I am just trying to do a partial post back. I just want to be able to update the Repeater and not update the whole page when i click the hyperlinks previous page and next page .

   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();

}

This is my 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>
v1rusxx
  • 21
  • 3

2 Answers2

2

Hyperlink controls don't do a postback at all, they evaluate to regular links that will do a GET, not a POST.

Since it seems you want something that looks like a link, but does a postback rather than a GET, what you need is a LinkButton. It looks like a link, but acts like a button and performs a postback.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • servy so your saying i could make linkbuttons and then make event handlers for the linkbuttons and maybe do a responce.redirect to url? Would this create a post? I am assuming to do a redirect it would just do a GET.... Any suggestions? – v1rusxx Nov 13 '12 at 18:57
  • @v1rusxx You were fine up until the part where you want to do a redirect. You don't do a redirect; doing so will result in a GET, not a postback, so there's no way to do a partial load. You should use a LinkButton, add an event handler, and in that event handler re-bind the repeater to the next page. – Servy Nov 13 '12 at 19:02
0

Figure I would post an answer to my own question since I figure it out with help of Servy. Hyperlinks do a Get not a post that is why my previous code above was not working. So I rework my code to include the following.

  1. I added Two buttons
  2. Made Event Handlers for the Two buttons to create a POST

Here is my Code might help someone

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();

}

here is my 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>
v1rusxx
  • 21
  • 3