Sunday, 27 April 2014

How to Remove GridView Columns In ASP.NET.


Objective:-How to Remove GridView Columns In ASP.NET

STEPS :-
STEP:- 1  CREATE NEW PROJECT.
Go to File -> New -> Project -> Select asp.net web forms application -> Entry Application Name -> Click OK.
STEP:-2 Attach  "jquery-1.7.1.min" file in Scripts Folder


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

  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head runat="server">
  4.     <title>Remove Column From GridView Demo</title>
  5.     <style type="text/css">
  6.     body
  7.     {
  8.     font-family: Arial;
  9.     font-size : 10pt;
  10.     }
  11.     </style>
  12.     <script type="text/javascript" src="Script/jquery-1.7.1.min.js" ></script>
  13.     
  14.     <script type="text/javascript" language="javascript">
  15.     $(document).ready(function() {
  16.      $("#<%=gdRows.ClientID%> th").click(function() {
  17.        var iIndex = $(this).closest("th").prevAll("th").length;
  18.        $(this).parents("#<%=gdRows.ClientID%>").find("tr").each(function() {
  19.           $(this).find("td:eq(" + iIndex + ")").remove();
  20.           $(this).find("th:eq(" + iIndex + ")").remove();
  21.        });
  22.     });
  23.     
  24.     $("#<%=gdRows.ClientID%> th").mouseover(function() {
  25.         $(this).css('cursor','pointer');
  26.     });
  27. });
  28.     </script>
  29. </head>
  30. <body>
  31.     <form id="form1" runat="server">
  32.     <div>
  33.         <b>Click on the column to remove the column.</b>
  34.         <br />
  35.         <br />
  36.         <asp:GridView ID="gdRows" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Names="Arial" Font-Size="Small" GridLines="Vertical">
  37.             <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
  38.             <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
  39.             <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  40.             <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
  41.             <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
  42.             <AlternatingRowStyle BackColor="#DCDCDC" />
  43.         </asp:GridView>
  44.         <br /><br />
  45.         <asp:Button ID="btnReset" Text="Reset" runat="server" OnClick="btnReset_Click" />
  46.     </div>
  47.     </form>
  48. </body>
  49. </html>




********C# Code***************
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page 
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if(!IsPostBack)
  15.             BindGrid();
  16.     }

  17.     private void BindGrid()
  18.     {
  19.         DataTable dt = new DataTable();
  20.         DataColumn dc;
  21.         dc = new DataColumn();
  22.         dc.ColumnName = "ID";
  23.         dc.DataType = System.Type.GetType("System.Int32");
  24.         dt.Columns.Add(dc);

  25.         dc = new DataColumn();
  26.         dc.ColumnName = "ProductName";
  27.         dt.Columns.Add(dc);

  28.         dc = new DataColumn();
  29.         dc.ColumnName = "Quantity";
  30.         dc.DataType = System.Type.GetType("System.Int32");
  31.         dt.Columns.Add(dc);

  32.         dc = new DataColumn();
  33.         dc.ColumnName = "Price";
  34.         dc.DataType = System.Type.GetType("System.Int32");
  35.         dt.Columns.Add(dc);


  36.         DataRow dr;
  37.         dr = dt.NewRow();
  38.         dr["ID"] = 1;
  39.         dr["ProductName"] = "Mouse";
  40.         dr["Quantity"] = 10;
  41.         dr["Price"] = 100;
  42.         dt.Rows.Add(dr);

  43.         dr = dt.NewRow();
  44.         dr["ID"] = 2;
  45.         dr["ProductName"] = "Speaker";
  46.         dr["Quantity"] = 15;
  47.         dr["Price"] = 990;
  48.         dt.Rows.Add(dr);

  49.         dr = dt.NewRow();
  50.         dr["ID"] = 3;
  51.         dr["ProductName"] = "Hard Drive";
  52.         dr["Quantity"] = 35;
  53.         dr["Price"] = 3990;
  54.         dt.Rows.Add(dr);

  55.         dr = dt.NewRow();
  56.         dr["ID"] = 4;
  57.         dr["ProductName"] = "Ram";
  58.         dr["Quantity"] = 22;
  59.         dr["Price"] = 399;
  60.         dt.Rows.Add(dr);

  61.         dr = dt.NewRow();
  62.         dr["ID"] = 5;
  63.         dr["ProductName"] = "Keyboard";
  64.         dr["Quantity"] = 20;
  65.         dr["Price"] = 350;
  66.         dt.Rows.Add(dr);

  67.         dr = dt.NewRow();
  68.         dr["ID"] = 6;
  69.         dr["ProductName"] = "Router";
  70.         dr["Quantity"] = 1;
  71.         dr["Price"] = 3990;
  72.         dt.Rows.Add(dr);

  73.         dr = dt.NewRow();
  74.         dr["ID"] = 7;
  75.         dr["ProductName"] = "LCD";
  76.         dr["Quantity"] = 62;
  77.         dr["Price"] = 3000;
  78.         dt.Rows.Add(dr);

  79.         dr = dt.NewRow();
  80.         dr["ID"] = 8;
  81.         dr["ProductName"] = "Processor";
  82.         dr["Quantity"] = 5;
  83.         dr["Price"] = 7000;
  84.         dt.Rows.Add(dr);

  85.         gdRows.DataSource = dt;
  86.         gdRows.DataBind();
  87.         
  88.     }
  89.     protected void btnReset_Click(object sender, EventArgs e)
  90.     {
  91.         BindGrid();
  92.     }
  93. }


