Friday, July 11, 2008

ASP: Inserting ASP tags from code behind files

Put a PlaceHolder object in your ASPX file like this:
<asp:PlaceHolder id="ph1" runat="server"/>

Then, from your code file insert dynamically the ASP tags like this:
TextBox tb = new TextBox();
tb.Text = "...some text...";
ph1.Controls.Add(tb);


This will add the <asp:textBox> control to the ASP file.

You can also add HTML tags using the HtmlXXX classes like this:
HtmlImage img = new HtmlImage();
img.Src = "./img123.jpg";
ph1.Controls.Add(img);


or like this:
HtmlGenericControl gc = new HtmlGenericControl("a");
gc.Attributes.Add("href", "http://www.hp.com");
gc.InnerText = "Hewlet Packard";
ph1.Controls.Add(gc);


Sometimes, you will need to add a HTML tag literally. For example, you may wish to add the <br/>. However, the following code:
HtmlGenericControl gc = new HtmlGenericControl("br");
would generate:
<br> </br>

So - instead, you should use the LiteralControl class like this:
ph1.Controls.Add(new LiteralControl("<br/>"));

Wednesday, July 09, 2008

ASP: Repeater control

The Repeater control is a data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list.
Typcially, Repeater control will have the following declaration:


<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
    ...
  </HeaderTemplate>
  <ItemTemplate>
    ...
  </ItemTemplate>
  <FooterTemplate>
    ...
  </FooterTemplate>


where the items inside the HeaderTemplate and the FooterTemplate will appear once, while the items in the ItemTemplate will repeat many times.

For example:


<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
    <table border="1" width="100%">
      <tr>
        <th> Title </th>
        <th> Artist </th>
        <th> Year </th>
      </tr>
  </HeaderTemplate>
  <ItemTemplate>
      <tr>
        <td> <%# DataBinder.Eval(Container.DataItem, "title") %> </td>
        <td> <%# DataBinder.Eval(Container.DataItem, "artist")%> </td>
        <td> <%# DataBinder.Eval(Container.DataItem, "year")%> </td>
      </tr>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:Repeater>


There is also the AlternatingItemTemplate and the Separator template. Read more on the MSDN help pages

Tuesday, July 08, 2008

ASP: Inline scripting

At the top of a ASPX file, there will be a line which starts like this:
   <%@ Page Language="C#" ...
This tells you which language you can use for embedded scripting (i.e. functionality that is not in the code-behind file.
For example, the following embedded script is placed before the opening tag:
   <script runat="server">
      void change(object sender, EventArgs e)
      {
         TextBox1.Text = "Radjenovic";
      }
   </script>

The change() function can be then assigned to, e.g., the ontextchanged event handler for the TextBox1. If the AutoPostBack attribute is also set to True then the text in the TextBox1 will change to "Some other text" as soon as the textbox loses focus (i.e. the user clicks elsewhere, or presses the TAB key).

Thursday, July 03, 2008

ASP: Launch new window on button click

One way to launch a new window when a button is clicked is by associating a javascript with the onlick event. This is best done when the page is loaded:

protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:window.open ('http://www.microsoft.com/'); return false;");
}


where Button1 is an 'asp:button'.
Also, note the 'return false;' part of the script: this makes sure that the page is not posted back to itself.

Thursday, May 01, 2008

SQL 2005 on Vista: IIS required

Installation of SQL Server 2005 in Windows Vista - warning message on IIS feature requirement during System Configuration Check
During the installation of SQL Server 2005 in Windows Vista, the following warning message may appear during System Configuration Check (IIS feature requirement), indicating that IIS is not configured properly.

===== Warning message =====

Microsoft Internet Information Services (IIS) is either not installed or is disabled. IIS is required by some SQL Server features. Without IIS, some SQL Server features will not be available for installation. To install all SQL Server features, install IIS from Add or Remove Programs in Control Panel or enable the IIS service through the Control Panel if it is already installed, and then run SQL Server Setup again. For a list of features that depend on IIS, see Features Supported by Editions of SQL Server in Books Online.

===== Solution =====

1. Log-on with a user having administrative privileges, or invoke applications explicitly as the built-in Administrator using the command ‘runas /user:administrator cmd.exe’.
2. Install IIS 7 on Vista
--> Start
--> Control Panel
--> Programs
--> Programs and Features
--> Turn Windows features on or off
--> Enable

3. Enable various features of IIS
--> Web management tools
      --> IIS 6 Management Compatibility
            --> IIS 6 WMI Compatibility
            --> IIS Metabase and IIS 6 configuration compatibility
--> World Wide Web Services
      --> Application Development Features
            --> ASP.NET
            --> ISAPI Extensions
            --> ISAPI Filters
      --> Common Http Features
            --> Default Document
            --> Directory Browsing
            --> HTTP Redirection
            --> Static Content
      --> Security
            --> Windows Authentication

4. Install SQL Server 2005.

5. Be reminded to apply SQL Server 2005 service pack 2.

Tuesday, April 22, 2008

ASP.NET Web Site Compilation

The ASP.NET Compilation tool (Aspnet_compiler.exe) enables you to compile an ASP.NET Web application, either in place or for deployment to a target location such as a production server. In-place compilation helps application performance because end users do not encounter a delay on the first request to the application while the application is compiled.
Compilation for deployment can be performed in one of two ways: one that removes all source files, such as code-behind and markup files, or one that retains the markup files.

There is no aspnet_compiler in .NET 3.5 because .NET 3.5 does not include ASP.NET. The latest version of ASP.NET was released as a part of the .NET Framework 2.0; howerver, since .NET 2.0 is a subset of the .NET Framework 3.5, you will find the aspnet_compiler in the same directory (%WINDIR%\Microsoft.NET\Framework\v2.0.50727\).