ASP.NET Gridview Edit Update Delete Cancel işlemleri itemtemplate ve edit itemptemplate

Web Form Tarafı

<%@ Page Title="" Language="C#" MasterPageFile="~/MainPage.master" EnableEventValidation="false" AutoEventWireup="true" CodeFile="sepet-islemleri.aspx.cs" Inherits="sepet_islemleri" %> <asp:Content ID="Content1" ContentPlaceHolderID="mainhead" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div style="background-color:white" class="container"> Siparişlerim <hr /> <asp:Label ID="lblSiparis" Font-Bold="true" runat="server" Text=""></asp:Label> <asp:GridView CssClass="marginorta" ID="siparisGridView1" Width="100%" runat="server" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" AutoGenerateColumns="False" OnRowDeleting="siparisGridView1_RowDeleting" OnRowUpdating="siparisGridView1_RowUpdating" OnRowEditing="siparisGridView1_RowEditing" OnRowCancelingEdit="siparisGridView1_RowCancelingEdit" > <Columns> <asp:TemplateField HeaderText="RESİM" SortExpression="resimyolu"> <ItemTemplate> <asp:Image Width="50" Height="60" ID="Image1" ImageUrl='<%# Bind("resimyolu") %>' runat="server" /> </ItemTemplate> <HeaderStyle CssClass="tacenter" /> </asp:TemplateField> <asp:TemplateField HeaderText="KOD" SortExpression="KOD"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("KOD") %>'></asp:Label> </ItemTemplate> <HeaderStyle CssClass="tacenter" /> </asp:TemplateField> <asp:TemplateField HeaderText="AÇIKLAMA" SortExpression="AÇIKLAMA"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("AÇIKLAMA") %>'></asp:Label> </ItemTemplate> <HeaderStyle CssClass="tacenter" /> </asp:TemplateField> <asp:BoundField DataField="KDV_DAHİL_FİYAT" HeaderText="KDV DAHİL FİYAT" ReadOnly="True" SortExpression="KDV_DAHİL_FİYAT" > <HeaderStyle CssClass="tacenter" /> </asp:BoundField> <asp:TemplateField HeaderText="MİKTAR"> <ItemTemplate> <asp:TextBox ID="TextBox1" CssClass="tacenter form-control" Text='<%# Eval("sepetdurumu") %>' Width="60" runat="server"></asp:TextBox> </ItemTemplate> <EditItemTemplate> <asp:TextBox CssClass=" tacenter" Text='<%# Eval("sepetdurumu") %>' Width="60" runat="server"></asp:TextBox> </EditItemTemplate> <HeaderStyle CssClass="tacenter" /> </asp:TemplateField> <asp:BoundField DataField="TOPLAM1" HeaderText="TOPLAM" ReadOnly="True" SortExpression="TOPLAM1" > <HeaderStyle CssClass="tacenter" /> </asp:BoundField> <asp:TemplateField> <ItemTemplate> <%-- <asp:LinkButton ID="editLbutton" runat="server" CommandName="Edit" ><img src="images/edit.png" /></asp:LinkButton>--%> <asp:LinkButton ID="deleteLbutton" runat="server" CommandName="Delete" ><img src="images/cart.png" /></asp:LinkButton> <asp:LinkButton ID="updateLbutton" runat="server" CommandName="Update" ><img src="images/saveupdate.png" /></asp:LinkButton> </ItemTemplate> <%-- <EditItemTemplate> <asp:LinkButton ID="canceLbutton" runat="server" CommandName="Cancel" ><img src="images/cancell.png" /></asp:LinkButton> </EditItemTemplate>--%> <HeaderStyle CssClass="tacenter" /> <ItemStyle CssClass="tacenter" /> </asp:TemplateField> </Columns> <FooterStyle BackColor="White" ForeColor="#333333" /> <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="White" ForeColor="#333333" /> <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F7F7F7" /> <SortedAscendingHeaderStyle BackColor="#487575" /> <SortedDescendingCellStyle BackColor="#E5E5E5" /> <SortedDescendingHeaderStyle BackColor="#275353" /> </asp:GridView> <div style="padding-right:15px;text-align:right" class="row top10"> <asp:Label ID="lblgenelToplam" Font-Bold="true" Font-Size="18" runat="server" Text=""></asp:Label> <p><asp:Label ID="lblnot" Font-Bold="true" ForeColor="Red" runat="server" Text=""></asp:Label></p> <hr /> <asp:Button ID="Button1" CssClass="btn btn-success right" runat="server" Text="Siparişi Onayla" OnClick="Button1_Click" /> <hr /> </div> </div> <script type="text/javascript"> function siparisonay() { swal({ title: "Başarılı.!", text: "Siparişiniz alınmıştır. Lütfen Onaylanmasını Bekleyiniz.!", icon: "success", button: "Kapat!", }); } </script> </asp:Content>

Codebehind C#

