Reading html from previous page

Discussion in 'ASP .Net' started by David C, Nov 24, 2008.

  1. David C

    David C Guest

    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
     
    David C, Nov 24, 2008
    #1
    1. Advertising

  2. David C

    bruce barker Guest

    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)


    "David C" wrote:

    > 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
    >
    >
    >
     
    bruce barker, Nov 24, 2008
    #2
    1. Advertising

  3. David C

    David C Guest

    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
    "bruce barker" <> wrote in message
    news:...
    > 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)
    >
    >
    > "David C" wrote:
    >
    >> 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
    >>
    >>
    >>
     
    David C, Nov 24, 2008
    #3
  4. David C

    Ben Amada Guest

    David C wrote:

    > 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
    --
    http://allben.net/
     
    Ben Amada, Nov 24, 2008
    #4
  5. David C

    Ben Amada Guest

    David C wrote:

    > 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
    --
    http://allben.net/
     
    Ben Amada, Nov 24, 2008
    #5
  6. David C

    Ben Amada Guest

    Ben Amada wrote:

    > 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
    http://allben.net/
     
    Ben Amada, Nov 24, 2008
    #6
  7. David C

    David C Guest

    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
    "Ben Amada" <> wrote in message
    news:...
    > David C wrote:
    >
    >> 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
    > --
    > http://allben.net/
    >
     
    David C, Nov 24, 2008
    #7
  8. David C

    Ben Amada Guest

    David C wrote:

    > 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.

    --
    Ben
    http://allben.net/
     
    Ben Amada, Nov 24, 2008
    #8
  9. David C

    bruce barker Guest

    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)


    "David C" wrote:

    > 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
    > "bruce barker" <> wrote in message
    > news:...
    > > 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)
    > >
    > >
    > > "David C" wrote:
    > >
    > >> 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
    > >>
    > >>
    > >>

    >
    >
    >
     
    bruce barker, Nov 25, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Krzeszkowski
    Replies:
    3
    Views:
    598
    Peter Blum
    Mar 3, 2005
  2. ABC
    Replies:
    2
    Views:
    451
    John Timney \( MVP \)
    Jan 14, 2006
  3. ABC
    Replies:
    2
    Views:
    427
    libero familymac news
    Jan 14, 2006
  4. bill
    Replies:
    6
    Views:
    9,140
  5. =?Utf-8?B?dGhlV2l6YXJkMQ==?=

    Cross Page Posting using public property on previous page

    =?Utf-8?B?dGhlV2l6YXJkMQ==?=, Apr 20, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    715
    Swanand Mokashi
    Apr 20, 2006
Loading...

Share This Page