| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 918 人关注过本帖
标题:GridView与checkbox的问题
只看楼主 加入收藏
rxez8
Rank: 1
等 级:新手上路
帖 子:6
专家分:4
注 册:2009-8-2
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
GridView与checkbox的问题
我在GridView中加入的是HTML的checkbox,我怎样实现选择删除行?
搜索更多相关主题的帖子: checkbox GridView 
2009-08-03 20:16
mengtinglyna
Rank: 2
来 自:云南
等 级:论坛游民
帖 子:23
专家分:85
注 册:2009-7-30
收藏
得分:3 
给你发一个我自己做的例子吧!试试看。
aspx:
<html xmlns="http://www. >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" style="text-align:center" runat="server">
    <fieldset>
        <legend style=" float:left; font-weight:normal; font-size:12px" >稿件分审</legend>
        <table>
          <tr>
          <td>
              <asp:GridView ID="GridView1" runat="server" Width="610px" AllowPaging="True" AutoGenerateColumns="False" BorderColor="CornflowerBlue"
                  CellPadding="4" ForeColor="#333333" OnPageIndexChanging="GV_PageIndexChanging" GridLines="None" BorderStyle="Solid" BorderWidth="1px" Font-Names="新宋体" Font-Size="Medium" DataKeyNames="Did">
                  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                  <Columns>
                  <asp:TemplateField HeaderText="审批">
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" />
                                </ItemTemplate>
                            </asp:TemplateField>

                     
                      <asp:BoundField DataField="DraftTitle" HeaderText="标题" SortExpression="DraftTitle" />
                      <asp:BoundField DataField="Sname" HeaderText="学科" SortExpression="Sname" />
                      <asp:BoundField DataField="Version" HeaderText="版本" SortExpression="Version" />
                     
                      <asp:TemplateField HeaderText="详细">
                <ItemTemplate>
                <asp:Label ID="Label1" runat="server"><a href='../draftdetail.aspx?id=<%# DataBinder.Eval(Container.DataItem, "Did")%>'>全文</A></asp:Label>
                 </ItemTemplate>
                 </asp:TemplateField>
                 
               
        
                 <asp:TemplateField HeaderText="专家">
                                <ItemTemplate>
                                    <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# ddlbind()%>' DataValueField="UsersID" DataTextField="UsersNmae" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>

                  </Columns>
                  <RowStyle BackColor="#EFF3FB" />
                  <EditRowStyle BackColor="#2461BF" />
                  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                  <HeaderStyle BackColor="#87BAE9" Font-Bold="True" ForeColor="White" />
                  <AlternatingRowStyle BackColor="White" />
              </asp:GridView>
            
        
        </td>
        </tr>
        <tr style="font-family:宋体; font-size:14px; width: 249px; height: 14px;">  
                 <td>&nbsp;&nbsp;&nbsp;<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged"
                        Text="全选" />
                    <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="确定" OnClick="Button2_Click" />
                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />
                    <asp:label id="lblPageCount" runat="server"></asp:label>&nbsp;
                     <asp:label id="lblCurrentIndex" runat="server"></asp:label>
                     <asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server"  Text="第一页" CommandArgument="0"></asp:linkbutton> &nbsp;
                     <asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server"  Text="上一页"  CommandArgument="prev"></asp:linkbutton> &nbsp;
                     <asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server"  Text="下一页"  CommandArgument="next"></asp:linkbutton> &nbsp;
                     <asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server"  Text="最后一页" CommandArgument="last"></asp:linkbutton> &nbsp;
        
</td></tr>
        
        </table>
   
    </fieldset>
    </form>
</body>
</html>
aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

using ContributeOnline.DataAccessHelper;
using ContributeOnline.BusinessLogicLayer;
public partial class editor_newDraft : System.Web.UI.Page
{   //主要代码
    DropDownList ddl = null;
    String str = null;
    CheckBox cbox = null;
    SqlConnection sqlcon;
    int i = 0;//
    string strCon = "Data Source=(local);Database=ContributeOnline;Uid=sa;Pwd=sa";
    protected void Page_Load(object sender, EventArgs e)//这个是session记录登录用户状态的,可以不管
    {      
        if (!IsPostBack)
        {
            if (Session["login_name"] != "")
            {
                InitData();
                ShowPageChangedStatus();
            }
            else
            {
                Jscript.AlertAndRedirect("没有登陆,请登陆!", "../index.aspx");
            }
            
        }
    }

