[Solved] UpdatePanel Server.Transfer Error: The message received from the server could not be parsed

RichardSa
 
on Aug 31, 2021 11:02 PM
Sample_124868.zip
1141 Views

I tried to pass Gridview row Data with “Server.Transfer” but when I click on the Gridview row, nothing happens. I also set  the EnableEventValidation="false" in the page.

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed

Here is my Gridview and code

<asp:UpdatePanel runat="server" ID="paneltemp">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" GridLines="None" DataKeyNames="Election_Name" AllowPaging="true" HeaderStyle-ForeColor="#eeeeee" HeaderStyle-Font-Bold="true" HeaderStyle-BackColor="#224f6d"
            AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged" OnPageIndexChanging="OnPageIndexChanging" OnRowDataBound="OnRowDataBound" class="table" Width="100%" HeaderStyle-HorizontalAlign="left" RowStyle-HorizontalAlign="Left">
            <Columns>
                <asp:BoundField DataField="Election_Name" HeaderText="ELECTION NAME" />
                <asp:BoundField DataField="Start_Date" HeaderText="START DATE" ReadOnly="true" />
                <asp:BoundField DataField="End_Date" HeaderText="END DATE" />
            </Columns>
        </asp:GridView>
        <hr />
        <div style="float: left; font-size: 10pt; margin-right: 1%; font-weight: 600;">
            Page&nbsp;
            <asp:Label ID="lblPageIndex" runat="server" Text="Label" />
            &nbsp;of&nbsp;
            <asp:Label ID="lblTotalPage" runat="server" />
            (<asp:Label ID="lblTotal" runat="server" />
            Election(s))&nbsp;&nbsp;
            <div class="dvPager">
                <asp:Repeater ID="rptPager" runat="server">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
                            CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
                            OnClick="Page_Changed" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
            </div>
        <br />
        <br />
    </ContentTemplate>
</asp:UpdatePanel>

Default.aspx.cs

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            Server.Transfer("summary.aspx?RowIndex=" + row.RowIndex);
        }
    }
}

Summary.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
        GridView Gridview1 = (GridView)this.Page.PreviousPage.FindControl("Gridview1");
        GridViewRow row = Gridview1.Rows[rowIndex];
        URLtxtbox.Text = Gridview1.DataKeys[rowIndex].Value.ToString();
        LblstartDate.Text = row.Cells[1].Text;
        LblendDate.Text = row.Cells[2].Text;
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 01, 2021 06:10 AM

Hi RichardSa,

Unfortunately, there is no way to detect that Server.Transfer() was called. This means that UpdatePanel can't do anything intelligent when someone calls Server.Transfer().

The response sent back to the client is the HTML markup from the page to which you transferred.

Since its HTML and not the special format, it can't be parsed.

So you have to use Response.Write().

If you want to use Server.Transfer() you have to remove the UpdatePanel.

Refer below example.

HTML

Default

<asp:GridView ID="GridView1" runat="server" GridLines="None" AllowPaging="true" HeaderStyle-ForeColor="#eeeeee" HeaderStyle-Font-Bold="true" HeaderStyle-BackColor="#224f6d"
    AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged" OnPageIndexChanging="OnPageIndexChanging" OnRowDataBound="OnRowDataBound" class="table" Width="100%"
    HeaderStyle-HorizontalAlign="left" RowStyle-HorizontalAlign="Left" DataKeyNames="ContactName">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField DataField="City" HeaderText="City" ReadOnly="true" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
<hr />
<div style="float: left; font-size: 10pt; margin-right: 1%; font-weight: 600;">
    Page&nbsp;       
    <asp:Label ID="lblPageIndex" runat="server" Text="Label" />
    &nbsp;of&nbsp;       
    <asp:Label ID="lblTotalPage" runat="server" />
    (<asp:Label ID="lblTotal" runat="server" />
    Election(s))&nbsp;&nbsp;
    <div class="dvPager">
        <asp:Repeater ID="rptPager" runat="server">
            <ItemTemplate>
                <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
                    CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
                    OnClick="Page_Changed" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:Repeater>
    </div>
</div>

Summary

<asp:Label ID="ContactName" runat="server" Text=""></asp:Label>
<asp:Label ID="City" runat="server" Text=""></asp:Label>
<asp:Label ID="County" runat="server" Text=""></asp:Label>

Code

Default

private int PageSize = 4;
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.ElectionPage(1);
    }
}

private void ElectionPage(int pageIndex)
{
    using (SqlConnection con = new SqlConnection("Data Source = 192.168.0.10\\SQL2014;DataBase=Northwind;UID=sa;PWD=pass@123;"))
    {
        using (SqlCommand cmd = new SqlCommand("Customers_GetCustomersPageWise", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Parameters.AddWithValue("@Name", userlabel.Text.Trim());
            cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
            cmd.Parameters.AddWithValue("@PageSize", PageSize);
            cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
            cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
            con.Open();
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    con.Close();
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
                    this.PopulatePager(recordCount, pageIndex);
                }
            }
        }
    }
}
private void PopulatePager(int recordCount, int currentPage)
{
    double dblPageCount = (double)((decimal)recordCount / (decimal)PageSize);
    int pageCount = (int)Math.Ceiling(dblPageCount);
    List<ListItem> pages = new List<ListItem>();
    if (pageCount > 0)
    {
        if (currentPage != 1)
        {
            pages.Add(new ListItem("Prev", (currentPage - 1).ToString()));
        }
        if (pageCount < 4)
        {
            for (int i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }
        }
        else if (currentPage < 4)
        {
            for (int i = 1; i <= 4; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }
            pages.Add(new ListItem("...", (currentPage).ToString(), false));
        }
        else if (currentPage > pageCount - 4)
        {
            pages.Add(new ListItem("...", (currentPage).ToString(), false));
            for (int i = currentPage - 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }
        }
        else
        {
            pages.Add(new ListItem("...", (currentPage).ToString(), false));
            for (int i = currentPage - 2; i <= currentPage + 2; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }
            pages.Add(new ListItem("...", (currentPage).ToString(), false));
        }
        if (currentPage != pageCount)
        {
            pages.Add(new ListItem("Next", (currentPage + 1).ToString()));
        }
    }
    rptPager.DataSource = pages;
    rptPager.DataBind();

    lblPageIndex.Text = currentPage.ToString();
    lblTotalPage.Text = ((recordCount / PageSize) + ((recordCount % PageSize) > 0 ? 1 : 0)).ToString();
    lblTotal.Text = recordCount.ToString();
}

protected void Page_Changed(object sender, EventArgs e)
{
    int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
    this.ElectionPage(pageIndex);
}

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.ElectionPage(1);
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
        e.Row.Style.Add("cursor", "pointer");
        e.Row.ToolTip = "Click to select this row.";
    }
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            Server.Transfer("summary.aspx?RowIndex=" + row.RowIndex);
        }
    }
}

Summary

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
        GridView Gridview1 = (GridView)this.Page.PreviousPage.FindControl("Gridview1");
        GridViewRow row = Gridview1.Rows[rowIndex];
        ContactName.Text = Gridview1.DataKeys[rowIndex].Value.ToString();
    }
}

Screenshot