gridview 中哪里能找到 rowcommand ? 如何使用?

7758iloveu 2008-03-05 04:05:24
刚接触ASP.NET 现在想用GRIDVIEW 连接数据库, 想实现新增数据功能。

查了资料说,可以先建几个输入框,然后在GridView 控件中增添一按钮,将该按钮的CommandName 属性命名为insert, 再用鼠标双击RowCommand事件,书写代码给待定参数赋值即可。
双击RowCommand 事件后代码如下:

void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName=="insert")
{

SqlDataSource1.InsertParameters.Clear();
SqlDataSource1.InsertParameters.Add("xm",TextBox1);
SqlDataSource1.InsertParameters.Add("xb",TextBox1);
SqlDataSource1.InsertParameters.Add("age",TextBox1);
SqlDataSource1.Insert();
}
}

但是我无论如何也找不到 RowCommand在什么地方。 哪位高手指点一下,说的具体详细一些。因为刚刚解除ASP.NET,不知道如何下手。谢谢。如果能抓图演示一下最好。我的EMAIL: henry.han@rubylights.com
非常期待高手回复,谢谢!
...全文
329 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
小三金 2010-01-27
  • 打赏
  • 举报
回复
1 选中GRIDVERW
2 在属性窗口中 选择 事件 按钮 然后双击你的rowcommand事件 这时就自动生成了。
7758iloveu 2008-03-11
  • 打赏
  • 举报
回复
虽然没解决,还是谢谢上面的各位 。结贴啦!
7758iloveu 2008-03-07
  • 打赏
  • 举报
回复
哪位高手提供个实例教程。期待中。。。。。。。。。。。。。。。。。。
越详细越好:)
rangeon 2008-03-06
  • 打赏
  • 举报
回复
OnRowCommand
地下室小红叔 2008-03-06
  • 打赏
  • 举报
回复
选中窗体里的GridView控件 按F4弹出属性栏 然后单击上面像闪电的那个标记"Z" 最后在它下面的列表中找到RowCommand并双击 自动生成空处理方法
7758iloveu 2008-03-06
  • 打赏
  • 举报
回复
我的代码如下:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

if (e.CommandName == "add")
{

SqlDataSource1.InsertParameters.Clear();
SqlDataSource1.InsertParameters.Add("UserName", TextBox1.Text);
SqlDataSource1.InsertParameters.Add("Password", TextBox2.Text);
SqlDataSource1.InsertParameters.Add("UserRole", TextBox3.Text);
SqlDataSource1.Insert();
}


}

但是按钮提交后数据没有加进去,也没有报错。哪里有问题哦。高手指点一下,谢谢啦!
7758iloveu 2008-03-06
  • 打赏
  • 举报
回复
To xierfly : 能不能详细一些 :) 最好贴出来代码,因为我刚学,不知道如何下手。非常感谢!
xierfly 2008-03-06
  • 打赏
  • 举报
回复
那你就对应gridview显示项,添加数据就可以了。然后重新 绑定一下gridview。
7758iloveu 2008-03-06
  • 打赏
  • 举报
回复
感谢上面的朋友。这个事件终于终于找到了!

我是想通过添加几个文本框和一个按钮,添加数据。 不是GridView控件里面的“插入”按钮。,因为每行都有一个插入按钮不美观。
应该如何实现?还望高手指点详细一点。谢谢!

按钮是不是一般的BUTTON就行? 点击后应该加哪些具体代码?
buller 2008-03-05
  • 打赏
  • 举报
回复
在程序设计的时候,在GridView的在的网页页面上选中GridView,打开属性窗口,找到事件一页(属性顶像闪电一样的图标),查找RowCommand事件,找到后双击就可以添加这个RwoCommand事件了。

补充下,哈哈,当年也是很久找不到事件
阿非 2008-03-05
  • 打赏
  • 举报
回复
除了那种方法,你也可以手写

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" >



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

}
北京的雾霾天 2008-03-05
  • 打赏
  • 举报
回复
下面的代码示例演示如何使用 RowCommand 事件在单击某行的“添加”按钮时将客户名称从 GridView 控件添加到 ListBox 控件。


<%@ Page language="C#" %>

<script runat="server">

void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];

// Create a new ListItem object for the customer in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text);

// If the customer is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!CustomersListBox.Items.Contains(item))
{
CustomersListBox.Items.Add(item);
}
}
}

void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{

// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}

}

</script>

<html>
<body>
<form runat="server">

<h3>GridView RowCommand Example</h3>

<table width="100%">
<tr>
<td width="50%">

<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
onrowcreated="CustomersGridView_RowCreated"
runat="server">

<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="City"
headertext="City"/>
</columns>

</asp:gridview>

</td>

<td valign="top" width="50%">

Customers: <br/>
<asp:listbox id="CustomersListBox"
runat="server"/>

</td>
</tr>
</table>

<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>

</form>
</body>
</html>
阿非 2008-03-05
  • 打赏
  • 举报
回复
LS 正解~
北京的雾霾天 2008-03-05
  • 打赏
  • 举报
回复
在程序设计的时候,在GridView的在的网页页面上选中GridView,打开属性窗口,找到事件一页,查找RowCommand事件,找到后双击就可以添加这个RwoCommand事件了。
7758iloveu 2008-03-05
  • 打赏
  • 举报
回复
SqlDataSource1.InsertParameters.Add("xm",TextBox1);
SqlDataSource1.InsertParameters.Add("xb",TextBox1);
SqlDataSource1.InsertParameters.Add("age",TextBox1);

输入框名字有误,不小心输错了:
SqlDataSource1.InsertParameters.Add("xm",TextBox1);
SqlDataSource1.InsertParameters.Add("xb",TextBox2);
SqlDataSource1.InsertParameters.Add("age",TextBox3);

但是我无论如何也找不到 RowCommand在什么地方。 哪位高手指点一下,说的具体详细一些。因为刚刚解除ASP.NET,不知道如何下手。谢谢。如果能抓图演示一下最好。我的EMAIL: henry.han@rubylights.com
非常期待高手回复,谢谢!
在单击 GridView 控件的按钮时,将引发 RowCommand 事件。 GridView 控件具有内置功能,用于进行编辑、删除和分页等操作。 还可以添加按钮并使用 RowCommand 事件向控件添加自定义功能。 可以通过下面的方式向 GridView 控件添加自定义功能: 向 GridView 控件添加 ButtonField 字段。 向 GridView 控件的模板添加 Button、LinkButton 或 ImageButton 控件。 可以使用事件参数的 CommandName 属性在事件处理程序方法标识按钮的功能。 如果使用的是 ButtonField 或 TemplateField 对象,则还可以使用 CommandArgument 属性来标识当前行。 使用的是 ButtonField 对象时,CommandArgument 属性自动设置为行索引。 使用的是 TemplateField 对象时,控件不会自动设置 CommandArgument 属性。 在这种情况下,如果必须在事件处理程序确定行索引,则可以使用数据绑定表达式将该按钮的 CommandArgument 属性设置为行索引。 响应 GridView 控件的按钮事件 将按钮的 CommandName 属性设置为标识其功能的字符串,如“打印”或“复制”。 如果使用的是 TemplateField 对象并且必须在事件处理程序方法访问行索引,则将按钮的 CommandArgument 属性设置为标识当前行的表达式。 下面的示例演示如何将 TemplateField 列某个按钮的 CommandArgument 属性设置为当前行索引。 在该示例,该列包含一个显示购物车的 Button 控件。 VBC#C++F#JScript 复制不支持该语言或没有可用的代码示例。 VBC#C++F#JScript 复制 CommandName="AddToCart" CommandArgument="" Text="Add to Cart" /> 为 GridView 控件的 RowCommand 事件创建一个方法。 在该方法,执行下列操作: 检查事件参数对象的 CommandName 属性来查看传入什么字符串。 如果需要,使用 CommandArgument 属性检索包含该按钮的行的索引。 为用户单击的按钮执行相应的逻辑。 下面的示例演示响应 GridView 控件的按钮单击的方法。 在该示例,TemplateField 列的按钮发送命令“AddToCart”。 RowCommand 事件处理程序确定被单击的按钮。 如果被单击的是购物车按钮,则代码执行相应的逻辑。
ASP.NET实现Gridview隐藏/显示列源码 介绍: 这篇文章演示如果让用户有显示/隐藏他们需要的GridView的列的功能,这是非常有用的,因为在GridView的所有列并不是每个的用户都需要的.用户想根据自己的需求看到想要的列.而不是显示一个巨大的gridview,霸占了整个屏幕,而是一个整洁的Gridview,而且它有所有你需要的列.对于页面的打印这也是一个非常有用的技术,因为用户可以灵活地选择GridView的列打印。 背景: RowCreated 和ItemDataBound 事件允许你用多种方式注入HTML, CSS,和JavaScript 来增强GridView 控件的功能。 文章将会演示两种显示和隐藏GridView列的方法,一种是客户端的方法,另外一种是服务段的方法. 在客户段显示和隐藏GridView的列 大部分代码是在GridViewRowCreated事件生成客户端的功能的。当GridView的Header行被创建后,一个带负号的HyperLink被插入每个Header行的单元格用来隐藏列。 这个hyperlink通过它的onclick事件调用一个HideCol的Javascript方法,CSS类用来增加负号的大小,当每个数据行被创建的时候,一个Id将会被添加到每行用来让Javascript区分每一行. 代码 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { GridView gridView = (GridView)sender; StringBuilder sb = new StringBuilder(); // For the header row add a link to each header // cell which can call the HideCol javascript method if (e.Row.RowType == DataControlRowType.Header) { // Loop through each cell of the row for (int columnIndex = 0; columnIndex 0) { Label columnTextLabel = new Label(); columnTextLabel.Text = e.Row.Cells[columnIndex].Text; e.Row.Cells[columnIndex].Controls.Add(columnTextLabel); } } } // Give each row an id if (e.Row.RowType == DataControlRowType.Pager) e.Row.Attributes.Add("id", gridView.ClientID + "_pager"); else e.Row.Attributes.Add("id", gridView.ClientID + "_r" + e.Row.RowIndex.ToString()); } SetupShowHideColumns方法生成“Show Columns”下拉菜单的HTML,输出在Literal控件上面 。 代码 private void SetupShowHideColumns(GridView gridView, Literal showHideColumnsLiteral) { StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append(""); sb.Append("- Show Column -"); showHideColumnsLiteral.Text = sb.ToString(); } 在数据绑定到GridView之后,其余的工作由ShowHideColumns.js的javascript来完成.当列头的hyperlink被点击的时候后,它将会传递GridView的名字,列的索引和列名给HideCol方法,这个方法能找到这一列的每个单元格,每个单元格的将添加display:none样式,用来隐藏这一列. 当选择"Show Column"的选项后,Javascript方法ShowCol将会被调用,它将移除每个单元格的display:none样式,这一列将会被再次显示. 在服务端显示/隐藏GridView的列 服务端的例子将通过RowCreated事件给每个列头添加一个负号,这次是使用LinkButton控件.设置CommandName和CommandArgument属性,这样当通过LinkButton引发RowCommand事件时,相关的列都可以隐藏。以前隐藏的列索引存储在一个List,这些列在建立时,将会被隐藏的。 代码 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { // For the header row add a link button to each header // cell which can execute a row command if (e.Row.RowType == DataControlRowType.Header) { // Loop through each cell of the header row for (int columnIndex = 0; columnIndex 0) { Label columnTextLabel = new Label(); columnTextLabel.Text = e.Row.Cells[columnIndex].Text; e.Row.Cells[columnIndex].Controls.Add(columnTextLabel); } } } // Hide the column indexes which have been stored in hiddenColumnIndexes foreach(int columnIndex in hiddenColumnIndexes) if (columnIndex 0) { this.GridView1ShowHideColumns.Visible = true; this.GridView1ShowHideColumns.Items.Add(new ListItem("-Show Column-", "-1")); foreach (int i in hiddenColumnIndexes) this.GridView1ShowHideColumns.Items.Add( new ListItem(columnNames[i], i.ToString())); } else { this.GridView1ShowHideColumns.Visible = false; } } 示例项目的例子: 客户端的例子: •C#.NET - Client-side example accessing data stored in session. •C#.NET - Client-side example which includes: MasterPage, UpdatePanel, GridView editing, paging and sorting, accessing data via the SqlDataSource control. •VB.NET - Client-side example accessing data stored in session. 服务端的例子 •C#.NET - Server-side example accessing data stored in session. •C#.NET - Server-side example which includes: MasterPage, UpdatePanel, GridView editing, paging and sorting, accessing data via the SqlDataSource control. •VB.NET - Server-side example accessing data stored in session. 结论: 如果你想让你的用户能够显示和隐藏在ASP.NET GridView的列,那么这种技术可能是非常有用的。 原文:http://www.codeproject.com/KB/webforms/ShowHideGridviewColumns.aspx 作者:朱祁林 出处:http://zhuqil.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getData(); } } //得到数据 public void getData() { this.GridView1.DataSource = BLL.mesManager.getGridData(); this.GridView1.DataBind(); } // 绑定之后发生 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lbtn = (LinkButton)e.Row.FindControl("LinkButton2"); e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor"); string aa = e.Row.Cells[3].Text.ToString(); lbtn.Attributes.Add("OnClick", "return confirm('您确认要删除吗?')"); } } //发生时间时激发 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "del") { int id = Convert.ToInt32(e.CommandArgument); int resultNum = BLL.mesManager.del(id); if (resultNum > 0) { Response.Write("<script>alert('删除成功');CommandName == "upd") { int id = Convert.ToInt32(e.CommandArgument); Response.Redirect("Update.aspx?id=" + id); } } protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { } //pageindexchangeing protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; getData(); } //全部删除 protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { int id=Convert.ToInt32( this.GridView1.Rows[i].Cells[1].Text); BLL.mesManager.del(id); } } getData(); } }

62,073

社区成员

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

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

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

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