Advertisement
philipattisano

Repeater control with update delete

Nov 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 5.76 KB | None | 0 0
  1.        <asp:Repeater ID="cpRepeater" runat="server"
  2.             onitemcommand="cpRepeater_ItemCommand"
  3.             onitemdatabound="cpRepeater_ItemDataBound">
  4.         <HeaderTemplate>
  5.         <table width="500px" border="1px">
  6.         <tr style="background-color:#fb7700">
  7.         <td >Check</td>
  8.         <td >Member</td>
  9.         <td >Type</td>
  10.         <td >Options</td>
  11.         </tr>
  12.         </HeaderTemplate>
  13.         <ItemTemplate>
  14.         <tr style="background-color:#ffffff">
  15.         <td >
  16.          <asp:CheckBox ID="chkDelete" runat="server" />
  17.         </td>
  18.         <td >
  19.             <asp:Label ID="lblID" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
  20.             <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Member")%>'></asp:Label>
  21.             <asp:TextBox ID="txtName" BackColor="#d4d0c8" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Member")%>' Visible="false"></asp:TextBox>
  22.          </td>
  23.          <td>
  24.             <asp:DropDownList ID="ddlType" runat="server">
  25.             </asp:DropDownList>
  26.          </td>
  27.          <td ><asp:LinkButton ID="lnkEdit" runat="server" CommandName="edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Edit</asp:LinkButton>
  28.             <asp:LinkButton Visible="false" ID="lnkUpdate" runat="server" CommandName="update" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Update</asp:LinkButton>
  29.             <asp:LinkButton Visible="false" ID="lnkCancel" runat="server" CommandName="cancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Cancel</asp:LinkButton>
  30.             <asp:LinkButton ID="lnkDelete" runat="server" CommandName="delete" OnClientClick='javascript:return confirm("Are you sure you want to delete?")'
  31.             CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'>Delete</asp:LinkButton>
  32.  
  33.         </td>
  34.         </tr>
  35.         </ItemTemplate>
  36.         <FooterTemplate>
  37.         <tr style="background-color:#15880a">
  38.         <td colspan="5">
  39.             </FooterTemplate>
  40.         </asp:Repeater>
  41.         <asp:LinkButton ID="lnkDelSelected" ForeColor="White" runat="server" onclick="LinkButton1_Click" OnClientClick='javascript:return confirm("Are you sure you want to delete?")'>Delete Selected</asp:LinkButton>
  42.  
  43. // THIS is a Connection string in Web.Config
  44.  
  45. <appSettings>        
  46. <add key="ConnectionString" value="Data Source=MADHU\MUKRAM_SQL2005;Initial Catalog=EcareDiary;User ID=sa;Password=;"/>
  47.        </appSettings>
  48.  
  49. Aspx.cs looks like:
  50.  
  51. To display the records.
  52.  
  53. private void BindRepeater()
  54.     {
  55.         SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
  56.         SqlCommand SqlCmd = new SqlCommand("select * from CPMEMBERS", SqlCnn);
  57.         SqlDataAdapter SqlAd1 = new SqlDataAdapter(SqlCmd);
  58.         DataSet ds = new DataSet();
  59.         SqlAd1.Fill(ds, "CPMEMBERS");
  60.         cpRepeater.DataSource = ds;
  61.         cpRepeater.DataBind();
  62.     }
  63.  
  64. // The code in ItemDataBound event of repeater.
  65.  
  66. // Populating the dropdownlist dynamically.
  67.  
  68. protected void cpRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
  69.     {
  70.         DropDownList ddlType = (DropDownList)e.Item.FindControl("ddlType");
  71.         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  72.         {
  73.             SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
  74.             SqlCommand SqlCmd = new SqlCommand("select * from CPMEMBERS", SqlCnn);
  75.             SqlDataAdapter SqlAd1 = new SqlDataAdapter(SqlCmd);
  76.             DataSet ds = new DataSet();
  77.             DataTable dt = new DataTable();
  78.             SqlAd1.Fill(dt);
  79.             ddlType.DataTextField = "Type";
  80.             ddlType.DataSource = dt;
  81.             ddlType.DataBind();
  82.  
  83.             ddlType.SelectedValue = DataBinder.Eval(e.Item.DataItem, "UpdatedType").ToString();
  84.         }
  85.     }
  86.  
  87. // Updating and deleting of the records using Item Command Event of repeater
  88.  
  89. if (e.CommandName == "update")
  90.         {
  91.             SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
  92.             SqlCommand SqlCmd = new SqlCommand("update CPMEMBERS set Member=@Member, UpdatedType=@UpdatedType where id=@ID", SqlCnn);
  93.             SqlCmd.Parameters.Add("@Member", SqlDbType.VarChar).Value = txtName.Text;
  94.             SqlCmd.Parameters.Add("@UpdatedType", SqlDbType.VarChar).Value = ddlType.SelectedItem.Text;
  95.             SqlCmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = e.CommandArgument;
  96.             try
  97.             {
  98.                 SqlCnn.Open();
  99.                 SqlCmd.ExecuteNonQuery();
  100.  
  101.             }
  102.             catch (Exception ex)
  103.             {
  104.                 ex.Message.ToString();
  105.             }
  106.             finally
  107.             {
  108.                 if (SqlCnn.State == ConnectionState.Open)
  109.                     SqlCnn.Close();
  110.             }
  111.             BindRepeater();
  112.         }
  113.         if (e.CommandName == "delete")
  114.         {
  115.             SqlConnection SqlCnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
  116.             SqlCommand SqlCmd = new SqlCommand("delete CPMEMBERS where id=@ID", SqlCnn);
  117.             SqlCmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = e.CommandArgument;
  118.             try
  119.             {
  120.                 SqlCnn.Open();
  121.                 SqlCmd.ExecuteNonQuery();
  122.  
  123.             }
  124.             catch (Exception ex)
  125.             {
  126.                 ex.Message.ToString();
  127.             }
  128.             finally
  129.             {
  130.                 if (SqlCnn.State == ConnectionState.Open)
  131.                     SqlCnn.Close();
  132.             }
  133.             BindRepeater();
  134.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement