向高手请教gridview的简单问题 web窗体中创建了gridview控件,绑定了数据源,我想在gridview中单击某一数据行后,触发一个相应的事件,请问用gridview的哪个属性才能单击后触发事件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以在 gridview 的 OnRowDataBound 事件里写点击事件,比如e.Row.Cells[0].Text = "<span onclick='点击事件'>" + e.Row.Cells[0].Text + "</span>";这就是给 gridview 第一列的的数据加一个点击事件 行绑定事件转换成对应控件控件对象.Attributes.add("onclick","alert('');"); dataGridView1_CellClick事件就可以 请问5楼的dataGridView1_CellClick事件怎么触发呢,应该用gridview的哪个属性来触发? 不对不对,web窗体里没有dataGridView啊,而且我用的是gridview 你是不是要做删除,修改?如果是的话直接在绑定的时候绑定一个linkbutton然后在gridview的command时间中写具体的执行过程linkbutton可以带两个参数一个是命令名称,一个是命令参数具体英文名称忘记了 <asp:LinkButton Runat="server" ID="lkbdel" CommandName="del" CommandArgument='<% DataBinder.Eval(Container.DataItem, "ID") %>'></asp:LinkButton>这是html页面的至于后台你可以在gridview的RowCommand(object sender, GridViewCommandEventArgs e)事件中用它的第二个参数 e来写 if (e.CommandName == "del") { }e.CommandArgument是用来得到你传递过来的那个参数(ID)的 不是,我的这个gridview控件放在一个ID为pnlSelect的Panel中,我想在单击数据行后,pnlSelect的visible属性变为false,同时另外一个ID为pnlAdd的Panel的visible属性变为true,同时把选中的该行的信息传到pnlAdd中的某些控件中,不知道该怎么实现,我觉得参数不好传啊 <ItemTemplate> <asp:LinkButton ID="lbtnDelete" runat="server" Text="删除" CommandName="Delete"></asp:LinkButton> </ItemTemplate>按钮有一个CommandName 的命令,可以和GridView的相关事件联系起来如删除: 在GridView 的Deleteing事件中 CommandName="Delete"编辑: CommandName=“Edit”-- RowEditing修改 CommandName="Update" RowUpdateing取消 CommandName=“Cancel” RowCanceling (单词可能写错了,注意一下就可以了) 给GridView添加行单击事件 功能:1:添加行单击事件2:获取行里面的单元格值主要代码: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button; if (btnHiddenPostButton != null) { e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID); // 额外样式定义 e.Row.Attributes["onmouseover"] = "javascript:this.style.background='red'"; e.Row.Attributes["onmouseout"] = "javascript:this.style.background=''"; e.Row.Attributes["style"] = "cursor:pointer"; e.Row.Attributes["title"] = "单击选择当前行"; } // 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引 //e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none"; } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = null; Label lbl = null; switch (e.CommandName) { case "HiddenPostButtonCommand": // 模板列 Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control row = cmdControl.NamingContainer as GridViewRow; // 当前行 // 如何访问单元格值 // string txt = row.Cells[0].Text; // 如何获取模板列中的 Label lbl = row.FindControl("MyLabelID") as Label; // 执行更多的自定义操作 // // string txt = lbl.Text.ToString(); Response.Write(txt); Response.Write(String.Format("GridView Version 当前第 {0} 行:", row.RowIndex + 1)); break; // case "Command2": // more cases // } } private void RowDataBound(string tag, object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //鼠标移动到某行上,该行变色 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#999999'"); //鼠标移开后,恢复 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); e.Row.Attributes["style"] = "cursor:pointer"; e.Row.Attributes["title"] = "单击选择行"; //双击后,实现该行的选中 //e.Row.Attributes.Add("onclick", "__doPostBack('" + tag + "','Select$" + e.Row.RowIndex + "')"); } } 你只需要在你的 RowDataBound事件里调用上面的方法 private void RowDataBound(string tag, object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //鼠标移动到某行上,该行变色 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#999999'"); //鼠标移开后,恢复 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); e.Row.Attributes["style"] = "cursor:pointer"; e.Row.Attributes["title"] = "单击选择行"; //双击后,实现该行的选中 //e.Row.Attributes.Add("onclick", "__doPostBack('" + tag + "','Select$" + e.Row.RowIndex + "')"); } } //事件调用如下: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { RowDataBound("GridView1", sender, e); } 请问13楼的前台代码怎么写,btnHiddenPostButton 按钮怎么和gridview联系起来? 不懂,这个正则表达式 老是遇到这问题 C# 如何根据一个字符串数组 绑定一个treeview 简体和繁体互想转换 想换程序学习的大侠进来看看。 谁能给我解释一下下面的这个代码啊,简化一下表达 timer计时器问题 急 printpreviewcontrol如何翻页? 如何让它支持滚轮?新版dotNet是否支持滚轮了? 继承的问题 在C#中如何确定已连接上Internet? C#winform写自动登陆网站读数据 帮忙,正则取值
控件对象.Attributes.add("onclick","alert('');");
如果是的话
直接在绑定的时候绑定一个linkbutton
然后在gridview的command时间中写具体的执行过程
linkbutton可以带两个参数
一个是命令名称,一个是命令参数
具体英文名称忘记了
<asp:LinkButton Runat="server" ID="lkbdel" CommandName="del" CommandArgument='<% DataBinder.Eval(Container.DataItem, "ID") %>'></asp:LinkButton>这是html页面的
至于后台你可以在gridview的RowCommand(object sender, GridViewCommandEventArgs e)事件中
用它的第二个参数 e来写 if (e.CommandName == "del")
{
}e.CommandArgument是用来得到你传递过来的那个参数(ID)的
<asp:LinkButton ID="lbtnDelete" runat="server" Text="删除" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>按钮有一个CommandName 的命令,可以和GridView的相关事件联系起来如删除: 在GridView 的Deleteing事件中 CommandName="Delete"
编辑: CommandName=“Edit”-- RowEditing
修改 CommandName="Update" RowUpdateing
取消 CommandName=“Cancel” RowCanceling (单词可能写错了,注意一下就可以了)
功能:1:添加行单击事件
2:获取行里面的单元格值
主要代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
if (btnHiddenPostButton != null) {
e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);
// 额外样式定义
e.Row.Attributes["onmouseover"] = "javascript:this.style.background='red'";
e.Row.Attributes["onmouseout"] = "javascript:this.style.background=''";
e.Row.Attributes["style"] = "cursor:pointer";
e.Row.Attributes["title"] = "单击选择当前行";
}
// 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引
//e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
} protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = null; Label lbl = null;
switch (e.CommandName) {
case "HiddenPostButtonCommand": // 模板列
Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
row = cmdControl.NamingContainer as GridViewRow; // 当前行
// 如何访问单元格值
// string txt = row.Cells[0].Text;
// 如何获取模板列中的 Label
lbl = row.FindControl("MyLabelID") as Label;
// 执行更多的自定义操作
//
//
string txt = lbl.Text.ToString();
Response.Write(txt);
Response.Write(String.Format("GridView Version 当前第 {0} 行:", row.RowIndex + 1));
break;
// case "Command2":
// more cases
//
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到某行上,该行变色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#999999'");
//鼠标移开后,恢复
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
e.Row.Attributes["style"] = "cursor:pointer";
e.Row.Attributes["title"] = "单击选择行";
//双击后,实现该行的选中
//e.Row.Attributes.Add("onclick", "__doPostBack('" + tag + "','Select$" + e.Row.RowIndex + "')");
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到某行上,该行变色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#999999'");
//鼠标移开后,恢复
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
e.Row.Attributes["style"] = "cursor:pointer";
e.Row.Attributes["title"] = "单击选择行";
//双击后,实现该行的选中
//e.Row.Attributes.Add("onclick", "__doPostBack('" + tag + "','Select$" + e.Row.RowIndex + "')");
}
} //事件调用如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
RowDataBound("GridView1", sender, e);
}