2013-12-19 3 views
2

У меня есть класс в C#, называемый «Point».Отправить параметр на другую страницу ASP.Net

public class Point(){ 
. 
. 
} 

В page1.aspx Я создал:

Point p1 = new Point(); 

Я хочу, чтобы отправить его в page2.aspx. я пытаюсь отправить с:

Response.Redirect("~/page2.aspx?x=p1"); 

И получить его в странице 2 с:

Point p2 =Request.QueryString["x"]; 

Это не работает. Не могли бы вы мне помочь?

+0

Какие свойства имеет точка? какая информация должна быть передана –

+0

Вы не можете передать объект в строку запроса. – afzalulh

ответ

3

Вы хотите использовать Session вместо QueryString

Session["myPoint"] = p1; 

А потом на page2.aspx

p2 = (Point)Session["myPoint"] 
5

Помимо того, что вы не можете просто поставить «p1» в string и ссылаться на экземпляр класса, вы не можете просто добавить объект в качестве аргумента запроса.

Вам нужно будет добавить аргументы для URL-адреса для каждого элемента Point. Например:

Response.Redirect(String.Format("~/page2.aspx?x={0}&y={1}", p1.x, p1.y)); 

В качестве альтернативы, вы можете использовать Session, если она не нужна в качестве аргумента запроса.

1

Нет, вы не можете напрямую передать объект в качестве строки запроса. невозможно.

Есть три способа передачи данных между двумя ASPX страниц

1) с использованием Response.Redirect()/Server.Transfer()

2) с помощью сеанса

3) с использованием общественного свойства

здесь я определил пример для каждого из них

1) using Server.Transfer() 

исходный код страницы Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      <div> 
       <asp:Label ID="lblUsername" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond" 
        Font-Size="Large" Style="z-index: 100; left: 240px; position: absolute; top: 32px" 
        Text="Username" Width="73px"></asp:Label> 
       <br /> 
       <asp:Label ID="lblPassword" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond" 
        Font-Size="Large" Style="z-index: 101; left: 237px; position: absolute; top: 80px" 
        Text="Password" Width="80px"></asp:Label> 
       <br /> 
       <asp:TextBox ID="txtPassword" runat="server" Style="z-index: 102; left: 355px; position: absolute; 
        top: 80px" TextMode="Password" Width="151px"></asp:TextBox> 
       <asp:TextBox ID="txtUsername" runat="server" Style="z-index: 103; left: 357px; position: absolute; 
        top: 30px" Width="153px"></asp:TextBox> 
       <asp:Label ID="lblMessage" runat="server" Font-Bold="False" Font-Names="Bookman Old Style" 
        Font-Size="Medium" Style="z-index: 104; left: 354px; position: absolute; top: 130px" 
        Text="Message :"></asp:Label> 
       <asp:Button ID="btnSubmit" runat="server" Font-Bold="True" Font-Names="Garamond" 
        Font-Size="Large" OnClick="btnSubmit_Click" Style="z-index: 106; left: 289px; 
        position: absolute; top: 160px" Text="Submit" /> 

      </div> 
     </div> 
    </form> 
</body> 
</html> 

код за

using System; 
using System.Configuration; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
    string s1, s2; 

    protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     s1 = txtUsername.Text; 
     s2 = txtPassword.Text; 

     if ((s1 == "sa") && (s2 == "123qwe")) 
     { 
      /* 
      * Passing data using QueryString. 
      */ 
      //Response.Redirect("Description.aspx?Username=&Password=" + s1 + " " + s2); 
      Server.Transfer("Description.aspx?Username=&Password=" + s1 + " " + s2); 
     } 
     else 
     { 
      lblMessage.Text = "Invalid Username and Password"; 
     } 
    } 
} 

страница назначения

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Description.aspx.cs" Inherits="Description" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Label ID="lblResult" runat="server" Font-Bold="True" Font-Names="Garamond" Font-Size="X-Large" 
      Height="22px" Style="z-index: 100; left: 307px; position: absolute; top: 19px" 
      Text="Result" Width="71px"></asp:Label> 
    </div> 
    </form> 
</body> 
</html> 