using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class sepet_islemleri : System.Web.UI.Page { string cs1 = ConfigurationManager.ConnectionStrings["musteriCS"].ConnectionString; SqlConnection conn; DataTable dt; SqlDataAdapter adp; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session["madi"] == null) { Response.Redirect("anasayfa.aspx"); } else { lblnot.Text = "Not: Ürün Miktarını Değiştirmek için Düzenleme Butonuna Basınız güncelleme işleminden Sonra Kaydediniz.."; bindGrid(); fiyatGuncelle(); } } } public void bindGrid() { conn = new SqlConnection(cs1); dt = new DataTable(); conn.Open(); adp = new SqlDataAdapter("select *from WEB_SEPET where TCNO_VERGİNO = '" + Session["tcno_vergino"].ToString() + "'",conn); adp.Fill(dt); conn.Close(); if (dt.Rows.Count > 0) { siparisGridView1.DataSource = dt; siparisGridView1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { string cs1 = ConfigurationManager.ConnectionStrings["musteriCS"].ConnectionString; SqlConnection baglanti = new SqlConnection(cs1); SqlCommand sorgu = new SqlCommand("SELECT Max(id) FROM siparis", baglanti); baglanti.Open(); int gelensatirid = (int)sorgu.ExecuteScalar(); baglanti.Close(); gelensatirid++; string sipariskodu = "CAG123" + gelensatirid; lblSiparis.Text = "Sipariş Kodunuz: " + sipariskodu; int satirsayisi = siparisGridView1.Rows.Count; for (int i = 0; i < satirsayisi; i++) { SqlConnection baglanti1 = new SqlConnection(cs1); SqlCommand cmd = new SqlCommand("select *from WEB_SEPET where TCNO_VERGİNO='" + Session["tcno_vergino"].ToString() + "'", baglanti1); baglanti1.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { string kod = dr["KOD"].ToString(); string urunadi = dr["AÇIKLAMA"].ToString(); string miktar = dr["sepetdurumu"].ToString(); string fiyat = dr["KDV_DAHİL_FİYAT"].ToString(); string toplam = dr["TOPLAM1"].ToString(); SqlConnection baglantiekle = new SqlConnection(cs1); SqlCommand eklecmd = new SqlCommand("insert into siparis (siparis_no,tcno_vergino,kod_ref,urunadi,miktar,fiyat,toplam,durum) values('" + sipariskodu + "','" + Session["tcno_vergino"].ToString() + "','" + kod + "','" + urunadi + "','" + miktar + "','" + fiyat + "','" + toplam + "',0)", baglantiekle); baglantiekle.Open(); eklecmd.ExecuteNonQuery(); baglantiekle.Close(); } dr.Close(); baglanti1.Close(); } siparisGridView1.Visible = false; lblgenelToplam.Visible = false; lblnot.Visible=false; Button1.Visible = false; ScriptManager.RegisterStartupScript(this, GetType(), "Popup", "siparisonay();", true); sepetSil(); } protected void siparisGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { Label lb = siparisGridView1.Rows[e.RowIndex].FindControl("Label1") as Label; string cs1 = ConfigurationManager.ConnectionStrings["musteriCS"].ConnectionString; SqlConnection sil = new SqlConnection(cs1); SqlCommand silcmd = new SqlCommand("Delete from sepet where kod_ref ='" + lb.Text.ToString() + "' and tcno_vergino='" + Session["tcno_vergino"].ToString() + "'", sil); sil.Open(); silcmd.ExecuteNonQuery(); sil.Close(); bindGrid(); fiyatGuncelle(); } protected void siparisGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { Label lb = siparisGridView1.Rows[e.RowIndex].FindControl("Label1") as Label; TextBox txt = siparisGridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox; conn = new SqlConnection(cs1); conn.Open(); SqlCommand cmd = new SqlCommand("update sepet set sepetdurumu='" + txt.Text.ToString() + "' where kod_ref='" + lb.Text.ToString() + "'", conn); cmd.ExecuteNonQuery(); conn.Close(); siparisGridView1.EditIndex = -1; bindGrid(); fiyatGuncelle(); } protected void siparisGridView1_RowEditing(object sender, GridViewEditEventArgs e) { siparisGridView1.EditIndex = e.NewEditIndex; bindGrid(); } protected void siparisGridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { siparisGridView1.EditIndex = -1; bindGrid(); } public void fiyatGuncelle() { float toplam = 0; int satirsayisi; satirsayisi = siparisGridView1.Rows.Count; for (int i = 0; i < satirsayisi; i++) { float fiyat = float.Parse(siparisGridView1.Rows[i].Cells[5].Text.ToString()); toplam += fiyat; } lblgenelToplam.Text = "KDV DAHİL TOPLAM FİYAT : " + toplam.ToString(); } public void sepetSil() { string cs1 = ConfigurationManager.ConnectionStrings["musteriCS"].ConnectionString; SqlConnection sil = new SqlConnection(cs1); SqlCommand silcmd = new SqlCommand("Delete from sepet where tcno_vergino='" + Session["tcno_vergino"].ToString() + "'", sil); sil.Open(); silcmd.ExecuteNonQuery(); sil.Close(); bindGrid(); } }

Hiç yorum yok:

Yorum Gönder