S.A.Parkhid
S.A.Parkhid

Reputation: 2870

Why does Response.Write show up before the controls on the page?

In this code , why does text for response.write appear before the text in the label? Here is the code behind:

protected void Page_Load(object sender, EventArgs e)
{
    HttpRequest request = Request;
    if (request.QueryString["game"] == "cstrike") Label1.Text = "15000 $";
    if (request.QueryString["game"] == "diablo3") Label1.Text = "25000 $";
    if (request.QueryString["game"] == "pes2012") Label1.Text = "30000 $";
    if (request.QueryString["game"] == "splinterCellConviction") Label1.Text = "75000 $";
    Response.Write("<span>Query:<span>" + request.QueryString.ToString());
} 

For example , the output looks like this:

Query: game=cstrike 15000$

why did it output in this order? The "asp label control" is above all the controls on the page :

I expected the output to look like this :

15000$ Query: game=cstrike

here is the asp.net page code front:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Pricing.aspx.cs" Inherits="queryStringPlay.Pricing" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>

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

Upvotes: 0

Views: 1970

Answers (2)

user94547
user94547

Reputation:

The normal rendering of the page is being preempted by your call to response.write. Here is the order of events in asp.net http://msdn.microsoft.com/en-us/library/ms178472.aspx

Your call to response.write is basically saying "go ahead and write this value to the stream without waiting for the rest of the rendering pipeline to happen". Its my opinion that this is bad form and that you should place a literal on the page that contains the 15000$ value. Then have your code just set the text value of that literal.

Upvotes: 1

Yahia
Yahia

Reputation: 70379

The compiler does everything you tell it... you can verify that by stepping through the code in a debugger... in this case you are using Response.Write and that gets sent to the client... the things you did before are not sent to the client because by using Response.Write you basically tell the ASP.NET runtime to "ignore" them!

What should that code accomplish exactly ?

Upvotes: 1

Related Questions