如何从gridview获取选定的chechbox? [英] how to get selected chechboxs from gridview ?

查看:74
本文介绍了如何从gridview获取选定的chechbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i我正在使用gridview查看来自db的主题,我添加了复选框列以从gridview中删除行

但如果我选中了一行,则不会删除任何人!

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认为这是回帖问题,但如何解决呢?



i think it is postingback problem, but how to solve it ?

推荐答案

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
{


}





您在页面加载事件中的这段代码可能会重新绑定Gridview,这会重置您选中的复选框。设置断点并调试代码以查看它的位置......



Your this bit of code in the page load event might be rebinding your Gridview which is resetting your selected checkbox. Put a breakpoint and debug your code to see where it is going...


这篇关于如何从gridview获取选定的chechbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