Server.Transfer with query string

I am starting to learn ASP.NET with C # as a programming language.

I am currently working with HTTPSERVERUTILITY.

I created a web form called Default.aspx and Default2.aspx:

I wrote the following encoding:

Default.aspx:

In the view of the source

     

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

</div>
</form>

In the Code-behind file:

protected void Button1_Click (object sender, EventArgs e) {

    Server.Transfer("Default2.aspx ? name =roseline & password = pass@123");
}

Coding for Default2.aspx:

In the source window:

                   

      

             

      

         

     

In the Code-Behind file:

public string n, p;
protected void Page_Load(object sender, EventArgs e)
{
    n = Request.QueryString["name"];
    p = Request.QueryString["password"];

}
protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = n;
    TextBox2.Text = p;
}

When I execute the above application, I get no errors.

When I click the Button1 button in Default.aspx, it shows me Default2.aspx, but when I click on the button, I don't get the value in the TextBox, the text fields are empty without any values.

Can someone tell me what is wrong with my encoding? Why doesn't it display values ​​in TextBoxes?

, !

!

+3
5

Server.Transfer.

:

Context.Items["ParamName"] = value;

Server.Transfer Response.Redirect

+7

Response.Redirect("default.aspx?name =roseline&password=pass@123");

. Response.Redirect Server.Transfer . .

: , Response.Redirect HttpServerUtility. , Response.Redirect - .

+2

Source.aspx

protected void Button1_Click(object sender, EventArgs e)
    {
        HttpContext _context = HttpContext.Current;
        _context.Items.Add("name", roseline);
        _context.Items.Add("password", pass@123);
       Server.Transfer("Destination.aspx");
    }

Destination.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext _context = HttpContext.Current;
        Response.Write(_context.Items["name"]);
        Response.Write(_context.Items["password"]);
     }
+1
source

You can use this method.

 string url = $"~/Registration.aspx?price={price}&membershipId={membershipId}";
 Server.Transfer(url);

And to read the values, you just need to use:

string membershipId = Request.QueryString["membershipId"];
+1
source

Use Response.Redirect instead:

 Response.Redirect("Default2.aspx?name=roseline&password=pass@123");

However, then your values ​​will be visible in the URL, and this may be impractical. There are many other ways to pass values ​​between page requests; all have their pros and cons.

0
source

Source: https://habr.com/ru/post/1714585/


All Articles