Reading html from previous page

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

What is the best way to read a section of html from the previous page? I
have used the example below for an individual control, but want to be able
to read the contents of a <div id="mydiv"></div> that was on the sending
page. Thanks.
David

If Not Page.PreviousPage Is Nothing Then
Dim SourceTextBox As TextBox
SourceTextBox = CType(PreviousPage.FindControl("userinfo"),
TextBox)
If Not SourceTextBox Is Nothing Then
userinfo.Text = SourceTextBox.Text
End If
End If
 
you will need to create a HtmlTextWriter, and pass it to the Render method of
the div. be sure all properties of the div's child controls are set before
doing the server transfer. any control that counts on prerender will have
troubles, so there are some restrictions on what controls will work


-- bruce (sqlwork.com)
 
Bruce,
I'm not clear on what you mean. I am trying to create a generic emailing
page that will read the contents of a specific DIV (based on id) from the
calling page and plop it into the body of an smtp email. Currently, I have
one page that emails a GridView (table) that is in that same page. However,
the user has requested this same functionality from other pages, so I
thought I could just require a DIV with a specific id and when the email
aspx page opens, it would read it and send the email...then return back to
the calling page. Thanks.

David
 
David said:
Bruce, I'm not clear on what you mean. I am trying to create a generic
emailing page that will read the contents of a specific DIV (based on
id) from the calling page and plop it into the body of an smtp email.
Currently, I have one page that emails a GridView (table) that is in
that same page. However, the user has requested this same functionality
from other pages, so I thought I could just require a DIV with a
specific id and when the email aspx page opens, it would read it and
send the email...then return back to the calling page. Thanks.

Does the user ever see the email aspx page? The problem is if the button on
the main page posts to your email page, the email page will only have access
to form controls (textboxes, dropdown boxes, etc). The email page won't
have access to html elements on the page (e.g. Divs).

If the user doesn't see (or needs to see) the email page, two ideas come to
mind.

1. Open your email page in a pop-up window via JavaScript when the button
is clicked. You can pass the data to the email page via JavaScript.

2. Probably better, create an Ajax-enabled web service you can pass the Div
contents to when a button is clicked. The user never leaves the page, but
the email is sent and you can show the user a "email sent" message when the
web service returns a successful result.

Ben
 
David said:
Bruce, I'm not clear on what you mean. I am trying to create a generic
emailing page that will read the contents of a specific DIV (based on
id) from the calling page and plop it into the body of an smtp email.
Currently, I have one page that emails a GridView (table) that is in
that same page. However, the user has requested this same functionality
from other pages, so I thought I could just require a DIV with a
specific id and when the email aspx page opens, it would read it and
send the email...then return back to the calling page. Thanks.

Does the user ever see the email aspx page? The problem is if the button on
the main page posts to your email page, the email page will only have access
to form controls (textboxes, dropdown boxes, etc). The email page won't
have access to html elements on the page (e.g. Divs).

If the user doesn't see (or needs to see) the email page, two ideas come to
mind.

1. Open your email page in a pop-up window via JavaScript when the button
is clicked. You can pass the data to the email page via JavaScript.

2. Probably better, create an Ajax-enabled web service you can pass the Div
contents to when a button is clicked. The user never leaves the page, but
the email is sent and you can show the user a "email sent" message when the
web service returns a successful result.

Ben
 
Ben said:
The problem is if the button on the main page posts to your email page,
the email page will only have access to form controls (textboxes,
dropdown boxes, etc). The email page won't have access to html elements
on the page (e.g. Divs).

I take that back. If the Div on the original page is set runat="server",
then you can access the div on the page posted to. I personally find the
web service a better approach, but ...

if (PreviousPage.IsCrossPagePostBack)
{
System.Web.UI.HtmlControls.HtmlGenericControl div =
PreviousPage.FindControl("userinfo") as
System.Web.UI.HtmlControls.HtmlGenericControl;

if (div != null)
string DivContent = div.InnerHtml;
}

.... or in VB ...

If (PreviousPage.IsCrossPagePostBack) Then

Dim div As System.Web.UI.HtmlControls.HtmlGenericControl = _
TryCast(PreviousPage.FindControl("userinfo"), _
System.Web.UI.HtmlControls.HtmlGenericControl)

If Not IsNothing(div) Then
Dim DivContent As String = div.InnerHtml
End If

End If
 
Ben,
Currently, the user does not need to see the email page. I was hoping to do
everything in the Page_Load event and then redirect back to the page that
called OR just do it from a pop-up window. I am not familiar with coding a
web service so I wouldn't have any idea how to handle that.

David
 
David said:
Ben, Currently, the user does not need to see the email page. I was
hoping to do everything in the Page_Load event and then redirect back to
the page that called OR just do it from a pop-up window. I am not
familiar with coding a web service so I wouldn't have any idea how to
handle that.

Okay. You can still access the Div on a crosspage postback (see my other
post). Or you can use the pop-up window. One other option is to perform a
regular postback (non-crosspage), and call a function in a custom class you
have to send the email. This class would go in the App_Code folder and
would be accessible by any page in your project. This last option is more
of a traditional approach in ASP.NET. Good luck.
 
with some restictions on the controls in the div, do the following:

var div = Page.PreviousPage.FindControl("divName');
if (div != null)
{
// render the div

var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
{
using (var htmlTW = new HtmlTextWriter(sw))
{
div.RenderControl(htmlTW);
}
}

// get the html

var html = sb.ToString();
}

-- bruce (sqlwork.com)
 
Back
Top