Tuesday, March 1, 2011

Is it possible to write the contents of b.aspx on a.aspx?

Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx is content. I want to display the contents of b.aspx inside a <div> on a.aspx. I know with PHP you can do it like so:

//a.php
<html>
   <head>
      <title>test</title>
   </head>
   <body>
      <?PHP
         include "b.php";
      ?>
   </body>
</html>

//b.php
<?PHP
   echo "Content String";
?>

//result
<html>
   <head>
      <title>test</title>
   </head>
   <body>
      Content String
   </body>
</html>

Thanks!

From stackoverflow
  • create a B.ascx that does everything you need, and then both B.aspx and A.aspx can include that control.

    Anders : That does not quite answer my question, is it possible to do the PHP example in ASP?
  • Probably Server.Execute will help.

    //a.aspx
    <html>
       <head>
          <title>test</title>
       </head>
       <body>
          <% Server.Execute("b.aspx"); %>
       </body>
    </html>
    
    //b.aspx
       Content String
    
    //result
    <html>
       <head>
          <title>test</title>
       </head>
       <body>
          Content String
       </body>
    </html>
    

    By the way, I do not recommend this approach. It's just to show it can be done. Master pages and user controls are normally the way to go.

    Anders : perfect, thank you mehrdad
    olle : Anders - Just out of curiosity whats the usecase that has you looking for this nonstandard way?
    Anders : I have a hosting service that has both PHP and ASP.NET 3.5 available simultaneously. I was curious to see if I could include a PHP file in an ASP.NET page and have it output the PHP file's contents.
    Mehrdad Afshari : Anders, this only works if b.aspx is an ASP.NET document. However, if you run PHP with Phalanger it would work, which is probably not the case. Server.Execute just works with ASP.NET stuff.
  • It sounds like MasterPages will accomplish this for you. Is this not an option for you?

  • you can go old-skool and use an IFRAME

    alternatively, could use a WebRequest in a.aspx.cs to open b.aspx, store the results in a string, and return that string inside a div on a.aspx

  • This scenario is handled by masterpages and or composing the page out of (user)controls in ASP.NET. As described at for instance here.

    Anders : thank you for your input, granted that you did not answer my question. the information was useful nonetheless
    Lance McNearney : This should be the real answer to the question. The Server.Execute() answer may work but you should really do it the correct way with a user control or master page so you don't run into bigger problems in the future.
    Anders : This is merely for an experiment with my new hosting service, as it has both PHP and ASP.NET running simultaneously. I wanted to see if I could include a PHP file on the ASP page and have it output its contents.

0 comments:

Post a Comment