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();
    }
}



No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

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