Search This Blog

Thursday, March 15, 2012

ADO.NET Nested GridView

ASPX Source File


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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False"
            BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
            CellSpacing="1" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Username">
                    <ItemTemplate>
                        &nbsp;<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>' Font-Bold="True"
                            Font-Italic="False" Font-Names="Lucida Handwriting" Font-Size="Large" Font-Strikeout="False"
                            ForeColor="#6600CC"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Show">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Bind("id") %>'
                            CommandName="BtnView">View</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="GridView">
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                            <AlternatingRowStyle BackColor="White" />
                            <EditRowStyle BackColor="#2461BF" />
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                            <RowStyle BackColor="#EFF3FB" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#594B9C" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#33276A" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Design :

In Aspx.cs File
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.SqlClient;

public partial class GridChildGrid : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=TOPS21;Initial Catalog=MyExample;Integrated Security=True;Pooling=False");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           Fill_Grid();
        }
    }
    public void Fill_Grid()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_user", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
      
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "BtnView")
        {
            int rowindex = Int32.Parse(e.CommandArgument.ToString());           
            SqlDataAdapter da = new SqlDataAdapter("select id,firstname,lastname,emailid,mobileno,city from tbl_user where id='"+rowindex+"'", con);
            rowindex--;
            DataSet ds = new DataSet();
            da.Fill(ds);           
            ((GridView)GridView1.Rows[rowindex].FindControl("GridView2")).DataSource = ds;
            ((GridView)GridView1.Rows[rowindex].FindControl("GridView2")).DataBind();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //int id = int.Parse(((Label)GridView1.Rows[e.Row.RowIndex].FindControl("Label2")).ToString());
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label L = (Label)e.Row.FindControl("Label2");
            int id = int.Parse(L.Text.ToString());
            SqlDataAdapter da = new SqlDataAdapter("select emailid from tbl_user where id='" + id + "'", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ((GridView)e.Row.FindControl("GridView2")).DataSource = ds;
            ((GridView)e.Row.FindControl("GridView2")).DataBind();
        }
    }
}

Output

 

On Button View of id = 2  Clicked.....

You will Get Below Snapshot as a output

 


If On Row Editing I have to Find Control Then
You Must Check The Second Condition

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //int id = int.Parse(((Label)GridView1.Rows[e.Row.RowIndex].FindControl("Label2")).ToString());
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                Label L = (Label)e.Row.FindControl("Label2");
                int id = int.Parse(L.Text.ToString());
                SqlDataAdapter da = new SqlDataAdapter("select emailid from tbl_user where id='" + id + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                ((GridView)e.Row.FindControl("GridView2")).DataSource = ds;
                ((GridView)e.Row.FindControl("GridView2")).DataBind();
            }
        }
    }

Thursday, February 2, 2012

JAVA SCRIPT VALIDATION

JAVASCRIPT.JS FILE


function dateobject() {
    var d = new Date();
    document.write("Full Date : "+d);
    document.write("<br>");
    document.write("<br>");
    document.write("Date is " + d.getDate());
    document.write("<br>");
    document.write("<br>");
    document.write("<br>");
    document.write("Day " + d.getDay());
    document.write("<br>");
    document.write("<br>");
    document.write("Current Time   (HH:MM:SS) are " + d.getHours()+" : "+d.getMinutes() +" : "+d.getSeconds() );
    document.write("<br>");   
    document.write("<br>");
    document.write("Current Time (Minutes) are " + d.getMinutes());
    document.write("<br>");
  
}

on ASPX PAGE

 <script src="datefunction.js" type="text/javascript"></script>



<input type="button" name="Button1" value="Date" onclick="dateobject()"/>



CHANGE IMAGE USING JAVASCRIPT

<h1>
                Change Image using Javascript</h1>
            <img src="images/one.jpg" id="img1" alt="This is image " onmouseover="f1()" onmouseout="f2()" height="200px"
                width="200px" />


 <script type="text/javascript">
        function f1() {
            img1.src = "images/two.jpg";

        }
        function f2() {
            img1.src = "images/three.jpg";
            popupwindow = window.open('Date1.aspx', 'Date', 'height="1024px,width="768px""');
        }
   
    </script>


USING JAVASCRIPT TEXTBOX

 <script type="text/javascript" language="javascript">
        function changecolor() {
            if (document.getElementById("txtusername").value != "") {
                document.getElementById("txtusername").style.backgroundColor = "#00CC00";
                document.getElementById('lblusername').style.color = '#00CC00';
                document.getElementById('lblusername').innerHTML = "Username is Entered";
            }//end of if

            else {
                document.getElementById("txtusername").style.backgroundColor = "#FF0000";

            }//end of else
        }//End of function
    </script>

<asp:TextBox ID="txtusername" runat="server" onmouseover="changecolor();" onmouseout="changecolor();">
                    </asp:TextBox>


VALIDATION USING JAVASCRIPT

<script type="text/javascript">
        function validate() {
          
            //if (user.value == "hitesh" && pass.value == "hitesh") {
           if (document.getElementById("txtname").value == "hitesh" && document.getElementById("txtpass").value == "hitesh") {
                alert("Congragulations !!!");
            }
            else {
                document.getElementById("Label1").innerHTML = "Enter Valid Username";
                document.getElementById("Label2").innerHTML = "Enter Valid Password";
               
                alert("Username / Password is not valid");
            }
        }

    </script>
