GridView与checkbox的问题
我在GridView中加入的是HTML的checkbox,我怎样实现选择删除行?
给你发一个我自己做的例子吧!试试看。
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> <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>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Text="第一页" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Text="上一页" CommandArgument="prev"></asp:linkbutton>
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Text="下一页" CommandArgument="next"></asp:linkbutton>
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Text="最后一页" CommandArgument="last"></asp:linkbutton>
</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)
{
}
}