Pages

Wednesday, 24 October 2012

Define Dependencies Between Cached Items

  • To add an item that has a dependency on a file to the cache
  1. You can add an item with a dependency to the cache by using the dependencies parameter in the Cache.Add or Cache.Insert method. The following example demonstrates using the Insert method to add an item with a dependency on an XML file to the cache:                           
    Visual basic
     
         Cache.Insert("MyData1", connectionString, new CacheDependency(Server.MapPath
("~\myConfig.xml")))
          C#
         Cache.Insert("MyData1", connectionString, new CacheDependency(Server.MapPath
("~\myConfig.xml")));
  • To add an item that has a dependency on another cache item to the cache
  1. You can also add an item that depends on another object in the cache to the cache,as shown in the following example:
       Visual Basic 
         Dim sDependencies As String(1)
         sDependencies(0) = "OriginalItem"
        Dim dependency As CacheDependency = New CacheDependency(Nothing, sDependencies)
        Cache.Insert("DependentItem", "This item depends on OriginalItem", dependency)
      C#
         string[] sDependencies = new string[1];
         sDependencies(0) = "OriginalItem";
         CacheDependency dependency = new CacheDependency(null, sDependencies);
        Cache.Insert("DependentItem", "This item depends on OriginalItem", dependency);

Saturday, 25 February 2012

Bind TreeView Control with an SqlDataSource in ASP.NET

       An exciting control included in the ASP.NET toolbox is the TreeView control.Because the TreeView can display only hierarchical data,it can be bound only to the XmlDataSource and SiteMapDataSource controls.The following code can bind a TreeView control to a SiteMapDataSource control to generate navigation for your page.
      < form id = " form1 " runat = " server " >
               <div>
                         < asp: TreeView ID="TreeView1" Runat="server"
                                    DtaSourceID="SiteMapDataSource1">
                         </asp:TreeView>
                         <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
                </div>
      </form>
                       
                                    

Thursday, 23 February 2012

Attaching SQL Server Cache Dependencies in ASP.NET

         In addition to changing settings in the OutputCache directive to activate SQL Server cache invalidation,you can also set the SQL Server cache invalidation programmatically,which can set as follows.
VB
Dim myDependency As SqlCacheDependency = New SqlCacheDependency("Nortwind","Customers")
Response.AddCacheDependency(myDependency)
Response.Cache.SetValidUtilExpires(true)
Response.Cache.SetExpires(DateTime.Now.AddMinutes(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
C#
SqlCacheDependency myDependency = new SqlCacheDependency( " Northwind " , " Customers " ) ;
Response.AddCacheDependency( myDependency ) ;
Response.Cache.SetValidUntilExpires( true ) ;
Response.Cache.SetExpires(DateTime.Now.AddNinutes(60)) ;
Response.Cache.SetCacheability(HttpCacheability.Public) ;
         You first create an instance of the SqlCacheDependency object,assigning it the value of the database and the table at the same time.The SqlCacheDependency class takes the following parameters;
                 SqlCacheDependency ( databseEntryName As String,tablename As String)
         You use this parameter construction if you working with SQL Server 7.0 or with SQL Server 2000.If you working with SQL Server 2005 use the following;
                 SqlCacheDependency(sqlCmd As System.Data.SqlClient.SqlCommand)
         After the SqlCacheDependency class is in place ,you add the dependency to the Cache object and set some of the properties of the Cache object as well.You can do this either programmatically or through the OutputCache directive.

Tuesday, 21 February 2012

Programmatically Assign Master page in ASP.NET

        To accomplish this you have to use the Page.MasterPageFile property through Page_PreInit event.The Page_PreInit event is the earliest point in which you can access the Page lifecycle.For this reason,this is where you need to assign any master page that is used by any content pages.The Page_PreInit is an important event to make this,it can set as follows;
VB
Protected Sub Page_PreInit(ByVal sender As object,ByVal e As System.EventArgs)
       Page.MasterPageFile = "~/MyMasterPage.master" ;
End Sub
C#
protected void Page_PreInit(object sender , EventArgs e)
{
       Page.MasterPageFile = "~/MyMasterPage.master" ;
}

Enabling theame in the Entire application in ASP.NET

              In addition to apply an ASP.NET theme to your ASP.NET pages using the Theme attribute within the Page directive,you can alse apply it at an application level from the Web.config file .It can set as folows;

<?xml version = "1.0" encoding = "UTF-8" ?>
<configuration>
         <system.web>
                   <pages theme = "GalexyTheme" />
         </system.web>
</configuration>
              If you specify the theme in Web.Config file,you don't need to define the theme again in the Page directive of your ASP.NET pages.This theme is applied automatically to each and every page within your application

Show Current Date and Time in ASP.NET

            Developers generally like to include some of their own custom JavaScript function in their ASP.NET pages.You have a couple of ways to do this.The first is to apply JavaScript directly to the controls on your ASP.NET pages.For example,look at a simple Label control that displays current Date and Time on Page Load.
VB 
     Protected Sub Page_Load(ByVal sender As object,ByVal e As System.EventArgs)
           Label1.Text = DateTime.Now.ToString( ) ;
     End Sub
C#
     protected void Page_Load(object sender,EventArgs e)
     {
            Label1.Text = DateTime.Now.ToString( ) ;
     }
              This little bit of code displays the current date and time on the page of the end user.

Monday, 20 February 2012

Get ID of the Dynamicaly created Button in ASP.NET

      Let the dynamically calling function be Btn_Click,then the ID of the clicked button can use as like the following;
        protected void Btn_Click(object sender,EventArgs e)
        {
                //Assign the id to the string ButtonID
               string ButtonID = ((Button)sender).ID.ToString( ) ;
               //Set ID to Label1's Text property
               Label1.Text  = ButtonID.ToString( ) ;
        }