谁来帮帮我 关于GridView的问题

Eagle_ice 2008-10-03 11:17:55
这是前台页面UserManage.aspx的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserManage.aspx.cs" Inherits="UserManage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="UserView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="UserView_RowCommand" OnRowDataBound="UserView_RowDataBound">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="UserID" HeaderText="用户ID" SortExpression="UserID" />
<asp:BoundField DataField="UserName" HeaderText="用户名称" SortExpression="UserName" />
<asp:BoundField DataField="Address" HeaderText="用户地址" SortExpression="Address" />
<asp:BoundField DataField="Telephone" HeaderText="联系电话" SortExpression="Telephone" />
<asp:TemplateField HeaderText="用户操作">
<ItemTemplate>
<asp:Button ID="EditBtn" runat="server" Text="编辑" CommandName="Modify"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>


我想在后台代码中添加这样一段代码:点击编辑按钮,获取数据绑定列的第一项(UserID)的值,同时传到编辑页面,在DataGrid中可以在DataGrid_ItemCommand(object source, DataGridCommandEventArgs e)事件中通过e.Item.Cells[0].Text获取到,但是在GridView_RowCommand(object sender, GridViewCommandEventArgs e)事件中不知道如何获取,在GridView_RowDataBound(object sender, GridViewRowEventArgs e)事件中可以,但是我要跳转页面需用到CommandName参数,可是它在RowDataBound中有没有,请较一下。以下是我的后台代码:

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;

public partial class UserManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = DB.createDB();
con.Open();
SqlCommand cmd = new SqlCommand("select * from Users",con);
SqlDataReader sdr = cmd.ExecuteReader();
UserView.DataSource = sdr;
UserView.DataBind();
con.Close();
}
protected void UserView_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = e.CommandName;
switch (commandName)
{
case "Modify":
Response.Redirect("EditUser.aspx?userID="+);//就是这里链接不过去,获取不到值
break;
case "Delete":
break;

}
}
protected void UserView_RowDataBound(object sender, GridViewRowEventArgs e)
{
string userId = e.Row.Cells[0].Text;//这里可以获取到
}
}

...全文
125 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eagle_ice 2008-10-03
  • 打赏
  • 举报
回复
以上的方法都不行啊...麻烦4楼的给个标准的 谢谢...
xuexiziji 2008-10-03
  • 打赏
  • 举报
回复
支持1楼,加上 CommandArgument就可以了
amandag 2008-10-03
  • 打赏
  • 举报
回复
不过这种做法都不标准,标准的方法是设置编辑按钮的CommandName为Edit,然后在RowEditing事件中处理

同样,得到主键的方式也不正规,应该是设置GridView的DataKeyNames为userID,然后通过 GridView1.DataKeys[index].Value.ToString()得到
amandag 2008-10-03
  • 打赏
  • 举报
回复
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = ((e.CommandSource as Button).NamingContainer as GridViewRow).RowIndex;
string userID = GridView1.Rows[index].Cells[0].Text;
//下面你的操作
}
namhyuk 2008-10-03
  • 打赏
  • 举报
回复
case "Modify":
Response.Redirect(string.Format("EditUser.aspx?userID={0}",e.CommandArgument));//就是这里链接不过去,获取不到值
break;
namhyuk 2008-10-03
  • 打赏
  • 举报
回复
asp:TemplateField HeaderText="用户操作">
<ItemTemplate>
<asp:Button ID="EditBtn" runat="server" Text="编辑" CommandName="Modify" CommandArgument='<%# Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>

protected void UserView_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = e.CommandName;
switch (commandName)
{
case "Modify":
Response.Redirect("EditUser.aspx?userID="+ Convert.ToInt32(e.CommandArgument));//就是这里链接不过去,获取不到值
break;
case "Delete":
break;

}
}
Eagle_ice 2008-10-03
  • 打赏
  • 举报
回复
楼上的是个强人...
  • 打赏
  • 举报
回复
你只要设计为
<asp:TemplateField HeaderText="用户操作">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UserID") %>' style="display:none" />
<asp:Button ID="EditBtn" runat="server" Text="编辑" OnClick="EditBtn_Click“>
</ItemTemplate>
</asp:TemplateField>

而后就可以在后台
    protected void EditBtn_Click(object sender, EventArgs e)
{
Button bt = sender as Button;
Label lb = bt.FindControl("Label1") as Label;
Response.Redirect("EditUser.aspx?userID="+lb.Text);
}


用不着那种上溯到GridView去的劳什子做法。


另外,把GridView和DetailsView放在一个页面上(例如左右摆放),每当GridView上选择一行时可以及时到DetailsView中编辑(其它时候DetailsView的Visible为False),这样的页面更实用一些。
Eagle_ice 2008-10-03
  • 打赏
  • 举报
回复
按照楼上们的方法 出现了这个错误 无论怎么改 就是有这个错误 不懂...:
http://topic.csdn.net/u/20081003/16/59b7d898-dd2a-4dce-b961-d77ac432ba7b.html
amandag 2008-10-03
  • 打赏
  • 举报
回复
<asp:GridView ID="UserView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="UserID" OnRowEditing="GridView1_RowEditing" >
<Columns>
<asp:BoundField DataField="UserID" HeaderText="用户ID" SortExpression="UserID" />
<asp:BoundField DataField="UserName" HeaderText="用户名称" SortExpression="UserName" />
<asp:BoundField DataField="Address" HeaderText="用户地址" SortExpression="Address" />
<asp:BoundField DataField="Telephone" HeaderText="联系电话" SortExpression="Telephone" />
<asp:TemplateField HeaderText="用户操作">
<ItemTemplate>
<asp:Button ID="EditBtn" runat="server" Text="编辑" CommandName="Edit"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Delete" runat="server" Text="删除" CommandName="Delete"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Response.Redirect("EditUser.aspx?userID=" + GridView1.DataKeys[e.NewEditIndex].Value.ToString());
}
gameboyerik001 2008-10-03
  • 打赏
  • 举报
回复
“在DataGrid中可以在DataGrid_ItemCommand(object source, DataGridCommandEventArgs e)事件中通过e.Item.Cells[0].Text获取到,但是在GridView_RowCommand(object sender, GridViewCommandEventArgs e)事件中不知道如何获取”


楼主到底用的2003还是2005

太诡异了
  • 打赏
  • 举报
回复
代码

一个模板中的Button.FindControl(另一个模板中的Label的ID)

完全可以返回你要的UserID所绑定的那个控件。
  • 打赏
  • 举报
回复
参考

http://topic.csdn.net/u/20080916/15/d22842d0-64c1-414c-8bd2-36f5edc74141.html

中我的回复。

你可以在设计窗口中进入编GridView设计窗口,选中UserID那一列,把它变为模板列,这样方便看到它的ID到底是什么。
namhyuk 2008-10-03
  • 打赏
  • 举报
回复
我只能说莫名其妙。

问题剩下的最多也就是UserID可能包含中文,需要Server.UrlEncoder()一下,目标页再DeCode的问题。

不行就是见鬼了。自己好好琢磨琢磨吧!

62,041

社区成员

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

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

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

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