Monday, September 20, 2010

asp.net Substitution and Caching example::





SubstitutionExample.aspx 

<%@ Page Language="C#" %>
<!-<%@ OutputCache Duration="300" VaryByParam="none" %>-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    static string myRandomNumber(HttpContext context) {
        int myNumber = new System.Random().Next(100, 500);
        return myNumber.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net Substitution and Caching example: how to use Substitution Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1 style="color:Fuchsia">Substitution Example</h1>
        <asp:Label ID="Label1" runat="server"></asp:Label>
        Page Cached Time:
        <h2 style="color:Teal">
            <%= DateTime.Now.ToLongTimeString() %>
        </h2>
        Current Time:
        <h2 style="color:Navy">
            <%= DateTime.Now.ToLongTimeString() %>
        </h2>
        <hr />
        Substitition Control Zone [Random Number]
        <h1 style="color:Red">
            <asp:Substitution ID="Substitution1" runat="server" MethodName="myRandomNumber" />
        </h1>
    </div>
    </form>
</body>
</html>

   

AutoCompleteExtender TextBox CompletionList Width Ajax ASP.NET

In this example i am going to describe how to set Width of Completion List in Ajax AutoComplete Extender TextBox.
The default behavior of completion list takes width equal to the width of textbox. we can change this behavior by applying some CSS style to set the width we want. default width is as shown below in the Image.


As obvious from the image above , width of completion list is equals to the width of textbox, to fix this issue write the CSS script mentioned below in Head section of html source of page.




<head runat="server">
    <title>Progress Image in AutoComplete TextBox</title>
<style>
        .AutoExtender
        {
            font-family: Verdana, Helvetica, sans-serif;
            font-size: .8em;
            font-weight: normal;
            border: solid 1px #006699;
            line-height: 20px;
            padding: 10px;
            background-color: White;
            margin-left:10px;
        }
        .AutoExtenderList
        {
            border-bottom: dotted 1px #006699;
            cursor: pointer;
            color: Maroon;
        }
        .AutoExtenderHighlight
        {
            color: White;
            background-color: #006699;
            cursor: pointer;
        }
        #divwidth
        {
          width: 150px !important;  
        }
        #divwidth div
       {
        width: 150px !important;  
       }
 </style>
</head>


Now Put a div with id "divwidth" above the html source of autocomplete extender


<div ID="divwidth">div>

and add this line in autocomplete extender HTML source 

CompletionListElementID="divwidth"
The complete html source of AutoComplete Extender will look like

<asp:TextBox ID="txtAutoComplete" runat="server" Width="252px">
</asp:TextBox>   
<div ID="divwidth"></div>
<ajaxToolkit:AutoCompleteExtender runat="server" 
             ID="AutoComplete1"
             BehaviorID="autoComplete"
             TargetControlID="txtAutoComplete"
             ServicePath="AutoComplete.asmx" 
             ServiceMethod="GetCompletionList"
             MinimumPrefixLength="1" 
             CompletionInterval="10"
             EnableCaching="true"
             CompletionSetCount="12"
             CompletionListCssClass="AutoExtender"
             CompletionListItemCssClass="AutoExtenderList"
             CompletionListHighlightedItemCssClass
             ="AutoExtenderHighlight"
             CompletionListElementID="divwidth">
<ajaxToolkit:AutoCompleteExtender>



Friday, September 17, 2010

How to use master and content page::

MasterPage.master



<%@ Master Language="C#" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<script runat="server">


</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net master page example: how to use master and content page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1 style="color:Red">My Site</h1>
        <hr />
        <table>
            <tr valign="top">
                <td>
                    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
                    </asp:ContentPlaceHolder>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
                    </asp:ContentPlaceHolder>
                </td>
            </tr>
        </table>
        <hr />
        <div style="color:Gray">Page footer</div>
    </div>
    </form>
</body>
</html>


ContentPage.aspx



<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="" %>


<script runat="server">


</script>


<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <asp:Calendar 
        ID="Calendar1"
        runat="server"
        BackColor="Orange"
        BorderColor="DarkOrange"
        ForeColor="Snow"
        >
        <TitleStyle BackColor="DarkOrange" />
        <SelectedDayStyle BackColor="Crimson" />
    </asp:Calendar>
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <asp:Image 
        ID="Image1"
        runat="server"
        ImageUrl="~/Images/sunset.jpg"
        />
</asp:Content>



ContentPage2.aspx

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="" %>

<script runat="server">

</script>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <asp:Calendar 
        ID="Calendar1"
        runat="server"
        BackColor="DodgerBlue"
        BorderColor="DarkBlue"
        ForeColor="Snow"
        >
        <TitleStyle BackColor="DarkBlue" />
        <SelectedDayStyle BackColor="LightBlue" ForeColor="DarkBlue" />
    </asp:Calendar>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <asp:Image 
        ID="Image1"
        runat="server"
        ImageUrl="~/Images/sea_beach2.jpg"
        Width="50%"
        Height="50%"
        />
</asp:Content>


Sunday, September 5, 2010

MultiView example: how to use MultiView control in asp.net





MultiView is a Standard ToolBox Control. Here i present a very simple use of MultiView control. 

Simple Use 
First create a Web Form name MultiView.aspx. Then add a MultiView control. In this example I use MultiView for present 5 beautiful forest images. When someone clicks the NextImage button, then he can see the next image. I also place a label control for showing the current image number. Here is the source code of MultiView.apx page.



<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if(!Page.IsPostBack){
            MultiView1.ActiveViewIndex = 0;
           }
    }
    void NextImage(object sender, System.EventArgs e)
    {
        MultiView1.ActiveViewIndex += 1;
    }
    protected void Page_PreRender(object sender, System.EventArgs e) {
        Label1.Text = "Beautiful Forest Image: " + (MultiView1.ActiveViewIndex + 1).ToString() + " of " + MultiView1.Views.Count.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MultiView Control Simple Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Crimson"></asp:Label>
        <br /><br />
        <asp:MultiView ID="MultiView1" runat="server">
            <asp:View ID="View1" runat="server">
                <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Forest1.jpg" />
                <br />
                <asp:Button ID="Button1" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View2" runat="server">
                <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/Forest2.jpg" />
                <br />
                <asp:Button ID="Button2" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View3" runat="server">
                <asp:Image ID="Image3" runat="server" ImageUrl="~/Images/Forest3.jpg" />
                <br />
                <asp:Button ID="Button3" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View4" runat="server">
                <asp:Image ID="Image4" runat="server" ImageUrl="~/Images/Forest4.jpg" />
                <br />
                <asp:Button ID="Button4" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View5" runat="server">
                <asp:Image ID="Image5" runat="server" ImageUrl="~/Images/Forest5.jpg" />
            </asp:View>
        </asp:MultiView>
  
    </div>
    </form>
</body>
</html>
  

Wednesday, September 1, 2010

asp.net ImageMap::









ImageMap.aspx



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageMap.aspx.cs" Inherits="ImageMap" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net ImageMap: how to use</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">ImageMap</h2>
        <asp:Label 
            ID="Label1"
            runat="server"
            Font-Size="Medium"
            ForeColor="DodgerBlue"
            >
        </asp:Label>
        <br />
        <asp:ImageMap 
            ID="ImageMap1"
            runat="server"
            ImageUrl="~/Images/Doll.gif"
            HotSpotMode="PostBack"
            OnClick="ImageMap1_Click"
            >
            <asp:RectangleHotSpot
                 Top="0" 
                 Bottom="360"
                 Left="0" 
                 Right="180" 
                 AlternateText="Green Doll" 
                 PostBackValue="Green Doll"
                 />
            <asp:RectangleHotSpot 
                Top="0"
                Bottom="360"
                Left="181"
                Right="360"
                AlternateText="Pink Doll"
                PostBackValue="Pink Doll"
                />
        </asp:ImageMap>
    </div>
    </form>
</body>
</html>
   
ImageMap.aspx.cs



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ImageMap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Label1.Text = "Click any doll!";
ImageMap1.BorderWidth = 3;
ImageMap1.BorderStyle = BorderStyle.Double;
ImageMap1.BorderColor = System.Drawing.Color.Crimson;
}
}

