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
非常期待高手回复,谢谢!
...全文
352 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
非常期待高手回复,谢谢!

62,243

社区成员

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

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

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

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