<asp:Button ID="btnsubmit" runat="server" Font-Bold="True" ForeColor="#FF3300" Height="31px"
                        Text="Submit" Width="80px" OnClientClick="validate();" />


OPEN ANOTHER PAGE USING JAVASCRIPT

    <script language="javascript" type="text/javascript">
        function link1() {
            window.location = "http://localhost:49374/Javascript_123/login.aspx";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Go to Login Page of This website:<asp:Button ID="btnlogin" runat="server" Text="Login" OnClientClick="link1()" />

Saturday, January 28, 2012

WPF - Windows Presentation Foundation.

WPF - Windows Presentation Foundation

                 WPF is used to create windows based application in asp.net framework. We can give impressive, attractive and effective GUI to the windows based application with the help of WPF.
                 WPF is the technology which comes with .NET Framework 3.0.


VERSIONS

                 Microsoft has released five major WPF versions: 
                 WPF 3.0 (Nov 2006), 
                 WPF 3.5 (Nov 2007), 
                 WPF 3.5sp1 (Aug 2008), 
                 WPF 4 (April 2010), and 
                 WPF 4.5 (August 2012).
 The latest Version of WPF is WPF 4.5 which released on Aug,12 by Microsoft.

What WPF can do

A WPF interface can combine images, text, 2D and 3D graphics, and more.

The Ability for Developers and Designers to Work Together

Vector Graphics System for Harnessing the Power of Graphics Hardware Acceleration


Intelligent Layout makes Design Easier


Advanced Styling Capabilities for Creating Beautiful Interfaces


What is XAML???


XAML stands for eXtensible Application Markup Language—pronounced “zammel.” 

With this new markup, UIs can be defined without the need to program, very similar to creating 

an HTML web page. 

Like Windows Forms, you build WPF forms using the interactive designer to drag and drop 

items on the UI and customize in the properties box. 

But unlike Windows Forms applications where the designer generates code in C#, VB.NET to 

create controls on the form, in a WPF application, the interactive designer generates a XAML script.

When you run the program, the XAML compiler converts the XAML into instances of objects 

using the .NET Framework. 

This is unlike most other markup languages, which are typically an interpreted language without

 such a direct tie to a backing type system.

GUI PAGE HAVING XAML FILE


XAML File

<Window x:Class="gridview.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="442" Width="583" Loaded="Window_Loaded">
    <Grid Height="402" Width="558">
        <Grid.RowDefinitions>
            <RowDefinition Height="404*" />
            <RowDefinition Height="38*" />
            <RowDefinition Height="6*" />
        </Grid.RowDefinitions>
        <ListView Name="Gridview1" ItemsSource="{Binding}" Margin="12,241,0,7" Grid.RowSpan="2">
            <ListView.View>
                <GridView>

                    <!--<GridViewColumn Header="Username">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock ></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>-->
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=id}"></GridViewColumn>

                    <GridViewColumn Header="Username" DisplayMemberBinding="{Binding Path=username}"></GridViewColumn>

                    <GridViewColumn Header="Password" DisplayMemberBinding="{ Binding Path=password}"></GridViewColumn>
                    <GridViewColumn Header="Edit">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="Edit" Content="Edit" Click="Edit_Click"></Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>
        <Label Content="Username" Height="28" HorizontalAlignment="Left" Margin="52,12,0,0" Name="label1" VerticalAlignment="Top" />
        <Label Content="Password" Height="28" HorizontalAlignment="Left" Margin="52,59,0,0" Name="label2" VerticalAlignment="Top" Width="63" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="149,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="149,0,0,278" Name="textBox2" VerticalAlignment="Bottom" Width="120" />
        <Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="149,111,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Button Content="Update" Height="34" HorizontalAlignment="Left" Margin="63,166,0,0" Name="update" VerticalAlignment="Top" Width="100" Click="update_Click" />
        <Button Content="Delete" Height="34" Margin="231,166,227,0" Name="delete" VerticalAlignment="Top" Click="delete_Click" />
    </Grid>
</Window>


.CS FILE


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace gridview
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void fill_grid()
        {
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlDataAdapter sda1 = new SqlDataAdapter("select * from tbl_reg",con1);
            DataTable dt1 = new DataTable();
            sda1.Fill(dt1);
            //Gridview1.DataContext = dt1.DefaultView;
            Gridview1.DataContext = dt1.DefaultView;
                //Gridview1.DataContext = dt1.DefaultView;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            fill_grid();
        }

       
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
                SqlCommand cmd1 = new SqlCommand("insert into tbl_reg values('" + textBox1.Text + "','" + textBox2.Text + "')", con1);
                con1.Open();
                cmd1.ExecuteNonQuery();
                con1.Close();
                fill_grid();
                MessageBox.Show("Record is Successfully Inserted into the Database");
            }
            else
            {
                MessageBox.Show("Please Enter The Username and Password");
            }
        }

       

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            textBox1.Text = drv.Row[1].ToString();
            textBox2.Text = drv.Row[2].ToString();
             
        }

        private void update_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("update tbl_reg set username='" + textBox1.Text + "',password='" + textBox2.Text + "'  where id='" + id1 + "'", con1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            MessageBox.Show("Record is Successfully Updated !!!");

        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("delete from tbl_reg where id='" + id1 + "'", con1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            MessageBox.Show("Record Successfully Deleted from the Database");

        }

   
    }
}