protected void ImageMap1_Click(object sender, ImageMapEventArgs e)
{
Label1.Text = "You ckecked: " + e.PostBackValue;
}
}

Using System.Xml, System.Xml.Xsl, System.Xml.XPath Namespace in asp.net





XSLTNamespace.aspx



<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
    string xmlFilePath = Request.PhysicalApplicationPath + @"App_Data\ITBookList.xml";
    string xsltFilePath = Request.PhysicalApplicationPath + @"App_Data\ITBookList.xslt";
    XPathDocument xPathDoc = new XPathDocument(xmlFilePath);
    XslCompiledTransform transform = new XslCompiledTransform();
    transform.Load(xsltFilePath);
    transform.Transform(xPathDoc, null, Response.Output);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Using System.Xml, System.Xml.Xsl, System.Xml.XPath Namespace in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>
   
ITBookList.xslt



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT document for display book list</title>
      </head>
      <body>
        <h2 style="color:DeepPink; font-style:italic;">How to use System.Xml, System.Xml.Xsl, System.Xml.XPath Namespace in asp.net</h2>
        <hr width="775" align="left" color="Pink" />
        <br />
        <table border="1" cellpadding="5" cellspacing="0" bordercolor="Orange">
          <tr bgcolor="OrangeRed" style="color:White; font-weight:bold">
            <td>Book ID</td>
            <td align="center">Name of Book</td>
            <td>Name of Author</td>
            <td>Book Price</td>
          </tr>
          <xsl:for-each select="books/book">
            <tr bgcolor="Snow" style="color:Crimson; font-weight:normal; font-style:italic;">
              <td height="10" style="font-weight:bold;" align="center">
                <xsl:value-of select="id"/>
              </td>
              <td height="10">
                <xsl:value-of select="name"/>
              </td>
              <td height="10">
                <xsl:value-of select="author"/>
              </td>
              <td height="10" style="font-weight:bold;" align="right">
                <xsl:value-of select="price"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
   
