Hi,
i am using gridview to view topics from db, i add column for checkbox to delete row from gridview
but if i checked one, no one is deleted !
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="searchResults.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="searchRes">
<br />
<asp:GridView ID="GridView1" runat="server" BorderStyle="Solid" RowStyle-BorderStyle="None" RowStyle-BorderWidth="0" Width="800" ShowHeader="false" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="IdSelector" runat="server" OnCheckedChanged="Button1_Click" AutoPostBack="False" />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataTextField="title" DataNavigateUrlFields="SubjectId" DataNavigateUrlFormatString="~/Topic/{0}" HeaderText="العنوان" ItemStyle-Width="400"/>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="حذف" />
<br />
</div>
</asp:Content>
and this is the behind code
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;
using System.Web.Routing;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=SALAH-PC\SQLEXPRESS;Initial Catalog=NewBCDB;Integrated Security=True;Pooling=False");
SqlDataAdapter Adapter;
DataSet ds = new DataSet();
SqlDataReader reader;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
Button1.Visible = false;
if (Page.RouteData.Values["Id"] != null)
{
if (!Page.IsPostBack)
{
int id;
if (int.TryParse(Page.RouteData.Values["Id"].ToString(), out id) == true)
{
if (Session["Type"] != null && Session["Type"].ToString() == "Admin")
{
Adapter = new SqlDataAdapter("SELECT Subjects.SubjectId,Subjects.Title,AgePeriods.Period,Users.UserName,Subjects.Status From Subjects INNER JOIN AgePeriods ON Subjects.AgeId = AgePeriods.AgePeriodId INNER JOIN Users ON Subjects.UserId = Users.UserId WHERE CId = '" + id + "' ORDER BY Subjects.SubjectId DESC ", con);
Adapter.Fill(ds, "Subjects");
GridView1.DataSource = ds.Tables["Subjects"];
GridView1.DataBind();
Session["Data"] = ds.Tables["Subjects"];
}
else
{
Adapter = new SqlDataAdapter("SELECT Subjects.SubjectId,Subjects.Title,AgePeriods.Period,Users.UserName,Subjects.Status From Subjects INNER JOIN AgePeriods ON Subjects.AgeId = AgePeriods.AgePeriodId INNER JOIN Users ON Subjects.UserId = Users.UserId WHERE CId = '" + id + "' AND Subjects.status = '1' ORDER BY Subjects.SubjectId DESC ", con);
Adapter.Fill(ds, "Subjects");
GridView1.DataSource = ds.Tables["Subjects"];
GridView1.DataBind();
Session["Data"] = ds.Tables["Subjects"];
}
}
else
{
Response.Redirect("~/homepage.aspx");
}
}
}
if (Page.RouteData.Values["index"] != null)
{
int index;
if (int.TryParse(Page.RouteData.Values["index"].ToString(), out index) == true)
{
if (index <= GridView1.PageCount)
{
GridView1.PageIndex = index - 1;
GridView1.DataSource = Session["Data"];
GridView1.DataBind();
if (Session["Type"] != null && Session["Type"].ToString() == "Admin")
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[2].Visible = false;
GridView1.Rows[i].Cells[3].Visible = false;
GridView1.Rows[i].Cells[6].Visible = false;
if (GridView1.Rows[i].Cells[6].Text == "2")
{
GridView1.Rows[i].BackColor = System.Drawing.Color.LightYellow;
}
}
}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[0].Visible = false;
GridView1.Rows[i].Cells[2].Visible = false;
GridView1.Rows[i].Cells[3].Visible = false;
GridView1.Rows[i].Cells[6].Visible = false;
}
}
}
Session["previous"] = Request.Url.AbsoluteUri;
}
}
else
{
}
if (GridView1.Rows.Count == 0 )
{
Button1.Visible = false;
}
if (Session["Type"] != null && Session["Type"].ToString() == "Admin")
{
Button1.Visible = true;
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int last = GridView1.PageIndex;
int newIndex = e.NewPageIndex;
GridView1.PageIndex = newIndex;
int c = int.Parse(Page.RouteData.Values["Id"].ToString());
GridView1.DataSource = Session["Data"];
GridView1.DataBind();
newIndex = newIndex + 1;
Response.Redirect("~/class/"+c+"/"+newIndex);
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
foreach (GridViewRow dr in GridView1.Rows)
{
CheckBox selector = (CheckBox)dr.FindControl("IdSelector");
if (selector.Checked == true)
{
int id = int.Parse(dr.Cells[2].Text);
com = new SqlCommand("UPDATE Subjects SET Status = '2' WHERE SubjectId = '" + id + "'", con);
com.ExecuteNonQuery();
}
}
con.Close();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.DataSource = Session["Data"];
GridView1.DataBind();
}
}
i think it is postingback problem, but how to solve it ?