Results 1 to 11 of 11

Thread: When should I use Response.Write() ?

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    When should I use Response.Write() ?

    The problem I'm having, is that my page is outputting my data, before the <html> ... </html> tags. So, my stuff comes out, and then IIS is sending out the normal HTML content.
    How can I have my page output data inside the html and body tags ?

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load    
    2.         Dim strArr() As String : strArr = Split(Request.QueryString("Code"), ",")
    3.         Dim strTableName As String, strSQLStatement
    4.         Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    5.         Dim lngTemp As Long : myConnection.Open()
    6.  
    7.         Select Case strArr(0)
    8.             Case "1" : strTableName = "tblMasterHeaders"
    9.             Case "2" : strTableName = "tblSecondaryHeaders"
    10.             Case "3" : strTableName = "tblThirdHeaders"
    11.             Case "4" : strTableName = "tblThirdHeaders"
    12.         End Select
    13.         lngTemp = strArr(0)
    14.         If Not strTableName = vbNullString Then
    15.             strSQLStatement = "SELECT * FROM " & strTableName & vbCrLf
    16.             If Not strTableName = "tblMasterHeaders" Then
    17.                 If strArr(0) = 4 Then
    18.                     strSQLStatement += "WHERE lngCurrentIndex = " & strArr(1)
    19.                 Else
    20.                     strSQLStatement += "WHERE lngParentIndex = " & strArr(1)
    21.                 End If
    22.             End If
    23.             Dim myCommand As New SqlCommand(strSQLStatement, myConnection)
    24.             Dim myReader As SqlDataReader : myReader = myCommand.ExecuteReader()            
    25.             Try
    26.                 While myReader.Read()
    27.                     If lngTemp = 1 Then
    28. [b]                        Response.Write(strGenerateHeaderLink(2, strArr(1), myReader.GetValue(2)) & vbCrLf & vbCrLf)[/b]
    29.                     ElseIf lngTemp = 2 Then
    30. [b]                        Response.Write(strGenerateHeaderLink(3, myReader.GetValue(0), myReader.GetValue(2)) & vbCrLf)[/b]
    31.                     ElseIf lngTemp = 3 Then
    32. [b]                        Response.Write(strGenerateHeaderLink(4, myReader.GetValue(0), myReader.GetValue(2)) & vbCrLf)[/b]
    33.                     ElseIf lngTemp = 4 Then
    34.                         Dim strSAPCodes() As String = Split(myReader.GetValue(3), ",")
    35.  
    36.                         Response.Write(strSendComment("start of productTableHeader.html"))
    37.                         Response.WriteFile("productTableHeader.html")
    38.                         Response.Write(strSendComment("end of productTableHeader.html"))
    39.  
    40.                         Dim strTableLocation As String
    41.                         strTableLocation += "<tr>" & vbCrLf
    42.                         strTableLocation += "<td width=" & Chr(34) & "90%" & Chr(34) & " colspan=" & Chr(34) & 5 & Chr(34) & ">" & strDoFormatting(Replace(strGetProduct(strSAPCodes(0), modMisc.prodDetailsEnum.strSectionText), "|", " &nbsp; &gt;&gt; &nbsp; ")) & "</td>" & vbCrLf
    43.                         strTableLocation += "</tr>" & vbCrLf
    44.                         Response.Write(strTableLocation)
    45.  
    46.                         Dim i As Long
    47.                         For i = 0 To UBound(strSAPCodes)
    48.                             Response.Write(strGetProductDescriptionBySAPCode(strSAPCodes(i)) & vbCrLf)
    49.                         Next
    50.  
    51.                         Response.Write(strSendComment("start of productTableFooter.html"))
    52.                         Response.WriteFile("productTableFooter.html")
    53.                         Response.Write(strSendComment("end of productTableFooter.html"))
    54.                     End If
    55.                 End While
    56.             Finally
    57.                 myReader.Close()
    58.                 myConnection.Close()
    59.             End Try
    60.         End If
    61.     End Sub

    The above code is coming out as :

    Code:
    <a href="ProductGroup.aspx?Code=2,1">Backup Solutions</b><br><a href="ProductGroup.aspx?Code=2,2">Boards (video, sound, .)</b><br><a href="ProductGroup.aspx?Code=2,3">CAD Peripherals</b><br><a href="ProductGroup.aspx?Code=2,4">CAD Software</b><br><a href="ProductGroup.aspx?Code=2,5">Connectivity (hub, switch, router,.)</b><br><a href="ProductGroup.aspx?Code=2,6">Courses & Training</b><br><a href="ProductGroup.aspx?Code=2,7">CPU, memory, SIMMs,.</b><br><a href="ProductGroup.aspx?Code=2,8">Drives (FD, HD, etc.)</b><br><a href="ProductGroup.aspx?Code=2,9">Entertainment Software</b><br><a href="ProductGroup.aspx?Code=2,10">Extended Warranty</b><br><a href="ProductGroup.aspx?Code=2,11">Handhelds & Notepads</b><br><a href="ProductGroup.aspx?Code=2,12">Keyboards, Mice, etc.</b><br><a href="ProductGroup.aspx?Code=2,13">Monitors & Displays</b><br><a href="ProductGroup.aspx?Code=2,14">Networking Hardware</b><br>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    	<HEAD>
    		<title></title>
    		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    		<meta name="vs_defaultClientScript" content="JavaScript">
    		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    	</HEAD>
    	<body MS_POSITIONING="GridLayout">
    	</body>
    </HTML>
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You are approaching ASP.NET in the wrong way. You are looking at as if it was ASP 3.0. You shouldn't need to use response.write statements if you are coding correctly. I have only seen one or two instances where this would be necessary, but I am sure even in those cases it could have been avoided with a little thought.

    You are just writing out tables, which .Net has a control for you to use. Drop the table control on your page, then you can programatically (sp?) add table rows to it when looping through a datasource if you want. What this does is allow you to completely seperate code and content, something you are not doing now. When you do this, you can change the style of the table on the aspx page, but the code will never have to be touched. This is the goal.

    We talked about this at a local study/focus group, and it is amazing how often people do this because of past habbits. What we all strive for is NO html in our code. This is extremely easy once you get used to it.

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I'd go along with that last bit completely. For a single value, you could look to use a label on the webform to write the value to.

    For the above, try looking up databind() and datagrid in the msdn library...

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    And if you truly feel you just can't do it with complete seperation (which shouldn't be the case), use a literal control and assign your html to its text property. It renders out its text as html, unlike the label.

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Hellswraith is right unlees your using a free brinkster account and you can't use code behind.
    Magiaus

    If I helped give me some points.

  6. #6
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Wait.

    I'm having to reconsider this. For instance I have an ImageList control that is basicly just a .net wrapped javascript array of ImageObjects one of it's methds is
    Code:
    public string ImageTag(int index);
    and about 10 or 15 overloads. It returns a string containg an image tag for the specified indexed image. I use response.write to write it on the page. What would be the way to do this?

    edit:
    n/m sorry I didn't read everything before I started running my mouth......
    Last edited by Magiaus; Mar 28th, 2004 at 07:18 PM.
    Magiaus

    If I helped give me some points.

  7. #7

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    *mutter*grumble*mutter*
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Okay. I shouldn't be using HTML myself.
    Then tell me how I should go about doing this then.

    I have attached a zip with 4 files.
    Basically, the index, and then going 3 levels deep to a product.
    How would I implement something that looked like that, without requiring the user to have the .net framework or some other complicated thing installed?

    This is what baffles me you see...
    Attached Files Attached Files
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Like alex_read said the datagrid and databind.

    None of the "Web Forms" controls require the .NET framework.

    I would suggest learning proper ASP.NET first through a book or tutorial. I found the book "Teach Yourself ASP.NET in 21 Days" very useful and easy to follow. (Most of the stuff was VERY basic but the Datagrid info was pretty useful)

    Good luck.
    Last edited by wey97; Mar 29th, 2004 at 09:35 AM.

  10. #10
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    well, the bit about not using html is not correct. one of the major goals of .net though is provide seperation of asp code and html code. so edit your html and edit your code but don't mix them together via <% %>. use codebehind or server side script blocks for server code and do html like always. And frankly I use Response.Write plenty..... but I use in a script block and if I'm not using Response.Write I use an HtmlWriter.
    Magiaus

    If I helped give me some points.

  11. #11
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    the funny thing is you'll see a lot of ASP.NET books combining both <%script > and HTML in the same listing in their examples.

    I think this is why some people go this way. Myself, I always found it confusing considering I really only started dynamic web development since the dawn of ASP.NET.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width