ITBookList.xml



<?xml version="1.0" ?>
<!-- Sample XML file for various XML, XSLT, XPath example-->
<books>
  <book Category="XML and XSLT">
    <id>1</id>
    <name>Beginning XML Databases</name>
    <author>Gavin Powell</author>
    <price>39.99</price>
  </book>
  <book Category="asp.net">
    <id>2</id>
    <name>ASP.NET 3.5 Website Programming: Problem - Design - Solution</name>
    <author>Chris Love</author>
    <price>44.99</price>
  </book>
  <book Category="asp.net">
    <id>3</id>
    <name>jQuery for ASP.NET Developers</name>
    <author>Joe Brinkman</author>
    <price>6.99</price>
  </book>
  <book Category="asp.net">
    <id>4</id>
    <name>ASP.NET MVC 1.0 Test Driven Development: Problem - Design - Solution</name>
    <author>Emad Ibrahim</author>
    <price>49.99</price>
  </book>
  <book Category="XML and XSLT">
    <id>5</id>
    <name>Beginning XSLT and XPath: Transforming XML Documents and Data</name>
    <author>Ian Williams</author>
    <price>49.99</price>
  </book>
  <book Category="XML and XSLT">
    <id>6</id>
    <name>XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition</name>
    <author>Michael Kay</author>
    <price>59.99</price>
  </book>
  <book Category="Database">
    <id>7</id>
    <name>Professional Microsoft SQL Server 2008 Programming</name>
    <author>Robert Vieira </author>
    <price>49.99</price>
  </book>
</books>