код за

using System; 
using System.Configuration; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 

public partial class Description : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     //For Response.Redirect - do this 

     //string username = Request.QueryString["Username"]; 
     //string password = Request.QueryString["Password"]; 
     //lblResult.Text = "Username : " + " Password : " + password; 

     //Below is for Server.Transfer() 

     if (Page.PreviousPage != null) 
     { 
      TextBox SourceTextBox_1 = 
       (TextBox)Page.PreviousPage.FindControl("txtUsername"); 
      TextBox SourceTextBox_2 = 
       (TextBox)Page.PreviousPage.FindControl("txtPassword"); 
      if (SourceTextBox_1 != null) 
      { 
       lblResult.Text = SourceTextBox_1.Text + " " + SourceTextBox_2.Text ; 
      } 
     } 

    } 
} 

2) Response.Redirect() и сессий объяснил что два брата здесь, мне не нужно обсуждать об этом.это ясно объяснено там

3) using properties 

исходный код страницы

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      <div> 
       <asp:Label ID="lblUsername" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond" 
        Font-Size="Large" Style="z-index: 100; left: 240px; position: absolute; top: 32px" 
        Text="Username" Width="73px"></asp:Label> 
       <br /> 
       <asp:Label ID="lblPassword" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond" 
        Font-Size="Large" Style="z-index: 101; left: 237px; position: absolute; top: 80px" 
        Text="Password" Width="80px"></asp:Label> 
       <br /> 
       <asp:TextBox ID="txtPassword" runat="server" Style="z-index: 102; left: 355px; position: absolute; 
        top: 80px" TextMode="Password" Width="151px"></asp:TextBox> 
       <asp:TextBox ID="txtUsername" runat="server" Style="z-index: 103; left: 357px; position: absolute; 
        top: 30px" Width="153px"></asp:TextBox> 
       <asp:Label ID="lblMessage" runat="server" Font-Bold="False" Font-Names="Bookman Old Style" 
        Font-Size="Medium" Style="z-index: 104; left: 354px; position: absolute; top: 130px" 
        Text="Message :"></asp:Label> 
       <asp:Button ID="btnSubmit" runat="server" Font-Bold="True" Font-Names="Garamond" 
        Font-Size="Large" OnClick="btnSubmit_Click" Style="z-index: 106; left: 289px; 
        position: absolute; top: 160px" Text="Submit" /> 
      </div> 
     </div> 
    </form> 
</body> 
</html> 

код за

using System; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
    private string myUserName; 
    /* 
    * Defining Properties in the source page to be Accessible on the destination page. 
    * means Exposing data to other pages using Properties 
    * To retrieve data from source page,Destination page must have 
    * <%@ PreviousPageType VirtualPath="~/Default.aspx" %> Directive added below <%@ Page %> Directive 

    */ 
    public string propUserName 
    { 
     get { return myUserName; } 
     set { myUserName = value; } 
    } 
    private string myPassword; 

    public string propPassword 
    { 
     get { return myPassword; } 
     set { myPassword = value; } 
    } 


    protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     if ((txtUsername.Text == "chandan") && (txtPassword.Text == "niit")) 
     { 
      myUserName = txtUsername.Text; 
      myPassword = txtPassword.Text; 
     } 
     Server.Transfer("Description.aspx"); 
    } 
} 

назначения страница

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Description.aspx.cs" Inherits="Description" %> 
<%@ PreviousPageType VirtualPath="~/Default.aspx" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      &nbsp; 
      <asp:Label ID="Label2" runat="server" Text="Password" style="z-index: 100; left: 336px; position: absolute; top: 69px" Font-Bold="True" Font-Size="Larger"></asp:Label> 
      <asp:Label ID="Label1" runat="server" Text="UserName" style="z-index: 102; left: 333px; position: absolute; top: 28px" Font-Bold="True" Font-Size="Larger"></asp:Label> 
     </div> 
    </form> 
</body> 
</html> 

код за

using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class Description : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Label1.Text = PreviousPage.propUserName; 
     Label2.Text = PreviousPage.propPassword; 

    } 
} 
Смежные вопросы