使用Server.Transfer重定向期间出错'无法解析从服务器收到的消息。'

时间:2016-12-27 08:05:27

标签: c# asp.net webforms

我尝试通过单击gridview内部的链接按钮使用Server.Transfer重定向到Transaction.aspx页面,这一切都发生在OnRowCommand事件中。 链接按钮的代码是

<asp:TemplateField Visible="true" HeaderText="" ShowHeader="false">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="btnRedirect" runat="server" CommandArgument='<%# Bind("ID") %>' CommandName="CompleteTransaction" Text="Complete Transaction"></asp:LinkButton>
                                            </ItemTemplate>
                                        </asp:TemplateField>

这是C#代码

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("CompleteTransaction"))
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row                
            GridViewRow selectedRow = GridView1.Rows[rowIndex - 1];
            Label lblServiceType = (Label)selectedRow.FindControl("lblServiceType");
            Label lblMethod = (Label)selectedRow.FindControl("lblMethod");
            Label lblSource = (Label)selectedRow.FindControl("lblSource");
            Label lblAmount = (Label)selectedRow.FindControl("lblAmount");
            string donorName = selectedRow.Cells[7].Text;
            string contactNo = Convert.ToString(GridView1.DataKeys[rowIndex - 1].Values[1]);
            string province = Convert.ToString(GridView1.DataKeys[rowIndex - 1].Values[2]);
            string city = Convert.ToString(GridView1.DataKeys[rowIndex - 1].Values[3]);
            string address = Convert.ToString(GridView1.DataKeys[rowIndex - 1].Values[4]);
            string donorId = Convert.ToString(GridView1.DataKeys[rowIndex - 1].Values[5]);
            string id = Convert.ToString(GridView1.DataKeys[rowIndex - 1].Values[0]);

            List<string> list = new List<string>();
            list.Add(lblServiceType.Text);
            list.Add(lblMethod.Text);
            list.Add(lblSource.Text);
            list.Add(lblAmount.Text);
            list.Add(donorName);
            list.Add(contactNo);
            list.Add(province);
            list.Add(city);
            list.Add(address);
            list.Add(donorId);
            list.Add(Convert.ToString(rowIndex));

            Session["Controls"] = list;
            Server.Transfer("~/Transaction.aspx");    
        }
    }

如果我点击链接按钮,我在控制台窗口中会出现以下错误

Uncaught Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
at Function.Error$create [as create] (ScriptResource.axd?d=D9drwtSJ4hBA6O8UhT6CQjvFd3OFr4OSn3jDIa_YkBYl_kDCdss53aQGXnuBrrAQMiPFhRMC1uQCNM…:237)
at Sys$WebForms$PageRequestManager$_createPageRequestManagerParserError [as _createPageRequestManagerParserError] (ScriptResource.axd?d=JnUc-DEDOM5KzzVKtsL1tdEX-qt8s9RxtGk2vMlQxGrK2fF3ftonwGzhez_pN-OzkVlcWDlUr8Qv6P…:665)
at Sys$WebForms$PageRequestManager$_parseDelta [as _parseDelta] (ScriptResource.axd?d=JnUc-DEDOM5KzzVKtsL1tdEX-qt8s9RxtGk2vMlQxGrK2fF3ftonwGzhez_pN-OzkVlcWDlUr8Qv6P…:1435)
at Sys$WebForms$PageRequestManager$_onFormSubmitCompleted [as _onFormSubmitCompleted] (ScriptResource.axd?d=JnUc-DEDOM5KzzVKtsL1tdEX-qt8s9RxtGk2vMlQxGrK2fF3ftonwGzhez_pN-OzkVlcWDlUr8Qv6P…:1314)
at Array.<anonymous> (ScriptResource.axd?d=D9drwtSJ4hBA6O8UhT6CQjvFd3OFr4OSn3jDIa_YkBYl_kDCdss53aQGXnuBrrAQMiPFhRMC1uQCNM…:47)
at ScriptResource.axd?d=D9drwtSJ4hBA6O8UhT6CQjvFd3OFr4OSn3jDIa_YkBYl_kDCdss53aQGXnuBrrAQMiPFhRMC1uQCNM…:3484
at Sys$Net$WebRequest$completed [as completed] (ScriptResource.axd?d=D9drwtSJ4hBA6O8UhT6CQjvFd3OFr4OSn3jDIa_YkBYl_kDCdss53aQGXnuBrrAQMiPFhRMC1uQCNM…:6373)
at XMLHttpRequest.Sys$Net$XMLHttpExecutor._onReadyStateChange (ScriptResource.axd?d=D9drwtSJ4hBA6O8UhT6CQjvFd3OFr4OSn3jDIa_YkBYl_kDCdss53aQGXnuBrrAQMiPFhRMC1uQCNM…:5993)

另外如果我使用try catch来抛出以下异常

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

1 个答案:

答案 0 :(得分:0)

您的模板字段缺少参数。

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="btnRedirect" runat="server" 
      CommandName="CompleteTransaction" 
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" PostBackUrl="~/WebForm2.aspx"
      Text="Complete Transaction" />
  </ItemTemplate> 
</asp:TemplateField>

在GridviewRowcommand事件

{
//at the end
 Session["Controls"] = list;
 Response.Redirect("~/WebForm2.aspx",false);
}

在下一页中,即在重定向页面中,在表单加载中添加以下代码。

     protected void Page_Load(object sender, EventArgs e)
            {
            Page previousPage = Page.PreviousPage;
            if (previousPage != null && previousPage.IsCrossPagePostBack)
            {
                lblName.ServiceType = ((Label)previousPage.FindControl("lblServiceType")).Text;
                lblEmail.Method= ((Label)previousPage.FindControl("lblMethod")).Text;
                //etc bind the remaining label from the previous page
            }