Saturday, 19 April 2014

How to HighLight Rows of GridView In ASP.NET.


Objective:-How to HighLight Rows of GridView In ASP.NET.

STEPS :-
STEP:- 1  CREATE NEW PROJECT.
Go to File -> New -> Project -> Select asp.net web forms application -> Entry Application Name -> Click OK.
STEP:-2 Attach  jquery-1.7.1.min file in Scripts Folder

********HTML Code***************
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head id="Head1" runat="server">
  4.     <title>Highlight GridView Rows on Checkbox Change</title>
  5.     <style type="text/css">
  6.     body
  7.     {
  8.         font-family: Arial;
  9.         font-size : 10pt;
  10.     }
  11.     .links
  12.     {
  13.         font-weight: bold;
  14.     }
  15.     </style>

  16.     <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>

  17.     <script type="text/javascript" language="javascript">
  18.     $(document).ready(function() 
  19.     {
  20.         $('#<%=gdRows.ClientID %>').find('input:checkbox[id$="chkDelete"]').click(function() {
  21.             var isChecked = $(this).prop("checked");
  22.             var $selectedRow = $(this).parent("td").parent("tr");
  23.             var selectedIndex = $selectedRow[0].rowIndex;
  24.             var sColor = '';
  25.             
  26.             if(selectedIndex%2 == 0)
  27.                 sColor = 'PaleGoldenrod';
  28.             else
  29.                 sColor = 'LightGoldenrodYellow';
  30.                 
  31.             if(isChecked)
  32.                 $selectedRow.css({
  33.                     "background-color" : "DarkSlateBlue",
  34.                     "color" : "GhostWhite"
  35.                 });
  36.             else
  37.                 $selectedRow.css({
  38.                     "background-color" : sColor,
  39.                     "color" : "black"
  40.                 });
  41.         });     
  42.     });
  43.     </script>

  44. </head>
  45. <body>
  46.     <form id="form1" runat="server">
  47.         <br />
  48.         <br />
  49.         <asp:GridView ID="gdRows" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
  50.             BorderWidth="1px" CellPadding="6" Font-Names="Arial" Font-Size="Small" GridLines="None"
  51.             AutoGenerateColumns="False" ForeColor="Black" Width="36%">
  52.             <FooterStyle BackColor="Tan" />
  53.             <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
  54.             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
  55.             <HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Left" />
  56.             <AlternatingRowStyle BackColor="PaleGoldenrod" />
  57.             <RowStyle HorizontalAlign="Left" />
  58.             <Columns>
  59.                 <asp:TemplateField HeaderText=" ">
  60.                     <ItemTemplate>
  61.                         <asp:CheckBox ID="chkDelete" runat="server" />
  62.                     </ItemTemplate>
  63.                 </asp:TemplateField>
  64.                 <asp:BoundField ShowHeader="true" DataField="ID" HeaderText="ID"></asp:BoundField>
  65.                 <asp:BoundField ShowHeader="true" DataField="ProductName" HeaderText="Product Name">
  66.                 </asp:BoundField>
  67.                 <asp:BoundField ShowHeader="true" DataField="Quantity" HeaderText="Quantity"></asp:BoundField>
  68.                 <asp:BoundField ShowHeader="true" DataField="Price" HeaderText="Price"></asp:BoundField>
  69.             </Columns>
  70.         </asp:GridView>
  71.     </form>
  72. </body>
  73. </html>

********C# Code***************
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (!IsPostBack)
  15.             BindGrid();
  16.     }

  17.     private void BindGrid()
  18.     {
  19.         DataTable dt = new DataTable();
  20.         DataColumn dc;
  21.         dc = new DataColumn();
  22.         dc.ColumnName = "ID";
  23.         dc.DataType = System.Type.GetType("System.Int32");
  24.         dt.Columns.Add(dc);

  25.         dc = new DataColumn();
  26.         dc.ColumnName = "ProductName";
  27.         dt.Columns.Add(dc);

  28.         dc = new DataColumn();
  29.         dc.ColumnName = "Quantity";
  30.         dc.DataType = System.Type.GetType("System.Int32");
  31.         dt.Columns.Add(dc);

  32.         dc = new DataColumn();
  33.         dc.ColumnName = "Price";
  34.         dc.DataType = System.Type.GetType("System.Int32");
  35.         dt.Columns.Add(dc);


  36.         DataRow dr;
  37.         dr = dt.NewRow();
  38.         dr["ID"] = 1;
  39.         dr["ProductName"] = "Mouse";
  40.         dr["Quantity"] = 10;
  41.         dr["Price"] = 100;
  42.         dt.Rows.Add(dr);

  43.         dr = dt.NewRow();
  44.         dr["ID"] = 2;
  45.         dr["ProductName"] = "Speaker";
  46.         dr["Quantity"] = 15;
  47.         dr["Price"] = 990;
  48.         dt.Rows.Add(dr);

  49.         dr = dt.NewRow();
  50.         dr["ID"] = 3;
  51.         dr["ProductName"] = "Hard Drive";
  52.         dr["Quantity"] = 35;
  53.         dr["Price"] = 3990;
  54.         dt.Rows.Add(dr);

  55.         dr = dt.NewRow();
  56.         dr["ID"] = 4;
  57.         dr["ProductName"] = "RAM";
  58.         dr["Quantity"] = 22;
  59.         dr["Price"] = 399;
  60.         dt.Rows.Add(dr);

  61.         dr = dt.NewRow();
  62.         dr["ID"] = 5;
  63.         dr["ProductName"] = "Wireless Keyboard";
  64.         dr["Quantity"] = 10;
  65.         dr["Price"] = 3500;
  66.         dt.Rows.Add(dr);

  67.         dr = dt.NewRow();
  68.         dr["ID"] = 6;
  69.         dr["ProductName"] = "Wi-Fi Router";
  70.         dr["Quantity"] = 1;
  71.         dr["Price"] = 3990;
  72.         dt.Rows.Add(dr);

  73.         dr = dt.NewRow();
  74.         dr["ID"] = 7;
  75.         dr["ProductName"] = "LCD";
  76.         dr["Quantity"] = 62;
  77.         dr["Price"] = 3000;
  78.         dt.Rows.Add(dr);

  79.         dr = dt.NewRow();
  80.         dr["ID"] = 8;
  81.         dr["ProductName"] = "Intel Processor";
  82.         dr["Quantity"] = 5;
  83.         dr["Price"] = 7000;
  84.         dt.Rows.Add(dr);

  85.         dr = dt.NewRow();
  86.         dr["ID"] = 9;
  87.         dr["ProductName"] = "Monitor";
  88.         dr["Quantity"] = 25;
  89.         dr["Price"] = 1990;
  90.         dt.Rows.Add(dr);

  91.         dr = dt.NewRow();
  92.         dr["ID"] = 10;
  93.         dr["ProductName"] = "Keyboard";
  94.         dr["Quantity"] = 20;
  95.         dr["Price"] = 350;
  96.         dt.Rows.Add(dr);

  97.         dr = dt.NewRow();
  98.         dr["ID"] = 11;
  99.         dr["ProductName"] = "headphones";
  100.         dr["Quantity"] = 12;
  101.         dr["Price"] = 500;
  102.         dt.Rows.Add(dr);

  103.         gdRows.DataSource = dt;
  104.         gdRows.DataBind();

  105.     }
  106. }

Wednesday, 16 April 2014

Show msg. after selecting color In Check Box


Write a program to display message after selecting color as per Check Box list.

