RadioButton Kullanarak GridView da Satır Seçilmesi
Merhaba arkadaşlar bu makalemizde RadioButton’a tıkladığımızda GridView’daki satırı seçip, seçili satırın arka plan rengini değiştireceğiz. Bu işlemi yapabilmek için javascript kodundan yararlanacağız.
Screenshot
Şekil 1
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)
{
DataBoundList();
}
}
}
protected void DataBoundList()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\kategori.mdf;Integrated Security=True");
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from [dbo].[Table]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
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>
<script type="text/javascript">
function CheckOtherIsCheckedByGVID(spanChk) {
var IsChecked = spanChk.checked;
if (IsChecked) {
spanChk.parentElement.parentElement.style.backgroundColor = '#228b22';
spanChk.parentElement.parentElement.style.color = 'white';
}
var CurrentRdbID = spanChk.id;
var Chk = spanChk;
Parent = document.getElementById("<%=GridView1.ClientID%>");
var items = Parent.getElementsByTagName('input');
for (i = 0; i < items.length; i++) {
if (items[i].id != CurrentRdbID && items[i].type == "radio") {
if (items[i].checked) {
items[i].checked = false;
items[i].parentElement.parentElement.style.backgroundColor = 'white'
items[i].parentElement.parentElement.style.color = 'black';
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id">
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:RadioButton ID="rb1" runat="server" onclick="javascript:CheckOtherIsCheckedByGVID(this);" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id"/>
<asp:BoundField DataField ="Yazar" HeaderText="Yazar" />
<asp:BoundField DataField="Kitap" HeaderText="Kitap" />
<asp:BoundField DataField="Fiyat" HeaderText="Fiyat" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN