使用Server.Transfer进行重定向时出错无法分析从服务器收到的消息。

问题描述:

我想通过点击GridView中的链接按钮来使用Server.Transfer重定向到Transaction.aspx页面,这一切都发生在OnRowCommand事件中。 代码链接按钮是使用Server.Transfer进行重定向时出错无法分析从服务器收到的消息。

<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. 
+0

什么是这些行的外??,请亲切的调试,让我们知道。 int rowIndex = Convert.ToInt32(e.CommandArgument); //获取当前行 GridViewRow selectedRow = GridView1.Rows [rowIndex - 1] ;.我的意思是第5行和第6行 –

您的模板字段缺少参数。

<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 
      } 
+0

不,这是行不通的。我其实已经有了命令参数。 –

+0

您是否尝试删除server.transfer行?GridView1_RowCommand事件是否被触发? –

+0

是的,它正在被解雇,response.redirect工作,但我需要使用server.transfer,因为我需要在重定向页面的PreviousPage检查。 –