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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Webform through a WebService to ASP.

Status
Not open for further replies.

AGIMA

Programmer
Jul 24, 2003
95
AU
I have a ASP application, this ASP application uses SOAP to communicate to a Webservice that returns HTML. This HTML is create by hand (Which is painfull).

What I want to do is create a web form within the Webservice and return the HTML of the web form through the webservice backt to the orginal ASP application.

There is a technical reason why I dont call the web form directly so this is out of the question, it has to be return as HTML through SOAP.



AGIMA - professional web hosting is our business.

AGIMA Computing
 
That's certainly possible...just have your WebMethod return data of type String and have it include the HTML with the <FORM> tags.
 
I have done what you are asking (I think)

Basically, you need to look into the
&quot;RenderChildren&quot; method of the Page class in asp.net.

The &quot;RenderChildren&quot; method will allow you to basically render an asp.net form into its resulting html string.
You can actually put this resulting html string into a string variable.

I use this method to generate HTML emails from an asp.net webform.

I think the concept is similar to your needs, instead of emailing the resulting html string (as I do), you want to return it as a SOAP XML string.

I can provide further details if you like, or try searching for this topic on the web.

Hope this helps...


Gilbert M. Vanegas
Programmer Analyst III
County of San Bernardino - ISD
Email : gvanegas@isd.sbcounty.gov
 
Gilbert

That sounds exactly what I need, do you have some code that I could use or look at?

AGIMA - professional web hosting is our business.

AGIMA Computing
 
Below is some actual code that I wrote to render html into a string variable.

I use the renderchildren method (this webform has an instance of a user control embedded in it).

I believe you would use the render method instead if your webform does NOT have user controls in it).

I have xxxxxx'd out the mail server variables..

What is cool about this approach is you can have a webform in your asp.net project and render it to a string variable, once it is in a string variable, you could send it as an html email or (as your topic suggests), send it as in a soap response...

Hope this helps.
P.S. Search the web for some articles (I think 4guysfromrolla has a similar topic) that might help you in this area.




Imports System.Web.Mail
Imports System.IO
Imports System.Data
Imports System.Text
Public Class HTMLEMAILFROMTRACKINGSYSTEM
Inherits System.Web.UI.Page
Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar
Protected WithEvents TRACKINGITEMEMAILDETAILS1 As TrackingItemEmailDetails


#Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim SB As New StringBuilder
Dim SW As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)

Try
TRACKINGITEMEMAILDETAILS1.TRKNO = Request.QueryString(&quot;TRKNUMBER&quot;)
If Request.QueryString(&quot;MODE&quot;) = &quot;UPDATE&quot; Then
TRACKINGITEMEMAILDETAILS1.STATUSCHG = Request.QueryString(&quot;STATUSCHG&quot;)
TRACKINGITEMEMAILDETAILS1.TITLE = &quot;A change in status has occurred for tracking number &quot; & Trim$(Request.QueryString(&quot;TRKNUMBER&quot;)) & &quot; &quot; & &quot;  &quot; & &quot;<BR>&quot; & TRACKINGITEMEMAILDETAILS1.STATUSCHG
TRACKINGITEMEMAILDETAILS1.STATUSCHG = Request.QueryString(&quot;STATUSCHG&quot;)
End If
Page.DataBind()
TRACKINGITEMEMAILDETAILS1.RenderBind()
Me.RenderChildren(htmlTW)
Dim strHTMLRENDERED As String = SB.ToString()
Dim objMAIL = New MailMessage
objMAIL.to = &quot;xxxxxxxxxx&quot;
objMAIL.FROM = &quot;xxxxxxx&quot;
objMAIL.subject = &quot;TRACKING SYSTEM&quot;
objMAIL.bodyformat = MailFormat.Html
objMAIL.BODY = strHTMLRENDERED
Response.Write(strHTMLRENDERED)
SmtpMail.SmtpServer = &quot;xxxxxx&quot;
SmtpMail.Send(objMAIL)
Catch ex As Exception

Response.Write(ex.ToString)

End Try
End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Try
Me.Render(writer)
Catch
End Try
End Sub


End Class


Gilbert M. Vanegas
Programmer Analyst III
County of San Bernardino - ISD
Email : gvanegas@isd.sbcounty.gov
 
Well ... How will I call the WebForm directly from the WebService though .... assuming I do not have a problem with that.

I tried Response.Redirect from the WebService but it did not work.
 
Hello MB22.
I think you have the concept backwards in this case.
I think the original post was stating he wants the webservice to send back the html string to a calling web form.

Therefore, if you followed the scenario above, the web service would generate some HTML (could be using the technique I showed above) or you could simply generate the html programatically such as...

dim strHTML as string
strHTML = &quot;<BR>&quot; & &quot;<table></table>&quot; etc...

