DataGrid问题急!

xuzhengbait 2007-04-05 09:54:32
我要在DataGrid上加模板列,我在模板列加了checkbox想实现的功能是想对那一行的数据进行修改只要点击checkbox就可以得到相应数据进行更新和删除。点击页首的checkbox低下的checkbox全选!
要代码!
               拜托了!!!
...全文
252 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzhengbait 2007-04-06
  • 打赏
  • 举报
回复
窗体加按钮就可以全选但是要用模板的就不行!无法引发OnCheckedChanged事件!
jimu8130 2007-04-06
  • 打赏
  • 举报
回复
是不是事件丢失了?
xuzhengbait 2007-04-06
  • 打赏
  • 举报
回复
改了没有反映啊
jimu8130 2007-04-06
  • 打赏
  • 举报
回复
建议你不要使用这样的方式来作全选,因为刷新太严重了,可以考虑js来作,这样比较好
jimu8130 2007-04-06
  • 打赏
  • 举报
回复
OnCheckedChanged-----你的autopostback有没有设置为true?
xuzhengbait 2007-04-06
  • 打赏
  • 举报
回复
说明白点啊拜托
net_lhb 2007-04-06
  • 打赏
  • 举报
回复
UP上面有正解了呀!
xuzhengbait 2007-04-06
  • 打赏
  • 举报
回复
public void CheckAll(object sender, System.EventArgs e)
{
CheckBox cbAll = (CheckBox)sender;
if(cbAll.Text=="全选")
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
cb.Checked = cbAll.Checked;
}
事件怎么才能运行啊!OnCheckedChanged改了怎么没有反应啊
amandag 2007-04-05
  • 打赏
  • 举报
回复
aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MsDataGrid
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class SelAndDropAll : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.DataGrid dgShow;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnDelete.Attributes.Add("onclick", "return confirm('您真的要删除所选项吗?');");
if(!IsPostBack)
BindData();

}
private void BindData()
{
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
DataSet ds = new DataSet();
da.Fill(ds,"studentinfo");
dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
dgShow.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = e.Item.ItemIndex;
BindData();

}

private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = -1;
BindData();
}
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShow.CurrentPageIndex = e.NewPageIndex;
BindData();
}
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();

}
////////////////////////////////////////////////////////////
//说明:执行制定SQL语句/////////////////////////////////////
///////////////////////////////////////////////////////////
private void ExecuteSql(string strSql)
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
SqlConnection conn =new SqlConnection(strconn);
SqlCommand com = new SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch(Exception e)
{
Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;

}
}

private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strSex = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
ExecuteSql(strSql);
dgShow.EditItemIndex = -1;
BindData();

}
public void CheckAll(object sender, System.EventArgs e)
{
CheckBox cbAll = (CheckBox)sender;
if(cbAll.Text=="全选")
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
cb.Checked = cbAll.Checked;
}
}
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
if(cb.Checked)
{
//以下执行删除操作
int nID = int.Parse(dgi.Cells[0].Text);
string strSql = "delete from tbStudentinfo where studentid="+nID;
ExecuteSql(strSql);
}
}
dgShow.CurrentPageIndex = 0;
BindData();
}

}
}

amandag 2007-04-05
  • 打赏
  • 举报
回复
aspx页面

<%@ Page language="c#" Codebehind="SelAndDropAll.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.SelAndDropAll" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGrid使用举例</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="dgShow" style="Z-INDEX: 101; LEFT: 31px; POSITION: absolute; TOP: 93px" runat="server" Width="842px" Height="172px" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow" CellPadding="2" GridLines="None" ForeColor="Black" PageSize="1" AutoGenerateColumns="False">
<SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
<AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>
<FooterStyle BackColor="Tan"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="StudentID" ReadOnly="True" HeaderText="学生ID"></asp:BoundColumn>
<asp:BoundColumn DataField="StudentName" HeaderText="学生姓名"></asp:BoundColumn>
<asp:BoundColumn DataField="StudentPass" HeaderText="密码"></asp:BoundColumn>
<asp:BoundColumn DataField="Sex" HeaderText="性别"></asp:BoundColumn>
<asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
<asp:BoundColumn DataField="Email" HeaderText="邮件地址"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="操作" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="选择">
<HeaderTemplate>
<asp:CheckBox id="cbAll" runat="server" OnCheckedChanged="CheckAll" Text="全选" AutoPostBack="True"></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="cbSelect" runat="server" AutoPostBack="True"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle>
</asp:DataGrid>
<asp:Button id="btnDelete" style="Z-INDEX: 102; LEFT: 34px; POSITION: absolute; TOP: 47px" runat="server" Text="删除"></asp:Button></FONT>
</form>
</body>
</HTML>

sd4709494 2007-04-05
  • 打赏
  • 举报
回复
<script language="javascript">//复选框全选
<!--
function SelectAll(tempControl)
{
var theBox=tempControl;
xState=theBox.checked;
elem=theBox.form.elements;
for(i=0;i<elem.length;i++)
if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
{
if(elem[i].checked!=xState)
elem[i].click();
}
}

//-->
</script>
jimu8130 2007-04-05
  • 打赏
  • 举报
回复
http://www.cnblogs.com/lovecherry/archive/2005/05/15/155835.html
去这里好好研究啊
cpp2017 2007-04-05
  • 打赏
  • 举报
回复
function CheckAll() { var bChecked = event.srcElement.checked; var objs = document.getElementsByName("chkId"); for(var i=0;i<objs.length;i++) { objs[i].checked = bChecked; } } <ASP:TEMPLATECOLUMN ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" headertext="<input type='checkbox' onclick='javascript:CheckAll()'>" > <ITEMTEMPLATE> <input type="checkbox" name="chkId" id="chk<%#DataBinder.Eval(Container.DataItem,"Id")%>" onpropertychange="javascript:return CheckBoxPropertisChange();" <%#GetChecked(DataBinder.Eval(Container.DataItem,"Id").ToString())%> > </ITEMTEMPLATE> </ASP:TEMPLATECOLUMN>

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