Wednesday, August 15, 2012

GridView using ArrayList

ArrayList

It is the set of Dissimilar data types.

It is under System.Collections NameSpace


Default.aspx


<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">
        </asp:GridView>
    </div>
    </form>
</body>
</html>



Default.aspx.cs


using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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

{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data();
        }
    }
    private void Data()
    {
        string[] arrlist = { "uday","raju","anvesh","kittu","sandhya" };
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        for (int i = 0; i < arrlist.Count(); i++)
        {
            dt.Rows.Add();
            dt.Rows[i]["Name"] = arrlist[i].ToString();
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}


Output

Wednesday, August 8, 2012

DataList Control

It is similar to repeater Control.

It provides editing  Facilities.

It provides adding new Records



It does not Provide Sorting  and Paging Facilities.















Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
         <asp:DataList ID="DataList1" runat="server">
        <HeaderTemplate>
        <tr>
        <th>
        EmpNo
        </th>
        <th>
        EmpName
        </th>
        <th>
        Designation
        </th>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td> <%#DataBinder.Eval(Container.DataItem,"eno")%></td>
        <td> <%#DataBinder.Eval(Container.DataItem,"ename")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"designation")%></td>
        </tr>
        </ItemTemplate>
     
        </asp:DataList>
    </table>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class RepeaterControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from employee", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }
}









Working with Repeater Control

Repeater Control

This Control is used to display the data in plain Text.

Repeater Control does not provide designing Facilities, Editing Facilities, Sorting Facilities

It is faster as compared to GridView, DataList

It is just used to display the data in plane format.














Default.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <tr>
        <th>
        EmpNo
        </th>
        <th>
        EmpName
        </th>
        <th>
        Designation
        </th>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td> <%#DataBinder.Eval(Container.DataItem,"eno")%></td>
        <td> <%#DataBinder.Eval(Container.DataItem,"ename")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"designation")%></td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
    </table>
    </div>
    </form>
</body>
</html>



Default.aspx.cs


using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class RepeaterControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from employee", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
}





Tuesday, August 7, 2012

On Row Data Bound Grid View(Insert , Modify)






Add three Pages with names AddPage.aspx, ModifyPage.aspx, and a Default.aspx.

Default.aspx

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

<head runat="server">

    <title></title>

<script type="text/javascript" language="javascript">

    var lastRowSelected;

    var originalColor;

    var EmpId = "";

    var deleno = "";

   function GridView_selectRow(row, eno, ename, eid) {

        EmpId = eid;

       if (lastRowSelected != row) {

            if (lastRowSelected != null) {

                lastRowSelected.style.backgroundColor = originalColor;

                lastRowSelected.style.color = 'Black'

                lastRowSelected.style.fontWeight = 'normal';

            }

            originalColor = row.style.backgroundColor

            row.style.backgroundColor = 'Blue'

            row.style.color = 'White'

            row.style.fontWeight = 'normal'

            lastRowSelected = row;

        }

    }

    function GridView_mouseHover(row) {

        row.style.cursor = 'hand';

    }

    function updatEmp() {

        location.replace("ModifyPage.aspx?eno=" + EmpId);

  }

  


    </script>  

</head>

<body>

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

    <div>

    <table align="center">

    <tr>

    <td align="center" colspan="2">

        <asp:GridView ID="GridView1" runat="server" 

            onrowdatabound="GridView1_RowDataBound" >

        </asp:GridView>

    </td>

    </tr>

    <tr>

    <td>

        &nbsp;</td>

    </tr>

    <tr>

    <td>

        <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" />

    </td>

    <td>

        &nbsp;</td>

    <td>

        <input id="btnModify" type="button" value="Modify" onclick="updatEmp();" />

    </td>

    </tr>

    </table>

       </div>

    </form>

</body>

</html>


Default.aspx.cs


using System.Data;

using System.Data.SqlClient;

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");

            SqlCommand cmd = new SqlCommand("select *from employee", con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            da.Fill(ds);

            GridView1.DataSource = ds;

            GridView1.DataBind();

        }

        

    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            e.Row.ID = e.Row.Cells[0].Text;

            e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[0].Text + "','" + DataBinder.Eval(e.Row.DataItem, "ename") + "','" + DataBinder.Eval(e.Row.DataItem, "eno") + "')");

            e.Row.Attributes.Add("onmouseover", "GridView_mouseHover(this)");

            //e.Row.Attributes.Add("onclick", "reloadPage(this)");

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Server.Transfer("AddPage.aspx");

    }

}

ScreenShot of Default.aspx





<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server"></asp:TextBox>
    </td>
    </tr>
        <tr>
    <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
     <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
     <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


AddPage.aspx.cs


using System.Web.UI.WebControls;

using System.Data.SqlClient;

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

