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


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...