简体   繁体   中英

ASP.NET I can't Transfer Data to Another Page

buttonclick

2.pageload

protected void lnkotel1_Click(object sender, EventArgs e)
    {
        Session["xd"] = lblotelid1.Text;



        Server.Transfer("~/otelsayfa.aspx");



        Response.Redirect("otelsayfa.aspx?id=" + lblotelid1.Text);
    }

 protected void Page_Load(object sender, EventArgs e)
    {

        lblotelid.Text = Session["xd"].ToString();




        var last = PreviousPage;
        if (last != null)
        {
            lblotelid.Text = ((TextBox)last.FindControl("TextBox1")).Text;
        }
        else
        {
            lblotelid.Text = "no";
        }




        if (Request.QueryString.Get("id") == null)
        {
            lblotelid.Text = "no";
        }
        else
        {
            lblotelid.Text = Request.QueryString["id"].ToString();
        }

I tried 3 different variation but all of them have failed. Here is the events after i run this codes one by one:

1st code: I get an error message says: System.Web.SessionState.HttpSessionState.this[string].get, null

2nd code: blank page

3rd code: "no"(because of else condition). If i remove else condition i get same error message as 1st code.

I am trying to solve this for days now. And I cant find any flaws. I honostly appreciate any kind of help now.

edit: I can transfer data from.master page to any.aspx page without any problem. But from.aspx page to diffirent.aspx page, it is just impossible. There is no way.

I believe that this answer on SO fits best.

By setting the PostBackUrl on your button, the data from your current page with get sent to your next page as part of the Request collections.

<asp:LinkButton id="lnkotel1" Text="Click Me!" runat="server" PostBackUrl="page2.aspx"/> 

To test for the crosspage postback, the MSDN link gave this example (I changed it to C#):

if(PreviousPage != null)
{
 

    if(PreviousPage.IsCrossPagePostBack == true){
         //Cross-page post
     }
     else 
     {
       //Not a cross-page post
     }
}

Then access the value of your original textbox like so:

((TextBox)PreviousPage.FindControl("lblotelid1")).Text

I used Listview/Formview and it worked.

For sending data by session you should do these in the first page:

Asp form:

<asp:Button ID="lblotelid1" runat="server" OnClick="lblotelid1_Click" Text="Yes" />

Code Behind:

    protected void lblotelid1_Click(object sender, EventArgs e)
    {
        Session["xd"] = lblotelid1.Text;

        Response.Redirect("~/otelsayfa.aspx");
    }

and on the second page

Asp form:

<asp:Label ID="lblotelid" runat="server"></asp:Label>

Code Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["xd"] != null)
            lblotelid.Text = Session["xd"].ToString();
        else
            lblotelid.Text = "No";
    }

Whatever you assign to session["xd"] in the first page, would assign to text of lblotelid label in second page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM