Aug 10, 2011

How to use Send Email Concepts in Asp.net application

The MailMessage and SMTP are classes defined in the .NET Framework Class Library's System.Web.Mail namespace. It is due to a security change made to asp.net just before it shipped, you need to set SMTPMail's, SMTPServer property to localhost even though localhost is the default. In addition, you must use the IIS configuration applet to enable localhost to relay message through the local SMTP Service.


Example:
        MailMessage message = new MailMessage ();
        message.From = <from@testmail.com>;
        message.To = <to@testmail.com>;
        message.Subject = "Welcome to Asp.net Example";
        message.Body = "Free Subscribe to the Asp.net Example";
        SmtpMail.SmtpServer = "localhost";
        SmtpMail.Send (message);

Aug 7, 2011

How to use Calendar with Literal Controls in Asp.net

(i) .ASPX File Content
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>Calendar with Literal Controls Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Calendar Runat="server" ID="Cal" BorderStyle="Solid" BackColor="White" Width="162px" ForeColor="Black" 
     CellSpacing="1" Height="113px" Font-Size="9pt" Font-Names="Verdana" 
     BorderColor="Black" PrevMonthText="Prev" NextMonthText="Next" ShowGridLines="True"
     TitleFormat="Month" SelectionMode="DayWeekMonth" OnSelectionChanged="Cal_SelectionChanged" />
     <Br>
     <asp:Literal ID="Literal1" Runat="server" />
    </div>
    </form>
</body>
</html>
(ii) .ASPX.CS File Content
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{   
    
    protected void Cal_SelectionChanged(object sender, EventArgs e)
    {
        try
        {            
            foreach (DateTime date in Cal.SelectedDates)
            {
                Literal1.Text = " Selected Date is  :" + date.ToString("d");
            }
        }
        catch (Exception )
        {
            
            throw;
        }
    }
}
(iii) Output

How to Add Two numbers using TextBox Control in asp.net

(i) .ASPX File Content
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Adding Two Number using TextBox Control Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0" width="300px">
         <tr>
          <td><asp:Label ID="Label1" runat="server" Text="First Number"></asp:Label></td>
          <td><asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
          <td><asp:Label ID="Label2" runat="server" Text="Second Number"></asp:Label></td>
          <td><asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
         <td></td>
         <td><asp:Button ID="btnCalculate" runat="server" Text="Submit" OnClick="btnCalculate_Click" /></td>
         </tr>
         <tr>
          <td><asp:Label ID="Label3" runat="server" Text="Result is"></asp:Label></td>
          <td><asp:TextBox ID="txtResult" runat="server"></asp:TextBox> </td>
         </tr>
        </table>    
    </div>
    </form>
</body>
</html>
(ii) .ASPX.CS File Content
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    //Declare variables
    int num1, num2, total = 0;
    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            //Adding two numbers
            num1 = int.Parse(txtFirstNumber.Text.ToString().Trim());
            num2 = int.Parse(txtSecondNumber.Text.ToString().Trim());
            total = num1 + num2;
            txtResult.Text = total.ToString();
        }
        catch (Exception)
        {
            throw;
        }
    }
}
(iii) Output

Jul 30, 2011

Asp.net Mobile Challenges, Configuration, Languages, Network Limitations

In. .Net Mobile is an additional extension to the Asp.net and the .NET Framework.  It is a set of server side form controls to building application for wireless mobile devices. These control give output for different devices by generating WML, HTML, or compact HTML.


What is a WML?
Wireless Markup Language, based on XML, is a markup language intended for devices that implement the Wireless Application Protocol (WAP) specification, such as mobile phones. It provides navigational support, data input, hyperlinks, text and image presentation, and forms, much like HTML (HyperText Markup Language).


What is a HTML?
HTML, which stands for HyperText Markup Language, is the predominant markup language for web pages. HTML elements are the basic building-blocks of web pages.


HTML is written in the form of HTML elements consisting of tags, enclosed in angle brackets (like ), within the web page content. HTML tags normally come in pairs like the first tag in a pair is the start tag, the second tag is the end tag (they are also called opening tags and closing tags). In between these tags web designers can add text, tables, images, etc..


The purpose of a web browser is to read HTML documents and compose them into visual or audible web pages. The browser does not display the HTML tags, but uses the tags to interpret the content of the page.

What is a compact HTML?
C-HTML (short for Compact HyperText Markup Language), also called i-mode-HTML,[1] is a subset of HTML for small information devices, such as smart phones and PDAs etc. C-HTML does not support tables, image maps, multiple fonts and styling of fonts, background colors and images, frames, style sheets, and is limited to a monochromatic display

Asp.net Mobile Challenges:
Varying Configuration
Browser Languages
Wireless Network Limitations
Pagination & Session Management


Varying Configuration
It is support memory -2mb to 64mb and capabilities like sound, color displays.
It's displays area -128x160 to 320x480 pixels


Browser Languages
It is supported languages like compact HTML, HTML, WML, HDML, VoiceXML. 


Wireless Network Limitations
It is limited bandwidth is -9.6kbps to 11mbps and also high latency, intermitten connectivity

Jun 22, 2011

