Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing using registerclientscriptblock

Status
Not open for further replies.

daveonion

Programmer
Aug 21, 2002
359
GB
Hi,

I copied a function to print out any control passed into it, however it uses Response.Write which after doing some research i discovered it is not compatible with Ajax update panels, i researched some more and discovered that you should now use registerclientscriptblock, however i am unsure as to what the syntax should be, i have copied the code i have below, hopefully i havent been too naieve and it is actually possible to get the function to work this way, any help would be massivley appreciated.

thanks

Public Shared Sub PrintWebControl(ByVal ctrl As Control, ByVal Script As String)
Dim stringWrite As StringWriter = New StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWrite)
If TypeOf ctrl Is WebControl Then
Dim w As Unit = New Unit(100, UnitType.Percentage)
CType(ctrl, WebControl).Width = w
End If
Dim pg As Page = New Page()
pg.EnableEventValidation = False
If Script <> String.Empty Then
pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script)
End If
Dim frm As HtmlForm = New HtmlForm()
pg.Controls.Add(frm)
Dim lbl As New Label
frm.Attributes.Add("runat", "server")
frm.Controls.Add(ctrl)
pg.DesignerInitialize()
pg.RenderControl(htmlWrite)
Dim strHTML As String = stringWrite.ToString()
lbl.Text = strHTML
pg.Controls.Add(lbl)

'this is where i replaced response.write("<Script>window.print.</script>")
pg.ClientScript.RegisterClientScriptBlock(pg.GetType(), "PrintJavaScript", "<script>window.print();</script>")
 
Without looking to deep or thinking too hard about your question..

What I have done rather than use response.write (which places the return before any html content and often blows up the way a page is displayed) is to use the asp:literal control.

This in conjunction with a stringbuilder object seems to solve the ajax issue. (the reason response.write doesn't work is that the response is outside an ajax update panel.

so..quickly

in page add an asp:updatepanel where you want the output printed.

inside the contenttemplate add an asp:literal control
e.g.
Code:
<asp:UpdatePanel runat='server' ID='updWrite'>
<ContentTemplate>
<asp:Literal runat='server' ID='litWrite' EnableViewState="false" />
</ContentTemplate>
</asp:UpdatePanel>

In the code behind loop through any array or set of controls or recordset adding to the string builder and then send the output to the literal control.. (literal controls are really just locations where you can output html.. they have no properties without the text you send them so are very usefull)

e.g.

Code:
dim s()={1,2,3,4,5,6,7,8,9,0}
dim sb as new stringbuilder()
for i as integer = 0 to s.lenght-1
 sb.append(s(i))
next
litWrite=sb.tostring()

See if it fits your requirements, my guess is it will.


Rob
 
I should look before i post... the last code example will fail...
It should be..

litWrite.text =sb.tostring()


it could also be
litWrite.text ="<div class='bluetext'><h4>" & sb.tostring() & "</h4></div>"

 
OK... so then i did look at the original post.. (Maybe i should read before i start to type)

The issue more likely than not is that your script needs to run in an ajax updatepanel..

My advice might be - take a different approach.. Look into jquery and consider making an ajax request that way. You should then get greater control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top