What I am saying is your web service does some &quot;work&quot; to generate some HTML.
The &quot;invoker&quot; of the web service (your &quot;web form&quot; or &quot;windows form&quot;) would call the web service.

The resulting html string returned from the web service could then simply be displayed in the web form or (in my sample above) sent as an html email.

You should look into &quot;adding a web reference&quot; in your web form if you are actually trying to write a web service and a caller to the web service.


Hope this helps...


Gilbert M. Vanegas
Programmer Analyst III
County of San Bernardino - ISD
Email : gvanegas@isd.sbcounty.gov
 
Well ... here is my scenario

I have already written the web service succesfully. However I only updated the database and retrieved data using the webservice. I did not redirect to another form(say &quot;MyReport.aspx&quot;..from the webservice ... which is what I ant to do now.

I have a full blown database web App. The app also displays reports in some of the pages.

There is a lot of data to supply to generate a report. For other apps that need to communicate with my web app, I created a web service for the other Apps to be able to pass a dataset with all the data to the webservcie. I save the dataset to my DAtabase.

Now from here I'm not sure what to do. I need to use this dataset and generate a report say (&quot;MyReport.aspx&quot;)and pass the report back to the user. You see previously I have only returned an updated dataset back to the user not a report or generated HTML from the webservice.

Are you suggesting from the webservice .. launching &quot;MyReport.aspx&quot; and rendering the output html as a string to pass back to the user.


Can I just launch &quot;MyReport.aspx&quot; from the webservice also......... ....just like I do when I'm using the original Web App by itself?

 
Your last sentence stating
&quot;Am i suggesting from the webservice... launching myreport.aspx rendering the output html as a string to pass back to the user&quot;. Yes, I believe I am stating that is probably a technique to meet what you are describing. You have quite an interesting project there and it sounds like it would be a good one to work with.

Ok, I am kind of understanding what you are wanting, but you see... redirecting to an asp.net webform from a webservice does not quite make sense to me.

I think of a webservice as a &quot;function call&quot; across the internet via tcp/ip, http and soap.

Therefore a webservice always must &quot;return&quot; to the caller.
By your stating redirecting, I think you mean you want to &quot;render&quot; the asp.net webform output into a string variable (which when I say render implies that the string variable will contain the formatted html comprising of the asp.net webform output)
As my example above shows, using the render and renderchildren methods implicit in a web.ui.page class will allow a programmer to &quot;capture&quot; the resulting html into a string variable. The webservice can then send back the string variable to the caller.
I am thinking this is what you are saying
.
Sorry If I am a little dense here, but by all means keep asking, I find the topic interesting.

Actually to take it a little step further as a simple proof of concept, I took a web service project I am using now and...
1) Added an asp.net webform to the web service project
2) Added a public function to the webform called &quot;renderReport&quot;
Here is what I wrote (in webform1.aspx)
public function RenderReport() as string
return &quot;<BR>&quot;
end function

3) Now I created a &quot;SendReporttoCaller&quot; web method in my web service project.
Basically you can do something like...
<WebMethod()> Public Function SendReporttoCaller() As String
Dim objFORM As New WebForm1 ' the webform which creates your reports
Dim strHTML As String = objFORM.RenderReport ' return the string from the public function call to the objform declaration
Return strHTML
End Function
4) When I run the webservice via the asmx file, I see the following.
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?>
<string xmlns=&quot;
So yes, this concept can actually work of course your RenderReport function in your webform will actually call the &quot;render&quot; methods to capture the resulting html into a string.

Take a look at the following article, its pretty good as pertains to your subject.


Gilbert M. Vanegas
Programmer Analyst III
County of San Bernardino - ISD
Email : gvanegas@isd.sbcounty.gov
 
V0520:
I tried Step3 over and over. It did NOT work for rendering a the Page. Yes.. for a simple return string &quot;<BR>&quot; when you call
Dim strHTML As String = objFORM.RenderReport
it works fine....

but like you inferred from Step4 that
&quot;So yes, this concept can actually work of course your RenderReport function in your webform will actually call the &quot;render&quot; methods to capture the resulting html into a string.&quot;

the return string had NOTHING in it. It looks like when you call a webform from a Web Service the PageLoad Event does not execute!

this is the code what I had in WebForm1 instead of return &quot;<BR&quot;

public function RenderReport() as string
Dim SB As New StringBuilder
Dim SW As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)

Page.DataBind()
Me.Render(htmlTW)
Dim strHTMLRENDERED As String = SB.ToString()

return strHTMLRENDERED
end function

strHTMLRENDERED returns nothing! I tried to move the page code ot the Page_Load event, but still the Page_Load event will not execute.

Just try your example again, put a few simple say &quot;HelloWorld&quot; labels or textboxes on WebForm1 and see if you can have WebForm1's html returned.

Thanks for the interest though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top