Merhaba arkadaşlar bu makalemizde FormView da verileri göstereceğiz. FormView ItemUpdating e yazacağımız kodla verilerin güncellemesini yapacağız.
Screenshot
Şekil
1
Şekil 2
Default.aspx.cs
using System;
using System.Collections.Generic;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindData();
}
}
SqlConnection con;
SqlCommand cmd;
protected void bindData()
{
con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
con.Open();
cmd = new SqlCommand("select * from [dbo].[Table]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
FormView1.DataSource = ds;
FormView1.DataBind();
}
protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
{
FormView1.ChangeMode(e.NewMode);
bindData();
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
int id = Convert.ToInt32((FormView1.FindControl("txtId") as TextBox).Text);
string yazar = (FormView1.FindControl("txtAd") as TextBox).Text;
string kitap = (FormView1.FindControl("txtKitap") as TextBox).Text;
double fiyat = Convert.ToDouble((FormView1.FindControl("txtFiyat") as TextBox).Text);
string guncelle = "update [dbo].[Table] set Yazar='" + yazar + "', Kitap = '"+ kitap +"',fiyat = "+ fiyat +" where id = "+ id;
con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
cmd = new SqlCommand(guncelle, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FormView1.ChangeMode(FormViewMode.ReadOnly);
bindData();
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" >
<div>
</div>
<asp:FormView ID="FormView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both"
onitemupdating="FormView1_ItemUpdating" OnModeChanging="FormView1_ModeChanging">
<EditRowStyle BackColor="OrangeRed" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<table border="1">
<tr>
<th><b>ID:</b></th>
<td ><%# Eval("ID") %></td>
</tr>
<tr>
<td><b>Yazar:</b></td>
<td ><%# Eval("Yazar") %></td>
</tr>
<tr>
<td><b>Kitap:</b></td>
<td ><%#Eval("Kitap") %></td>
</tr>
<tr>
<td><b>Fiyat:</b></td>
<td><%# Eval("Fiyat") %></td>
</tr>
</table>
<asp:LinkButton ID="lnkDuzenle" Text="Düzenle" CommandName="Edit" runat="server" />
<asp:LinkButton ID="lnkYeni" Text="Yeni" CommandName="New" runat="server" />
<asp:LinkButton ID="lnkSil" Text="Sil" CommandName="Delete" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<table border="1">
<tr>
<th><b>ID:</b></th>
<td><asp:TextBox ID="txtId" Text='<%# Bind("ID") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Yazar:</b></td>
<td><asp:TextBox ID="txtAd" Text='<%# Bind("Yazar") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Kitap:</b></td>
<td><asp:TextBox ID="txtKitap" Text='<%# Bind("Kitap") %>' runat="Server"/> </td>
</tr>
<tr>
<td><b>Fiyat:</b></td>
<td><asp:TextBox ID="txtFiyat" Text='<%# Bind("Fiyat") %>' runat="Server" /></td>
</tr>
</table>
<asp:LinkButton ID="lnkGuncelle" Text="Güncelle" CommandName="Update" runat="server" ForeColor="#CCFF99" />
<asp:LinkButton ID="lnkIptal" Text="İptal" CommandName="Delete" runat="server" ForeColor="#CCFF99" />
</EditItemTemplate>
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
</asp:FormView>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder