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.