********HTML Code***************
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="margin-left:30px; height: 236px; color: #008000;">
   
        <br />
        Select Your favourite Color:<br />
        <br />
        <asp:CheckBox ID="CheckBox1" runat="server" text="Blue" ForeColor="Blue"/>
        <asp:CheckBox ID="CheckBox2" runat="server" text="red" ForeColor="Red"/>
   
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Show" onclick="Button1_Click1" />
   
    &nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" ForeColor="#003366" Text="Label"></asp:Label>
   
    </div>
    </form>
</body>
</html>


********C# Code***************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class display_message_after_selecting_color_as_per_Check_Box : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
       protected void Button1_Click1(object sender, EventArgs e)
    {
        Label1.Text = "Ok! your favourite color is/are ";
        string color = "";
        if (CheckBox1.Checked)
            {
            // Response.Write("<font color='blue'>Blue</font>");
            Label1.Text += "<font color='blue'> Blue </font>";
            color += " Blue ";
            }

           if (CheckBox2.Checked)
            {
            //Response.Write("<font color='red'>Red</font>");
            Label1.Text += "<font color='red'> Red </font>";
            color += " Red ";
             }
            Response.Write("<script>alert('your favourite color is/are" + color + "')</script>");
            }
    }

Use of Post Back in ASP.NET


Create a Web Site to show the Post Back property of Web pages.

********HTML Code***************
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3.     <title></title>
  4. </head>
  5. <body>
  6.     <form id="form1" runat="server">
  7.     <div style="margin-left:20px;">
  8.     
  9.         <br />
  10.         <asp:Label ID="Label1" runat="server" Text="Click Here" ForeColor="#003300"></asp:Label>
  11.         <br />
  12.         <br />
  13.         <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
  14.     
  15.     </div>
  16.     </form>
  17. </body>
  18. </html>



********HTML Code***************
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. public partial class Post_Back : System.Web.UI.Page
  8. {
  9.     protected void Page_Load(object sender, EventArgs e)
  10.     {
  11.         if (!Page.IsPostBack)
  12.         {
  13.             Label1.Text = "Page is not in Post back";
  14.         }
  15.     }
  16.     protected void Button1_Click(object sender, EventArgs e)
  17.     {
  18.         Label1.Text = "Page is in post back";
  19.     }
  20. }

Monday, 14 April 2014

Calculate Total amount of a particular product

Write a program to Calculate Total amount of a particular product purchased by customers.

********HTML***************
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="margin-left:40px; height: 235px;">
   
        <br />
        Product : -
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>ICE-Cream</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        Price : -&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList2" runat="server">
            <asp:ListItem>20</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        Quantity : -<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Total" onclick="Button1_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
    </div>
    </form>
</body>

</html>


********C# Code***************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Product_of_Item : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Total is :-"+Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(DropDownList2.SelectedValue);
    }
}

                                      Sunday, 13 April 2014

                                      How to Display Date Time And Message in ASP.NET

                                      Create a Web Site to display Message as shown in output to Handling Events and Post backs property Of Web page also display Current date & time in a label.


                                      ******HTML Code*****************************
                                      1. <html xmlns="http://www.w3.org/1999/xhtml">
                                      2. <head runat="server">
                                      3.     <title></title>
                                      4. </head>
                                      5. <body>
                                      6.     <form id="form1" runat="server">
                                      7.     <div style="Margin-left:38px; height: 206px;">
                                      8.      
                                      9.         <br />
                                      10.         Enter Your Name :
                                      11.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                      12.         <br />
                                      13.         <br />
                                      14. &nbsp;<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                                      15.         <br />
                                      16.         <br />
                                      17. &nbsp;<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                                      18.         <br />
                                      19.         <br />
                                      20.         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show" />
                                      21.      
                                      22.     </div>
                                      23.     </form>
                                      24. </body>
                                      25. </html>

                                      ******C# Code*****************************
                                      1. using System;
                                      2. using System.Collections.Generic;
                                      3. using System.Linq;
                                      4. using System.Web;
                                      5. using System.Web.UI;
                                      6. using System.Web.UI.WebControls;

                                      7. public partial class Display_Date_Time_And_Message : System.Web.UI.Page
                                      8. {
                                      9.     protected void Page_Load(object sender, EventArgs e)
                                      10.     {

                                      11.     }
                                      12.     protected void Button1_Click(object sender, EventArgs e)
                                      13.     {
                                      14.         Label1.Text = TextBox1.Text + " " + "Welcome To Facebook";
                                      15.         Label2.Text = DateTime.Now.ToString();
                                      16.     }
                                      17. }