Introduction to .NET Mobile Application

Today, It is many tools available on the market for creating application for mobile phones and portable devices.
The Microsoft introduced mobile technology for creating asp.net mobile application. Window mobile is a compact operating system for mobile devices and including Pocket PCs, Smart phones and Portal Media Centers. We can able to develop mobile based application on the Visual Studio 2003, 2005, 2008 to integrated .NET Compact Framework. It is possible to develop impressive mobile window form based and web based application.
.Net Mobile is a set of server-side forms controls to build application for wireless mobile devices. These controls produce different output for different devices by generating WML, HTML or Compact HTML.


Mobile Web Form Controls
Adapts display based on the device capabilities
It is generates multiple markup languages
It is support common desktop controls

AdRotator, Clendar, Command, CompareValildator, DeviceSpecific, Form, Image, Label, Link, List, ObjectList, Selection List, Panel, rabgeValidator, RequiredFieldValidator, RegularExpressionValidator, StyleSheet, TextBox, TextView, Validation Summary and device specific controls like phoneCall

How to Create a RadioButton Control in Asp.Net

RadioButton is a server control use to display option similar to checkbox.


RadioButton control supports the following properties




RadioButton Example
<%@ 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 btnSubmit_Click(object sender, EventArgs e)
    {
        if (rdlYes.Checked)
            lblResult.Text = rdlYes.Text;
        if (rdlNo.Checked)
            lblResult.Text = rdlNo.Text;
        if (rdlNone.Checked)
            lblResult.Text = rdlNone.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>RadioButton Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    Did you like my website?    
    <ul>
        <li>
        <asp:RadioButton id="rdlYes" Text="Yes" GroupName="Source" Runat="server" />
        </li>
        <li>
        <asp:RadioButton id="rdlNo" Text="No" GroupName="Source" Runat="server" />
        </li>
        <li>
        <asp:RadioButton id="rdlNone" Text="None" GroupName="Source" Runat="server" />
        </li>
    </ul>    
    <asp:Button id="btnSubmit" Text="Submit" Runat="server" OnClick="btnSubmit_Click" />   
    <hr />    
    Answer: <asp:Label id="lblResult" Runat="server" />    
    </div>
    </form>
</body>
</html>
Output:

May 29, 2011

How to Create an TextboxAutoPostBack in ASP.NET

<%@ 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 txtSearch_TextChanged(object sender, EventArgs e)
    {
        lblSearchResults.Text = "Search for: " + txtSearch.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>TextBox AutoPostBack</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    <asp:Label id="lblSearch" Text="Search:" Runat="server" />
    <asp:TextBox id="txtSearch" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged" Runat="server" />
    <br />
    <hr />
    <asp:Label id="lblSearchResults" Runat="server" />    
    </div>
    </form>
</body>
</html>

Output:

How to Create an AutoComplete Textbox Control in ASP.NET

<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Show AutoComplete</title>
</head>
<body>
<form id="form1" runat="server">
<div>    
<asp:Label id="lblFirstName" Text="First Name:" AssociatedControlID="txtFirstName" Runat="server" />
<br />
<asp:TextBox id="txtFirstName" AutoCompleteType="FirstName" Runat="server" />    
<br /><br />    
<asp:Label id="lblLastname" Text="Last Name:" AssociatedControlID="txtLastName" Runat="server" />
<br />
<asp:TextBox id="txtLastName" AutoCompleteType="LastName" Runat="server" />    
<br /><br />
<asp:Button id="btnSubmit" Text="Submit" Runat="server" />    
</div>
</form>
</body>
</html>
Output:



May 22, 2011

How to Enable or Disable ViewState Property in ASP.NET

ViewState:
ViewState allows the state of objects to be stored in a hidden field on the page. ViewState is transported to the client and back to the server, and is not stored on the server or any other external source. ViewState is used to retain the state of server-side objects between post backs.


Enable ViewState Property:
It allows the page to save the users input on a form across postbacks. It saves the server-side values for a given control into ViewState, which is stored as a hidden value on the page before sending the page to the clients browser


Show ViewState:



<%@ 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 btnAdd_Click(object sender, EventArgs e)

    {

        lblCounter.Text = (Int32.Parse(lblCounter.Text) + 1).ToString();

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Show View State</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>    

    <asp:Button id="btnAdd" Text="Add" OnClick="btnAdd_Click" Runat="server" />    

    <asp:Label id="lblCounter" Text="0" Runat="server" />    

    </div>

    </form>

</body>

</html>


Output:




How to Create a First Asp.net web page using CodeBehind


ASPX Page:

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

<!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 id="Head1" runat="server">

    <title>First Page Code-Behind</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>    

    <asp:Button id="Button1" Text="Click Here" OnClick="Button1_Click" Runat="server" /> <br /><br />        

    <asp:Label id="Label1" Runat="server" />    

    </div>

    </form>

</body>

</html>
Code Behind Page:
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;


public partial class FirstPageCodeBehind : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Label1.Text = "Click the Button";

    }
  

    protected void Button1_Click(object sender, EventArgs e)

    {

        Label1.Text = "Welcome ASP.NET Example..!";

    }

}

Output:

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: