3
votes

I am beginner learning ASP.NET with C# as the programming language.

Currently I am working with HTTPSERVERUTILITY.

I have created a web form named as Default.aspx and Default2.aspx:

I have written the following coding :

Default.aspx:

In source view

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

</div>
</form>

In 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 Source View:






In 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 am not getting any error.

When I click on the Button1 in Default.aspx it shows me the Default2.aspx, but when I click on the button I am not getting the values in the TextBox, the TextBoxes are empty without any values.

Can anyone tell me what is wrong with my coding? Why it is not displaying the values in the TextBoxes?

Please help me out!

Thanks in advance!

5

5 Answers

9
votes

You can't append a query string in Server.Transfer.

You can pass values in Context

Eg:

Context.Items["ParamName"] = value;

You can get the differences between Server.Transfer and Response.Redirect from here

2
votes

Try using

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

instead. More information on the differenses between Response.Redirect and Server.Transfer see this page.

EDIT: Responded a little to fast, Response.Redirect doesn't use the HttpServerUtility class off course. However I would say that using Response.Redirect is the normal way to go about this.

2
votes

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"]);
     }
2
votes

You can use this way.

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

And to read the values simply you need to use:

string membershipId = Request.QueryString["membershipId"];
0
votes

Use Response.Redirect instead:

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

However, then your values will be visible in the url, and that may not be practical. There are numerous other ways of transferring values between page requests, all have their pros and cons.