GridView RowCommand Olayları-1

Herkese selam. Bu makalemizde GridView RowCommand a örnek vereceğiz. Projenize GridView ve Label ekleyin. GridView nesnenizin Columns kısmına <asp:CommandField SelectText="Seç" ShowSelectButton="True" /> u ekleyin. GridView nesnesine Seç isimli sutun oluşturmuş olduk. Seç linkine basıldığında 1. sütundaki stringi Label1 e yazdırıyoruz.



Şekil 1

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 GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = GridView1.Rows[index];

//Seç tuşuna basıldığında 1. sütundaki text i

//Label1 de gösteriyoruz.

Label1.Text = row.Cells[1].Text;

}

}

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" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False"

DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" AllowPaging="True"

CellPadding="4" ForeColor="#333333" GridLines="None">

<AlternatingRowStyle BackColor="White" />

<Columns>

<asp:CommandField SelectText="Seç" ShowSelectButton="True" />

<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"

SortExpression="CustomerID" />

<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"

SortExpression="CompanyName" />

<asp:BoundField DataField="ContactName" HeaderText="ContactName"

SortExpression="ContactName" />

<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

<asp:BoundField DataField="PostalCode" HeaderText="PostalCode"

SortExpression="PostalCode" />

<asp:BoundField DataField="Country" HeaderText="Country"

SortExpression="Country" />

Columns>

<EditRowStyle BackColor="#7C6F57" />

<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

<RowStyle BackColor="#E3EAEB" />

<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

<SortedAscendingCellStyle BackColor="#F8FAFA" />

<SortedAscendingHeaderStyle BackColor="#246B61" />

<SortedDescendingCellStyle BackColor="#D4DFE1" />

<SortedDescendingHeaderStyle BackColor="#15524A" />

asp:GridView>

<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>

<br />

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [City], [PostalCode], [Country] FROM [Customers]">asp:SqlDataSource>

div>

form>

body>

html>

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek dileğiyle. Hoşçakalın. Bahadır ŞAHİN

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...