ASPX page loses formatting after executing script

ccbryan

Active member
Joined
Oct 14, 2008
Messages
30
Programming Experience
5-10
Hi. I am an ASP newbie working on someone else's page, and I need a little guidance...

The page (call it Page A) contains formatting (CSS style sheet). I have added a button to the form that displays a Word document in a new window.

VB.NET:
   Protected Sub btnSpecSheet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSpecSheet.Click
        Dim specstyle As String
        If Me.txtStyle.Text > "" Then
            Try
                specstyle = Me.txtStyle.Text
                Response.Write("<script>")
                Response.Write("window.open('Specsheets/" & specstyle & ".doc','_blank')")
                Response.Write("</script>")
                Me.txtStyle.Text = ""
            Catch ex As Exception
                Dim cerror As String
                cerror = ex.InnerException.ToString()
            End Try

        End If
    End Sub

This works fine, but when the specsheet document is closed, Page A has lost some of its style sheet formatting. That is, the page (within the browser window) has shifted to the left and formatting of a contained grid control is lost (de-bolded, column alignment).

There is a button ('Submit') that the previous developer created that kicks off some VB processing and refreshes the page with the proper datagrid, and this button does not have the formatting effects of my btnspecsheet. Debugging, I can see that Submit sends the process back through the page's HTML, though I can't figure out why.

button Submit's VB code:

VB.NET:
    Protected Sub ButtonSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSubmit.Click
        Dim strSelected As String = Trim(RadioButtonListInquiryType.SelectedItem.Text)

        If strSelected = "Invoices" Then
            Me.lblStyle.Visible = False
            Me.txtStyle.Visible = False
            Me.btnSpecSheet.Visible = False
            If Me.GridViewOpenInvoices.Rows.Count < 1 Then
                Me.LabelMessage.ForeColor = Drawing.Color.Green
                Me.LabelMessage.Text = "No records found.."
                Me.LabelMessage.Visible = True
            Else
                Me.GridViewOpenInvoices.Visible = True
            End If
        Else
            Me.GridViewOpenInvoices.Visible = False
        End If

        If strSelected = "Credit Memos" Then
            Me.lblStyle.Visible = False
            Me.txtStyle.Visible = False
            Me.btnSpecSheet.Visible = False
            If Me.GridViewcreditMemos.Rows.Count < 1 Then
                Me.LabelMessage.ForeColor = Drawing.Color.Green
                Me.LabelMessage.Text = "No records found.."
                Me.LabelMessage.Visible = True
            Else
                Me.GridViewcreditMemos.Visible = True
            End If
        Else
            Me.GridViewcreditMemos.Visible = False
        End If

        If strSelected = "Orders" Then
            Me.lblStyle.Visible = False
            Me.txtStyle.Visible = False
            Me.btnSpecSheet.Visible = False
            If Me.GridViewOrders.Rows.Count < 1 Then
                Me.LabelMessage.ForeColor = Drawing.Color.Green
                Me.LabelMessage.Text = "No records found.."
                Me.LabelMessage.Visible = True
            Else
                Me.GridViewOrders.Visible = True
            End If
        Else
            Me.GridViewOrders.Visible = False
        End If
        
        If strSelected = "Spec Sheets" Then
            Me.lblStyle.Visible = True
            Me.txtStyle.Visible = True
            Me.btnSpecSheet.Visible = True
        End If

    End Sub

And here's button Submit's HTML. Actually, it's all the code between the end of a radiobuttonlist ("</asp:RadioButtonList>") and the beginning of a datagrid ("<asp:GridView"):

VB.NET:
                    <asp:Button ID="ButtonSubmit" runat="server" Enabled="False" Font-Bold="True" 
                        ForeColor="Green" Text="Submit" Width="220px" />  
                   <br />   
                   <asp:Button ID="BacktoGrid" runat="server" Font-Bold="True" ForeColor="Green" 
                          Text="Back To List" Visible="False" Width="104px"  />
                   <span style="font-size: 7pt"> 
                      <asp:Label ID="lblStyle" runat="server" Font-Bold="True" Font-Size="Large" ForeColor="Green" 
                          Text="Enter Style:" Width="97px" Visible="False"></asp:Label> 
                      <asp:TextBox ID="txtStyle" runat="server" Visible="False"></asp:TextBox>    
                      <asp:Button ID="btnSpecSheet" runat="server" Text="Spec Sheet" Visible="False" />
                   </span>
                   <br /> 
                   <asp:Label ID="LabelMessage" runat="server" Font-Bold="True" Font-Size="Medium" 
                          ForeColor="Red" Text="Place message here." Visible="False"> </asp:Label>                      
                   <br />

Btnspecsheet does not go back through the HTML, and I think this is the problem, but how do I get it to do so? I suspect that this has something do do with postback, but I'm too ignorant at the moment to proceed.

Any thoughts?

Chandler
 
Hi

from my experience using response.write breaks the css styling on a page so I think that is what is happening for you. One way around this would be to add the window.open bit to the OnClientClick attributes of btnSpecSheet and use javascript's document.getElementById method to get the value of the text box, so your button declaration in the .aspx page would look something like this...


<asp:Button ID="btnSpecSheet" runat="server" Text="ButtonText" OnClientClick='window.open("Specsheets/" + document.getElementById("txtStyle").value + ".doc", "_blank");' />

alternatively you could put the windo.open bit inside a javascript function that checks to make sure that txtStyle is not empty and then returns false if it is so that the page doesnt post back and then call the function from the OnClientClick part

If you need any help with that let me know.

Hope this helps

Mike
 
Last edited:
Hi Mike, thanks for the reply.

A suggestion from a colleague eventually got me around the problem by putting an empty scriptliteral at the very bottom of the page, and then when I want to open a document I do this in my codebehind:

VB.NET:
 If fs.FileExists(MainPath & "PDF/Packlists/" & strBOL & "-0-" & Session("Customer") & ".pdf") = True Then
                            PDF = strBOL & "-0-" & Session("Customer") & ".PDF"
                            scriptLiteral.Text = "<script type=""text/javascript"">window.open('PDF/Packlists/" & PDF & "','_blank')</script>"
End If

And when I want to open a 'popup' window for composing an email...

VB.NET:
strstring = "email.aspx?DOCNO=" & strOrderNo & "&type=" & stremailtype & "&type=" & Session("Cust_No") & "&efrom=" & strFrom
            scriptLiteral.Text = "<script type=""text/javascript"">window.open('" & strstring & "','_blank','height=500 width = 500')</script>"

This seems to take care of the problem because I'm never actually leaving my main page, so CSS formatting is unaffected.

And sorry, I can't figure out how to mark the thread "solved."
 
Last edited:
Back
Top