Saturday, 25 February 2012

Restrict Checkboxlist To Single Selection


How we can restrict checkboxlist to single selection
(Or)
How we can do checkboxlist behaves like a radiobuttonlist

Javascript Code:

<script type = "text/javascript">

    function MutExChkList(chk)
    {
        var chkList = chk.parentNode.parentNode.parentNode;
        var chks = chkList.getElementsByTagName("input");
        for(var i=0;i<chks.length;i++)
        {
            if(chks[i] != chk && chk.checked)
            {
                chks[i].checked=false;
            }
        }
    }

</script>

Design View of the CheckBoxList:

                <asp:CheckBoxList ID="cblSelect" runat="server" RepeatLayout="Flow" >
                <asp:ListItem Text="Yes" Value="Y" onclick="MutExChkList(this);" ></asp:ListItem>
                <asp:ListItem Text="No" Value="N" onclick="MutExChkList(this);"></asp:ListItem>
                </asp:CheckBoxList>

All the best,
Prakash

Monday, 20 February 2012

AJAX Introduction & How To Download AJAX Toolkit



Hi guys,



Please click the below link and download the file.  In that, you will get notes on AJAX introduction with explanation and examples and also how to download AJAX Toolkit and the procedure on how to add into our visual studio .Net.


AJAX Introduction and How to Download AJAX Toolkit


Regards,
Prakash

How To Install IIS on Windows 7 or Vista


If you are a developer using ASP.NET, one of the first things you’ll want to install on Windows 7 or Vista is IIS (internet information server). Keep in mind that your version of Windows may not come with IIS. I’m using Windows 7 Ultimate edition.
First, go to Control Panel, and then click on Programs. You’ll see a link for “Turn Windows features on or off”


If you expand the Internet Information Services tree node, you can see that there are a lot of options beneath it. You will probably want to explore these options, because even if you click on IIS, some of the necessary options for doing development aren’t checked.
Once you’ve gone ahead and checked the items you want, and clicked OK, you’ll see this dialog for a while….
Now when you navigate in your browser to localhost, you’ll see the new default page… slick!

How To Install IIS in Windows XP




To install IIS 5.1 in Windows XP Pro follow these steps:
  1. On the Start menu, click Control Panel.
  2. Double-click Add or Remove Programs.
  3. Click Add/Remove Windows Components.
  4. Click Internet Information Services (IIS) and then click Details.
  5. In Internet Information Services (IIS), select the check boxes for SMTP Service and World Wide Web Service, and then click OK.
  6. In Windows Component selection, ensure that the Internet Information Services (IIS) check box is selected, and then click OK to install the components that you selected.


Friday, 17 February 2012

ASP.Net Themes and Skins

ASP.NET Themes and Skins


ASP.NET Themes and ASP.NET skins are another method of adding style and consistency to ASP.NET pages without managing each page and control separately. Themes and skins are an alternative to using Master Pages.

ASP.NET themes are a unified look which can be applied to every page in a web site. ASP.NET skins are also like a theme, but it can be applied to Controls like Buttons Controls, DropDownList Controls. The following ASP.NET tutorial helps you learn the basics on ASP.NET Themes and ASP.NET Skins with a sample Theme you can build on.

Creating an ASP.NET Theme

Themes and skins are good for adding style and consistency to ASP.NET pages without managing each page and control separately. But in order to use ASP.NET Themes and ASP.NET skins, you should first create and ASP.NET Theme.  ASP.NET by default searches for available themes in a folder called App_Themes. The following steps show you how to create this folder and start a new theme, and design skins for two ASP.NET Controls.

1.    Right click the Project name in the Solution Explorer and select Add ASP.NET Folder - -> Theme from the Context Menu.
 
2.    Rename Theme1 to “myTheme”.
 
3.    Right Click the “myTheme” folder and add a Skin File named customSkin.skin. The customSkin.skin file will open with the default skin template.

4.    At the bottom of the customSkin.skin File, after the “--%>” markup, enter the following skin description code.

<asp:DropDownList runat="server" CssClass="myCustomStyle" >
</asp:DropDownList>
<asp:Button runat="server" BorderStyle="Dotted" CssClass="myCustomStyle" />

5.    Right Click on the “myTheme” folder and Add a new Style Sheet named “myCustom.css” to the Project.
6.    Add the following Style Class into the “myCustom.css”.

myCustomStyle
{
background-color:#ccffcc;
font-style: italic;
font-family: Arial, Tahoma, 'Trebuchet MS';
font-size: xx-large;
}

Now you have successfully created a skin for an ASP.NET DropDownList Control and a Button Control. The content of the “customSkin.skin” declares that all controls of these types should use the “CssClass” called “myCustom.css”. The “myCustomStyle” class is in the “myCustom.css”.

The Button control has been programmed to use a dotted border style in the “customSkin.skin”. This property isn’t part of the style sheet and therefore applies only to buttons and not to the DropDownList control.

Applying Theme to a Web Site

The best and the fastest way to tell every page and every control in a web site to use a given theme is to code it in the “web.config” file. The following steps show you how to make a theme available site wide.

1.    Open the “web.config” file from the Solution Explorer.
Note:  If you do not have a “web.config” file, run the application for the first time, and it will ask whether to create one or not.

2.    Go to the element starting with “<pages” within the “<system.web>” section.
Note:  If the “web.config” file does not have “<pages” just type in this tag, and Visual Studio will suggest and help you auto fill it.

3.    Add the following code snippet after the “<pages” for the “theme” attribute.

<pages theme="myTheme">
</pages>

At runtime, every page in the web site now knows to use the “myTheme” ASP.NET theme. This ASP.NET theme makes the DropDownList and the Button Controls within the pages to use the “myTheme” ASP.NET theme styles.

You also can use the “styleSheetTheme” attribute instead of the theme attribute. The “styleSheetTheme” attribute allows local setting to override the global theme.

<pages styleSheetTheme="myTheme">
</pages>

Applying Theme to a Page

You also have the capability of setting a theme for an individual page alone by configuring its “theme” or “styleSheetTheme” settings. This allows different themes on different pages. The following steps show you how to apply a theme to an individual page.

1.    Remove any themes assigned in the “web.config” file. (Check Assigning a theme to the whole Web Site). This is because if you leave the theme declared in the “web.config” file, it effects for whole site, thus it overrides any theme applied to an individual page locally.
2.    Go to the Design View of the ASP.NET page.
3.    Select the page by clicking on the blank area and go to the Properties Window of the Document Object (You can also press F4).
4.    Set the “StyleSheetTheme” property to the name of you preferred theme (“myTheme”).

5.    Drag and drop a DropDownList and a Button Control on the page and you will see how the IDE looks up the “myTheme” ASP.NET theme and applies the style information in the Design View.

GridView with Example

GridView:
The GridView view mode displays a list of data items by binding data fields to columns and by displaying a column header to identify the field. The default GridView style implements buttons as column headers. By using buttons for column headers, you can implement important user interaction capabilities; for example, users can click the column header to sort GridView data according to the contents of a specific column.

The following illustration shows a GridView view of ListView content.

GridView view of ListView content

Styled ListView

GridView columns are represented by GridViewColumn objects, which can automatically size to their content. Optionally, you can explicitly set a GridViewColumn to a specific width. You can resize columns by dragging the gripper between column headers. You can also dynamically add, remove, replace, and reorder columns because this functionality is built into GridView. However, GridView cannot directly update the data that it displays.

The below code deals with inserting a data to a database and displaying the items to the grid aas well and this code mainly focuses on how to export the items on the grid to excel.

Here we deal with keywords like htmltextwriter and string writer, the string writer is mainly used for implementing htmltextwriter and the htmltextwriter is used for producing indented outputs and conversion purposes as well and the code is as follows

NOTE: set EnableEventValidation ="fase" at the top of your page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public partial class Default6 : System.Web.UI.Page
{
    string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connection);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Country values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        BindGrid();
    }
    private void BindGrid()
    {
        SqlConnection con = new SqlConnection(connection);
        con.Open();
        da = new SqlDataAdapter("Select * from Country", con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
   
    private void ExportGridView()
    {
        string attachment = "attachment; filename=Country.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

    }
   

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GridView1.Columns[2].Visible = false;
        GridView1.Columns[3].Visible = false;
        ExportGridView();
    }
    private void clear()
    {
        TextBox1.Text = string.Empty;
        TextBox2.Text = string.Empty;
        btnSave.Enabled = true;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        this.clear();
    }
}

ASP.Net Caching

ASP.Net Caching

Introduction

The majority [if not all] of the pages in a dynamic website are dynamic.  That is, pages that are created on user request.  As we all know, dynamic web pages help to provide dynamic content, customized for the user requesting the page [e.g.: the user's home page].  Dynamic pages also help provide dynamic content fetched from a changing data store without the need for the administrator to change the page content every time something changes in the data store [e.g.: Listing of books in a publisher's website].  The disadvantage is the overhead in creating the pages for every user request.

To overcome this, some websites have page creation engines which create all pages in one go and save them as HTML pages which are then served to the users.  But this will only help in scenarios where the page content is the same for all requests [user-independent].  The listing of books is the same irrespective of the user requesting the page.  Even if there is provision for listing books category wise by providing different category ID values through the querystring, the page output for a particular category of books is the same for all users.

ASP.NET provides support for "caching" which will help us solve this problem to a great extend.  It can cache [store in memory] the output generated by a page and will serve this cached content for future requests.  And this is useful only in the second scenario described earlier, where the page content is the same for all requests [user-independent].  The caching feature is customizable in various ways and we will see how we can do that as we go through this article.

Caching a page

In order to cache a page's output, we need to specify an @OutputCache directive at the top of the page.  The syntax is as shown below:

<%@ OutputCache Duration=5 VaryByParam="None" %>

As you can see, there are two attributes to this directive.  They are:
·                                 Duration - The time in seconds of how long the output should be cached.  After the specified duration has elapsed, the cached output will be removed and page content generated for the next request.  That output will again be cached for 10 seconds and the process repeats. 
·                                 VaryByParam - This attribute is compulsory and specifies the querystring parameters to vary the cache. 
In the above snippet, we have specified the VaryByParam attribute as None which means the page content to be served is the same regardless of the parameters passed through the querystring.

If there are two requests to the same page with varying querystring parameters, e.g.: .../PageCachingByParam.aspx?id=12 and .../PageCachingByParam.aspx?id=15] and separate page content is generated for each of them, the directive should be:

<%@ OutputCache Duration=5 VaryByParam="id" %>

The page content for the two requests will each be cached for the time specified by the Duration attribute.
To specify multiple parameters, use semicolon to separate the parameter names.  If we specify the VaryByParam attribute as *, the cached content is varied for all parameters passed through the querystring.

Some pages generate different content for different browsers.  In such cases, there is provision to vary the cached output for different browsers.  The @OutputCache directive has to be modified to:

<%@ OutputCache Duration=5 VaryByParam="id" VaryByCustom="browser" %>

This will vary the cached output not only for the browser but also its major version.  I.e., IE5, IE 6, Netscape 4, Netscape 6 will all get different cached versions of the output.

Caching page fragments

Sometimes we might want to cache just portions of a page.  For example, we might have a header for our page which will have the same content for all users.  There might be some text/image in the header which might change everyday.  In that case, we will want to cache this header for a duration of a day.

The solution is to put the header contents into a user control and then specify that the user control content should be cached.  This technique is called fragment caching.

To specify that a user control should be cached, we use the @OutputCache directive just like we used it for the page.

<%@ OutputCache Duration=10 VaryByParam="None" %>

With the above directive, the user control content will be cached for the time specified by the Duration attribute [10 secs].  Regardless of the querystring parameters and browser type and/or version, the same cached output is served.

Data Caching

ASP.NET also supports caching of data as objects.  We can store objects in memory and use them across various pages in our application.  This feature is implemented using the Cache class.  This cache has a lifetime equivalent to that of the application.  Objects can be stored as name value pairs in the cache.  A string value can be inserted into the cache as follows:

Cache["name"]="Smitha";

The stored string value can be retrieved like this:

if (Cache["name"] != null)
    Label1.Text= Cache["name"].ToString();

To insert objects into the cache, the Add method or different versions of the Insert method of the Cache class can be used.  These methods allow us to use the more powerful features provided by the Cache class.  One of the overloads of the Insert method is used as follows:

Cache.Insert("Name", strName, 
    new CacheDependency(Server.MapPath("name.txt"), 
    DateTime.Now.AddMinutes(2), TimeSpan.Zero);

The first two parameters are the key and the object to be inserted.  The third parameter is of type CacheDependency and helps us set a dependency of this value to the file named name.txt.  So whenever this file changes, the value in the cache is removed.  We can specify null to indicate no dependency.  The fourth parameter specifies the time at which the value should be removed from cache.  The last parameter is the sliding expiration parameter which shows the time interval after which the item is to be removed from the cache after its last accessed time.

The cache automatically removes the least used items from memory, when system memory becomes low.  This process is called scavenging.  We can specify priority values for items we add to the cache so that some items are given more priority than others:

Cache.Insert("Name", strName, 
    new CacheDependency(Server.MapPath("name.txt"), 
    DateTime.Now.AddMinutes(2), TimeSpan.Zero, 
    CacheItemPriority.High, null);

The CacheItemPriority enumeration has members to set various priority values.  The CacheItemPriority.High assigns a priority level to an item so that the item is least likely to be deleted from the cache.