TextBox a Girilen Veriyi Sql Veritabanında Arayıp, Sonucu CheckBoxList de Listelemek
Merhaba arkadaşlar bu makalemizde textbox a aramak için girdiğimiz veriyi sql veritabanında arayıp, arama sonucunu checkBoxList listesinde gösterimini sağlayacağız.
Yukarıdaki işlemleri yapabilmemiz için jquery.min.js javascriptini kullanacağız.
İlk önce checkUser procedure ni oluşturuyoruz. CheckBoxList nesnesinde sql veritabanındaki person tablosundaki verileri gösterebilmek için, CheckBoxList de DataTextField kısmına Name sütununu, DataValueField kısmına da Id sütununu aşağıdaki gibi bağlıyoruz.
cblist.DataTextField = "Name";
cblist.DataValueField = "Id";
Şekil 1
CREATE PROCEDURE checkUser
(
@UserName VARCHAR(50)
)
AS
BEGIN
SELECT COUNT(*) FROM dbo.users WHERE UserName = @UserName
END
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace textbox_search_checkboxlist
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindData();
}
}
private void bindData()
{
SqlConnection con = new SqlConnection("Data Source=sirius\\SQLEXPRESS02;Initial Catalog=master;User ID=sa;Password=2344;Integrated Security=true");
con.Open();
SqlCommand cmd = new SqlCommand("Select * From dbo.person ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
cblist.DataSource = ds;
cblist.DataTextField = "Name";
cblist.DataValueField = "Id";
cblist.DataBind();
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="textbox_search_checkboxlist.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="javascript/jquery.min.js"></script>
<script type="text/javascript">
function SearchPerson(txtSearch, cblPerson) {
if ($(txtSearch).val() != "") {
var count = 0;
$(cblPerson).children('tbody').children('tr').each(function () {
var match = false;
$(this).children('td').children('label').each(function () {
if ($(this).text().toUpperCase().indexOf($(txtSearch).val().toUpperCase()) > -1)
match = true;
});
if (match) {
$(this).show();
count++;
}
else { $(this).hide(); }
});
$('#spnCount').html((count) + ' match (eslesen kayit sayisi)');
}
else {
$(cblist).children('tbody').children('tr').each(function () {
$(this).show();
});
$('#spnCount').html('');
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width: 550px">
<legend style="font-size:x-large">Search Person (Kisi Arama)</legend>
<asp:TextBox ID="txtSearch" runat="server" Width="246px" onkeyup="SearchPerson(this,'#cblist');"
placeholder="Search person" Font-Names="Tahoma" Font-Size="X-Large" ></asp:TextBox>
<span id="spnCount" style="font-size:x-large"></span>
<div style="height: 200px; overflow-y: auto; overflow-x: hidden">
<asp:CheckBoxList ID="cblist" runat="server" RepeatColumns="1"
RepeatDirection="Vertical" Width="240px" ClientIDMode="Static" Font-Names="Tahoma" Font-Size="X-Large">
</asp:CheckBoxList>
</div>
</fieldset>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN