Herkese selam. Arkadaşlar bu makalemizde GridView da TemplateField olayına örnek vereceğiz. Sayfanıza GridView nesnesi ekleyin. GridView Tasksde Edit Columns a tıklayarak Şekil 1 deki gibi TemplateField alanını GridView nesnenize ekleyin. Sayfınıza buton ekleyin. Butonumuzu TemplateField alanında ItemTemplate kısmında tanımlıyoruz. Butonumuza gerekli olan kodlarımızı yazıyoruz. Hepsi bu kadar :)
aspx.cs
using System;
using System.Collections.Generic;
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 Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string[] CommandArgument = btn.CommandArgument.Split(',');
string CommandArgument1 = CommandArgument[0];
string CommandArgument2 = CommandArgument[1];
string CommandArgument3 = CommandArgument[2];
Label1.Text = "Posta Kodu: " + CommandArgument1.ToString() +
" " + "Şehir: " + CommandArgument2.ToString() +
" " + "Ülke: " + CommandArgument3.ToString();
}
}
aspx kodu
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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 runat="server">
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"
AllowPaging="True" >
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode"
SortExpression="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>' CommandArgument='<%#Eval("PostalCode") + "," + Eval("City") + "," + Eval("Country") %>' OnClick="Button1_Click" />
ItemTemplate>
asp:TemplateField>
Columns>
asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStrings %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [City], [PostalCode], [Country] FROM [Customers]">
asp:SqlDataSource>
div>
form>
body>
html>Ekran Çıktısı
Şekil 1
Şekil 2
0 comments:
Yorum Gönder