Mar 3, 2011

How to Bind Data in DropDownList in Asp.net Using SqlDataSource



Dropdownlist Control:
It is enables users to select from a single-selection drop-down list. The drop-down list contain "n" number of items.


The DropDownList control also supports data binding, such as data to bind the control to a data source like object data source, xml data source and sql data source, that contains the items to display in the control. This DropDownList control can be used to add data manually or even dynamically data binding from database.


DataBind Method
It is method to bind the data source to the DropDownList control


DataTextField, DataValueField Property
To specify which field in the data source to bind to the Text and Value properties of each list item in the control.


SelectedIndex Property
The SelectedIndex property to programmatically determine the index of the item selected by the user from the DropDownList control


DropDownList Syntax





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

<!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 dropdownlist control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    <table>
    <tr>
    <td style="height: 45px"><asp:Label ID="Label1" runat="server" Style="left: -1px; position: relative; top: 0px"
            Text="Name:" Width="46px"></asp:Label></td>
     <td style="height: 45px"><asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
            DataTextField="EmpName" DataValueField="EmpName" Height="26px" Style="left: 3px;
            position: relative; top: 0px" Width="125px">
         <asp:ListItem> &lt;&lt; Select &gt;&gt;</asp:ListItem>
        </asp:DropDownList></td>
     <td style="height: 45px">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:masterConnectionString %>"
            SelectCommand="SELECT [EmpName] FROM [tbl_employee_profile]"></asp:SqlDataSource></td>
    </tr>
    </table>
        <asp:DropDownList ID="DropDownList" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Output:

Feb 15, 2011

How to Create a First Asp.net web page using Visual Studio



Open Visual Studio 2008
Goto File Menu
Select New WebSite (Shift +Alt+N)
This will be opened New Website dialog box
Enter the folder location should be created New Website
Click Ok Button
After you create a new website, you can add an asp.net page to it

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

<!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, EventArgs e)
    {
        lblCurrentDateTime.Text = DateTime.Now.ToString().Trim();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Welcome Asp.net First Page</title>    
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <center>
        <asp:Label ID="lblHeader" runat="server" Text="Welcome Asp.net First Page" Font-Bold="true" Font-Size="Medium"></asp:Label>
       </center>
        <asp:Label ID="lblCurrentDateTime" runat="server" Text="[DateTime]" ></asp:Label>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
    </div>
    </form>
</body>
</html>
Output:


Feb 3, 2011

Asp.net Application Directory Structure



In Web Application should have a good planned solution explorer directory structure you design. Asp.net introduced special directory list below.

  • BIN
  • App_Code
  • App_GlobalResoures
  • App_LocalResources
  • App_WebReferences
  • App_Data
  • App_Browsers
  • App_Themes

BIN Directory
      It is contains all the precompiled .net assemblies like DLLs that the Asp.net web application uses.These assemblies can include precompiled class as well as other assemblies referenced by the class


App_Code Directory
      It is contains source code files that are dynamically compiled for use in your application. These code files are usually separate components or a data access library


App_GlobalResources
      It is stores global resources that are accessible to every page


App_LocalResources
     It is serves the same purpose as app_globalresources, except these resources are accessible for their dedicated page only


App_WebReferences
     It is stores refereces to web services that the web application uses


App_Data
     It is reserved for data storage and also mdb files and xml files


App_Browsers
     It is contains browser definitions stored in xml files. These xml files define the capabilities of client side browsers for different rendering actions


App_Themes
     It is stores the themes use 

Asp.net Application Directory Structure
How to add application directory structure above list special directories 


      


Jan 29, 2011

How to Gridview Header Filtering in Asp.net



In Asp.net Grid view control use to header row filtering for each column to search condition based on that start with first character for each column data. In this sample grid view filter application to used Linq query concepts and also data bind from the data table objects. It is used to Linq query for filtering purpose such as select statement like operator.

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

<!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>Gridview Filter Example</title>
    <link rel="stylesheet" type="text/css" href="gridview.css" media="all" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <table style="width: 640px" border="0" cellpadding="0" cellspacing="6" class="GridviewTable">
        <tr >
            <td style="width: 120px">
                Product ID
            </td>
            <td style="width: 120px">
                Product Name
            </td>
            <td style="width: 120px">
                Product Weight
            </td>
            <td style="width: 120px">
                Product Price
            </td>
            <td style="width: 120px">
                Product Expiry Date
            </td>
         </tr>
          <tr >
                <td style="width: 120px;">
                    <asp:TextBox ID="txtProductId" runat="server" Width="75px"></asp:TextBox>
                    &nbsp;<asp:Button ID="btnProductId" runat="server" Text="Go" 
                        onclick="btnProductId_Click" />
                </td>
                <td style="width: 120px;">
                <asp:TextBox ID="txtProductName" runat="server" Width="75px"></asp:TextBox>
                   &nbsp;<asp:Button ID="btnProductName" runat="server" Text="Go" 
                        onclick="btnProductName_Click" />
                </td>
                <td style="width: 120px;">
                <asp:TextBox ID="txtProductWeight" runat="server" Width="75px"></asp:TextBox>
                    &nbsp;<asp:Button ID="btnProductWeight" runat="server" Text="Go" 
                        onclick="btnProductWeight_Click" />
                </td>
                <td style="width: 130px;">                       
                    <asp:TextBox ID="txtProductPrice" runat="server" Width="75px"></asp:TextBox>
                    &nbsp;<asp:Button ID="btnProductPrice" runat="server" Text="Go" 
                        onclick="btnProductPrice_Click" />
                </td>
                <td style="width: 130px;">                       
                    <asp:TextBox ID="txtProductExpiryDate" runat="server" Width="75px"></asp:TextBox>
                    &nbsp;<asp:Button ID="btnProductExpiryDate" runat="server" Text="Go" 
                        onclick="btnProductExpiryDate_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="5">
                    <asp:GridView ID="gvProductDtls" runat="server" AutoGenerateColumns="False"
                         Width="640px" CssClass="Gridview" ShowHeader="False" >                       
                        <Columns>
                            <asp:BoundField DataField="ProducId" ItemStyle-Width="120px" >
                            <ItemStyle Width="120px"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="ProductName" ItemStyle-Width="120px" >
                            <ItemStyle Width="120px"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="ProductWeight" ItemStyle-Width="120px" >
                            <ItemStyle Width="120px"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="ProductPrice" ItemStyle-Width="130px" >
                            <ItemStyle Width="130px"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="ProductExpiryDate" ItemStyle-Width="130px" >
                            <ItemStyle Width="130px"></ItemStyle>
                            </asp:BoundField>
                        </Columns>                       
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


GridviewFilterExample.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;
/// 
/// Gridview Filter Example 
/// 
public partial class GridviewFilterExample : System.Web.UI.Page
{
    DataTable productTable;
    /// 
    /// Page load event is call to bind data from datatable in gridview
    /// 
    /// /// protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            productTable = new DataTable();
            productTable = CreateDataTable();
            Session["ProductTable"] = productTable;

            this.gvProductDtls.DataSource = ((DataTable)Session["ProductTable"]).DefaultView;
            this.gvProductDtls.DataBind();
        }
    }

    /// 
    /// Create a datatable columns and rows 
    /// 
    /// 
    private DataTable CreateDataTable()
    {
        DataTable myDataTable = new DataTable();

        DataColumn myDataColumn;

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "ProducId";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "ProductName";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "ProductWeight";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "ProductPrice";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "ProductExpiryDate";
        myDataTable.Columns.Add(myDataColumn);

        myDataTable.Rows.Add("01", "G Powder", "500mg", "1,500.00", "12/12/2011");
        myDataTable.Rows.Add("02", "G Eye Drops", "50ml", "200.00", "02/04/2012");
        myDataTable.Rows.Add("03", "G Syrup", "250ml", "150.00", "08/10/2011");

        return myDataTable;
    }

    /// 
    /// Gridview filter based on the Product id 
    /// 
    /// /// protected void btnProductId_Click(object sender, EventArgs e)
    {
        if (txtProductId.Text.ToString().Trim() != "")
        {
            DataTable dt = (DataTable)Session["ProductTable"];
            var query = from t in dt.AsEnumerable()
                        where t.Field("ProducId").StartsWith(txtProductId.Text.ToString().Trim())
                        || t.Field("ProducId").Contains(txtProductId.Text.ToString().Trim())
                        select t;
            DataTable dtable = new DataTable();
            dtable = query.CopyToDataTable();
            this.gvProductDtls.DataSource = dtable;
            this.gvProductDtls.DataBind();
        }
        else
        {
            this.gvProductDtls.DataSource = ((DataTable)Session["ProductTable"]).DefaultView;
            this.gvProductDtls.DataBind();
        }
    }
)

Output:


Dec 5, 2010

New Features in ASP.NET 2.0 Version

ASP.NET 2.0 is not just about adding new features. It also streamlines performance and simplifies configuration with a new tool called WAT (Website Administration Tool).

ASP.NET is a programming framework built on the common language runtime that can be used on a server to build powerful web application. The first version of ASP.NET offered several important advantages over previous web development models.

ASP.NET 2.0 improves upon the foundation by adding support for several new and exciting features in the areas of development productivity, administration and management, extensibility, and performance.

How to easily build development productivity of new feature controls
  • New Server Controls
  • Data Controls
  • Navigation Controls
  • Login Controls
  • Web Part Controls
  • Master Pages
  • Themes and Skins
  • Personalization
  • Localization
How to monitor Administration and Management
  • Configuration API
  • ASP.NET MMC Admin Tool
  • Pre-compilation Tool
  • Health Monitoring and Tracing          

How to build customize control and flexible extensibility
  • Provider-driven Application Services
  • Server Control Extensibility
  • Data Source Controls
  • Compilation Build Providers
  • Expression Builders

 How to build performance and scalability
  • 64-Bit Support
  • Caching Improvements