{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into employee values(" + txtEno.Text + ",'" + txtEname.Text + "','" + txtDesignation.Text + "')", con);
        int i=cmd.ExecuteNonQuery();
        if(i>0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();

    }

}

ScreenShot of AddPage.aspx(Click on the Image to View





















ModifyPage.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server" ReadOnly="true"></asp:TextBox>
    </td>
    </tr>
    <tr>
       <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
           <td>
        <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click"/>
    </td>
   </tr>
    </table>
    </div>
    </form>
</body>
</html>

ModifyPage.aspx.cs

using System.Data;
using System.Data.SqlClient;

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

            int employeeno = Convert.ToInt32(Context.Request.QueryString["eno"]);
            SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
            con.Open();
            SqlCommand cmd = new SqlCommand("select *from employee where eno=" + employeeno, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtEno.Text = Convert.ToString(dr[0]);
                txtEname.Text = Convert.ToString(dr[1]);
                txtDesignation.Text = Convert.ToString(dr[2]);
            }
            con.Close();
        }
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        int employeeno = Convert.ToInt32(Context.Request.QueryString["eno"]);
         SqlCommand cmd = new SqlCommand("update employee set ename='" + txtEname.Text + "',designation='" + txtDesignation.Text + "' where eno="+employeeno, con);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();
    }
}



Friday, August 3, 2012

Grid View Insert Update using OnRowDataBound

Design the Table




Add three Pages with names AddPage.aspx, ModifyPage.aspx, and a Default.aspx.

AddPage.aspx


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

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server"></asp:TextBox>
    </td>
    </tr>
        <tr>
    <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
     <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
     <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


AddPage.aspx.cs


using System.Web.UI.WebControls;

using System.Data.SqlClient;

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

{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into employee values(" + txtEno.Text + ",'" + txtEname.Text + "','" + txtDesignation.Text + "')", con);
        int i=cmd.ExecuteNonQuery();
        if(i>0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();

    }

}

ScreenShot of AddPage.aspx(Click on the Image to View)




Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type="text/javascript" language="javascript">
   var oldgridSelectedColor;
var eno;
function setMouseOverColor(element)
{
    oldgridSelectedColor = element.style.backgroundColor;
    element.style.backgroundColor = 'yellow';
    element.style.cursor = 'hand';
    element.style.textDecoration = 'underline';
}

function setMouseOutColor(element) {
    eno = element;
    element.style.backgroundColor = oldgridSelectedColor;
    element.style.textDecoration = 'none';
}
function onRowSelect(employeeno, element) {
   element.style.backgroundColor = 'Red';
   eno = employeeno;
   window.location.href = "ModifyPage.aspx?employeeno="+eno;
}
</script>
  </head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
    <tr>
    <td align="center">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            onrowdatabound="GridView1_RowDataBound">
      <Columns>
      <asp:BoundField DataField="eno" HeaderText="Eno" />
      <asp:BoundField DataField="ename" HeaderText="Ename" />
      <asp:BoundField DataField="designation" HeaderText="Designation" />
      </Columns>
        </asp:GridView>
    </td>
    </tr>
    <tr>
    <td>
        <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" />
      </td>
    </tr>
    </table>
      </div>
    </form>
</body>
</html>


Default.aspx.cs

using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from employee", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();  
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddPage.aspx");
    }
   
   
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] =
                "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] =
                "javascript:setMouseOutColor(this);";
          e.Row.Attributes.Add("onclick", "onRowSelect('" + DataBinder.Eval(e.Row.DataItem, "eno") + "',this)");

        }
    }
}






To modify the Record click on the record in the GridView.

ModifyPage.aspx

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="Eno"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEno" runat="server" ReadOnly="true"></asp:TextBox>
    </td>
    </tr>
    <tr>
       <td>
        <asp:Label ID="Label2" runat="server" Text="Ename"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtEname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
           <td>
        <asp:Label ID="Label3" runat="server" Text="Designation"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
        <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click"/>
    </td>
     </tr>
    </table>
    </div>
    </form>
</body>
</html>


ModifyPage.aspx.cs

using System.Data;
using System.Data.SqlClient;

public partial class ModifyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int employeeno = Convert.ToInt32(Context.Request.QueryString["employeeno"]);
            SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
            con.Open();
            SqlCommand cmd = new SqlCommand("select *from employee where eno=" + employeeno, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtEno.Text = Convert.ToString(dr[0]);
                txtEname.Text = Convert.ToString(dr[1]);
                txtDesignation.Text = Convert.ToString(dr[2]);
            }
            con.Close();
        }
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        int employeeno = Convert.ToInt32(Context.Request.QueryString["employeeno"]);
        SqlCommand cmd = new SqlCommand("update employee set ename='" + txtEname.Text + "',designation='" + txtDesignation.Text + "' where eno="+employeeno, con);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Response.Redirect("Default.aspx");
        }
        con.Close();
    }
}


Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...