Framework / Web Forms / No Code Behind

In This Topic
    No Code Behind
    In This Topic

    Although it is much easier to write and maintain code with the traditional code behind strategy there are cases when you have to use no code behind. Following are some cases where the no code behind model can come handy:

     

    • Porting legacy ASP applications.
    • Directly streaming images to the browser.
    • Pages that contain multiple charts that have to be handled by one instance of the chart

     

    This topic will describe the basic steps you need to follow when creating no code behind pages using Nevron Chart for .NET.

    1. After you create you ASP .NET application and assign the necessary references, as described in the Integrating In Web Forms topic of this book, add a new html page to the project and change the extension to aspx. This is crucial because it informs Internet Information Services (IIS) that the page contains code. Initially the page contains the following HTML:
      HTML
      Copy Code
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
      <HTML>
          <HEAD>
              <META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
              <TITLE>
              </TITLE>
          </HEAD>
          <BODY>
          </BODY>
      </HTML>
      
    2. Insert the following ASPX code at the beginning of the page:

      For a charting component:
      HTML
      Copy Code
      <%@ Import namespace ="System.Drawing"%>
      <%@ Import namespace ="Nevron"%>
      <%@ Import namespace ="Nevron.GraphicsCore"%>
      <%@ Import namespace ="Nevron.Chart"%>
      <%@ Import namespace ="Nevron.Chart.WebForm"%>
      <%@ Register TagPrefix="ncwc" Namespace="Nevron.Chart.WebForm" Assembly="Nevron.Chart.WebForm" %>
      <%@ Control Language="c#" %>
      
      For a diagramming component:
      HTML
      Copy Code
      <%@ Import namespace ="System.Drawing"%>
      <%@ Import namespace ="Nevron"%>
      <%@ Import namespace ="Nevron.GraphicsCore"%>
      <%@ Import namespace ="Nevron.Diagram"%>
      <%@ Import namespace ="Nevron.Diagram.WebForm"%>
      <%@ Register TagPrefix="ncwc" Namespace="Nevron.Diagram.WebForm" Assembly="Nevron.Diagram.WebForm" %>
      <%@ Control Language="c#" %>
      
    3. Insert the following code inside the HTML body tag:
      HTML
      Copy Code
      <TABLE id="Table2" style="Z-INDEX: 102; LEFT: 10px; POSITION: absolute; TOP: 10px" cellSpacing="1" cellPadding="1" border="0">
      <TR>
      <TD vAlign="top" align="left" Width="420" Height="320">
      <%
      
          //add component specific initialization code here
      
          //manually create a HtmlTextWriter object which will render the created component in the
      
          //context of ASP.NET
          HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output);
      
          //use the RenderControl method of either the charting or diagramming component
      
          //assume that the have created a Chart control
          chartControl.RenderControl(writer);
      
      %>
      </TD>
      </TR>
      </TABLE>
      

      That's it - now you have an aspx page using no code behind that displays either diagram or chart. Note that you have to match the size of the control passed through the Width and Height properties with the width of the table data. You also have to manually create an HtmlTextWriter object in order to pass it to the RenderControl method of the component.

      The no code behind technique is also very useful when you have to directly stream an image to the browser. In this case however the page must not contain any other HTML content other that the code creating and rendering the chart, because the control changes the MIME type to an image MIME type (image/png, image/bmp etc.). Any additional content will confuse the client browser and it will not be able to read the image.

    See Also