Translate

Tuesday, 6 January 2015

Repeater Control In Asp.net

USE [DBPABITRA]
GO

/****** Object:  Table [dbo].[tbexecutive]    Script Date: 01/06/2015 17:47:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbexecutive](
[id] [varchar](10) NULL,
[workername] [varchar](50) NULL,
[currentadd] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


-------------------------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.Configuration;
using System.Data.SqlClient;
public partial class RepeatearControl : System.Web.UI.Page
{  
    SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=mypass;database=MyDB");
    protected void Page_Load(object sender, EventArgs e)
    {

        if (con.State == ConnectionState.Closed)
        {

            con.Open();

        }

        if (Page.IsPostBack == false)
        {

            Show_Data();

        }

    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "edit")
        {

            ((Label)e.Item.FindControl("Label1")).Visible = false;

            ((Label)e.Item.FindControl("Label2")).Visible = false;

            ((TextBox)e.Item.FindControl("Textbox1")).Visible = true;

            ((TextBox)e.Item.FindControl("Textbox2")).Visible = true;

            ((LinkButton)e.Item.FindControl("LinkEdit")).Visible = false;

            ((LinkButton)e.Item.FindControl("LinkDelete")).Visible = false;

            ((LinkButton)e.Item.FindControl("LinkUpdate")).Visible = true;

            ((LinkButton)e.Item.FindControl("Linkcancel")).Visible = true;

        } if (e.CommandName == "delete")
        {

            SqlCommand cmd = new SqlCommand("delete from tbexecutive where id=@id", con);

            cmd.Parameters.AddWithValue("@id", e.CommandArgument);

            cmd.ExecuteNonQuery();

            cmd.Dispose();

            Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "");

            Show_Data();

        }

        if (e.CommandName == "update")
        {

            string str1 = ((TextBox)e.Item.FindControl("TextBox1")).Text;

            string str2 = ((TextBox)e.Item.FindControl("TextBox2")).Text;

            SqlDataAdapter adp = new SqlDataAdapter("update tbexecutive set workername=@workername, currentadd=@add where id=@id", con);

            adp.SelectCommand.Parameters.AddWithValue("@workername", str1);

            adp.SelectCommand.Parameters.AddWithValue("@add", str2);

            adp.SelectCommand.Parameters.AddWithValue("@id", e.CommandArgument);

            DataSet ds = new DataSet();

            adp.Fill(ds);

            Show_Data();

            Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "");

        } if (e.CommandName == "cancel")
        {

            ((Label)e.Item.FindControl("Label1")).Visible = true;

            ((Label)e.Item.FindControl("Label2")).Visible = true;

            ((TextBox)e.Item.FindControl("TextBox1")).Visible = false;

            ((TextBox)e.Item.FindControl("TextBox2")).Visible = false;

            ((LinkButton)e.Item.FindControl("LinkEdit")).Visible = true;

            ((LinkButton)e.Item.FindControl("LinkDelete")).Visible = true;

            ((LinkButton)e.Item.FindControl("LinkUpdate")).Visible = false;

            ((LinkButton)e.Item.FindControl("Linkcancel")).Visible = false;

        }



    }

    public void Show_Data()
    {

        SqlDataAdapter adp = new SqlDataAdapter("select * from tbexecutive ORDER BY workername ASC", con);

        DataSet ds = new DataSet();

        adp.Fill(ds);

        Repeater1.DataSource = ds;

        Repeater1.DataBind();

    }

}

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


<!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 id = "Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
   
        <asp:Repeater ID="Repeater1" runat="server"
            onitemcommand="Repeater1_ItemCommand">
            <HeaderTemplate>
           

          <table width="350"><tr
bgcolor="#FF6600"><td>Name</td><td>Address</td><td>Action</td></tr></HeaderTemplate>

           <ItemTemplate><tr><td> <asp:Label
ID="Label1" runat="server" Text='<%#Eval("workername")
%>'></asp:Label>

      <asp:TextBox ID="TextBox1" runat="server"
Text='<%#Eval("workername") %>'
Visible="false"></asp:TextBox>
        </td>
        <td>
        <asp:Label ID="Label2" runat="server" Text='<%#Eval("currentadd") %>'></asp:Label>

      <asp:TextBox ID="TextBox2" runat="server"
Text='<%#Eval("currentadd") %>'
Visible="false"></asp:TextBox>
        </td>
         <td>

      <asp:LinkButton ID="LinkEdit" runat="server"
CommandArgument='<%#Eval("id") %>'
CommandName="edit">Edit</asp:LinkButton>

          <asp:LinkButton ID="LinkDelete" runat="server"
CommandArgument='<%#Eval("id") %>'
CommandName="delete">Delete</asp:LinkButton>

          <asp:LinkButton ID="LinkUpdate" runat="server"
CommandArgument='<%#Eval("id") %>' CommandName="update"
Visible="false">Update</asp:LinkButton>

          <asp:LinkButton ID="Linkcancel" runat="server"
CommandArgument='<%#Eval("id") %>' CommandName="cancel"
Visible="false">Cancel</asp:LinkButton>
            </td>
        </tr>
           
     
       
        </ItemTemplate>
        </asp:Repeater>
   
    </div>
    </form>
</body>
</html>
---------------------------

No comments:

Post a Comment