    public SqlDataReader ddlbind()//数据库绑定gridview的,这个也可以不管
    {
        string sqlstr = "select UsersID, UsersNmae from UsersInfo where UsersType ='4' order by Uid asc";
        sqlcon = new SqlConnection(strCon);
        SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        return sqlcom.ExecuteReader();
    }


    /// <summary>
    /// 读取用户数据
    /// </summary>
    private void InitData()
    {
        DataSet ds = DraftInfo.QueryDraftBySubjectT(1);
        GridView1.DataSource = ds;
        GridView1.DataBind();//数据库绑定代码
        
        GridView1.DataKeyNames = new string[] {"Did"};
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");//与dropdownlist的绑定,可以不看的
        }

    }
    private void ShowPageChangedStatus()//分页代码
    {

        lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
        lblPageCount.Text = "总共 " + GridView1.PageCount.ToString() + " 页";
    }
    //判定翻页方向
    public void PagerButtonClick(object sender, EventArgs e)
    {
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "next":
                if (GridView1.PageIndex < (GridView1.PageCount - 1))
                {
                    GridView1.PageIndex += 1;
                }
                break;
            case "prev":
                if (GridView1.PageIndex > 0)
                {
                    GridView1.PageIndex -= 1;
                }
                break;
            case "last":
                GridView1.PageIndex = (GridView1.PageCount - 1);
                break;
            default:
                GridView1.PageIndex = System.Convert.ToInt32(arg);
                break;
        }
        GridView1.DataBind();
        ShowPageChangedStatus();
    }

    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        InitData();
        int startIndex;
        startIndex = GridView1.PageIndex * GridView1.PageSize;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
        ShowPageChangedStatus();
    }


    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)//主要代码,和checkbox的使用,看看吧
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {

                cbox.Checked = false;
            }

        }
    }



    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
         DropDownList ddl = (DropDownList)sender;
         string str = ddl.SelectedItem.Text;


    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        String users = Convert.ToString(Session["Login_name"]);
        sqlcon = new SqlConnection(strCon);
        SqlCommand sqlcom;
        
      
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
         
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");//主要代码
            DropDownList ddl = (DropDownList)this.GridView1.Rows[i].FindControl("DropDownList1");
            string str = ddl.SelectedItem.Value;
            string did = GridView1.DataKeys[i].Value.ToString();
            int id = Convert.ToInt32(did);
            DraftInfo draft = new DraftInfo();
            draft.LoadData(id);
            string notes = "分审了稿件:" + draft.draftTitle;
         
            if (cbox.Checked == true)   
            {
               
                string dd = GridView1.DataKeys[i].Value.ToString();
                string sqlstr1 = "update DraftInfo set DraftState=3 where Did= '" + GridView1.DataKeys[i].Value.ToString() +"'";
                sqlcom = new SqlCommand(sqlstr1, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
     //以下都是条件满足之后,对数据库进行的信息添加操作,可以不管的
                string sqlstr = "insert into permission values ('" + GridView1.DataKeys[i].Value.ToString() +" ','"+ str +"')"; //插入分配信息
                sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();


                Hashtable ht1 = new Hashtable();

                ht1.Add("UsersID", SqlStringFormat.GetQuotedString(users));
                ht1.Add("NoteTime", SqlStringFormat.GetQuotedString(DateTime.Now.ToString()));
                ht1.Add("Note", SqlStringFormat.GetQuotedString(notes));

                NoteInfo note = new NoteInfo();
                note.Add(ht1);
               
            }
        }
        Response.Write("<Script Language=JavaScript>alert('成功!');window.navigate('newDraft.aspx');</Script>");

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)//主要代码
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }
}

人生不过是一个回头!
2009-08-04 10:10
田代印
Rank: 2
来 自:山东
等 级:论坛游民
帖 子:16
专家分:10
注 册:2009-8-3
收藏
得分:3 
他问的不是这个问题吧!呵呵
2009-08-06 14:49
快速回复:GridView与checkbox的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014200 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved