【在线】GridView中添加了一个按钮,单击该按钮时怎样获得对应行的个项值? rt 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 GridView中添加了一个按钮,单击该按钮时怎样获得对应行的各项值?比如第一列 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Display") { DataRow isplay_Row = ((DataRow)((Button)e.CommandSource).Parent.Parent); } }所在行的按钮的父控件的父控件按钮->单元格->行 只要取出同行的值就行了,比如取出第二行,字段为name的值怎么取? protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Display") { DataRow isplay_Row = ((DataRow)((Button)e.CommandSource).Parent.Parent); isplay_Row["name"].ToString(); } }当前行name字段 出现错误,无法将...Control转换为DataRow //我是新手,不知对不对 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int liRow; string lsField; if (e.CommandName == "ShowDetail") { liRow= Convert.ToInt32(e.CommandArgument); lsField = GridView1.Rows[liRow].Cells[3].Text; } } 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;public partial class test : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { GridBind(); } } public void GridBind() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("数字", typeof(int))); dt.Columns.Add(new DataColumn("平方", typeof(int))); dt.Columns.Add(new DataColumn("立方", typeof(int))); DataRow dr; for (int count = 1; count <= 10; count++) { dr = dt.NewRow(); dr[0] = count; dr[1] = count * count; dr[2] = count * count * count; dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Display") { GridViewRow isplay_Row = (GridViewRow)((Button)e.CommandSource).Parent.Parent; string select1 = ((Label)isplay_Row.FindControl("数字")).Text;//数字 string select2 = ((Label)isplay_Row.FindControl("平方")).Text;//平方 string select3 = ((Label)isplay_Row.FindControl("立方")).Text;//立方 } }} 我用GridView1_SelectedIndexChanged实现了,但不喜欢这样 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %><!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> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" ShowHeader="False"> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <Columns> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("数字") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="数字" runat="server" Text='<%# Bind("数字") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("平方") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="平方" runat="server" Text='<%# Bind("平方") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("立方") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="立方" runat="server" Text='<%# Bind("立方") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Display" Text="Display" /> </ItemTemplate> </asp:TemplateField> </Columns> <RowStyle BackColor="#E3EAEB" /> <EditRowStyle BackColor="#7C6F57" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </div> </form></body></html> gdstx 的写法也对,不过要在绑定事件中给button加参数 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((Button)e.Row.FindControl("Button1")).CommandArgument = e.Row.RowIndex.ToString(); } } 如何实现到邮箱激活这个功能 突然有个很变态的想法! ExecuteScalar()问题 问个asp.net2.0不刷新的问题 问一个小弱的问题阿:List<T> CS里获取DataList中的值的问题。 100分求《Asp.net入门经典---c#编程篇》 泪求ASP.NET网上考试系统源代码! [高分讨论,UP有分!]关于把excel导入到数据库的讨论! ASP.NET如何配置? 怎样创建文件 应届毕业生给来当程序员一月600都没人要,哭死(福州)。
{
if (e.CommandName == "Display")
{
DataRow isplay_Row = ((DataRow)((Button)e.CommandSource).Parent.Parent);
}
}所在行的按钮的父控件的父控件按钮->单元格->行
怎么取?
{
if (e.CommandName == "Display")
{
DataRow isplay_Row = ((DataRow)((Button)e.CommandSource).Parent.Parent);
isplay_Row["name"].ToString();
} }当前行name字段
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int liRow;
string lsField;
if (e.CommandName == "ShowDetail")
{
liRow= Convert.ToInt32(e.CommandArgument);
lsField = GridView1.Rows[liRow].Cells[3].Text;
}
}
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;public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridBind();
}
} public void GridBind()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("数字", typeof(int)));
dt.Columns.Add(new DataColumn("平方", typeof(int)));
dt.Columns.Add(new DataColumn("立方", typeof(int))); DataRow dr; for (int count = 1; count <= 10; count++)
{
dr = dt.NewRow();
dr[0] = count;
dr[1] = count * count;
dr[2] = count * count * count;
dt.Rows.Add(dr);
} GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Display")
{
GridViewRow isplay_Row = (GridViewRow)((Button)e.CommandSource).Parent.Parent;
string select1 = ((Label)isplay_Row.FindControl("数字")).Text;//数字
string select2 = ((Label)isplay_Row.FindControl("平方")).Text;//平方
string select3 = ((Label)isplay_Row.FindControl("立方")).Text;//立方
} }
}
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" ShowHeader="False">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("数字") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="数字" runat="server" Text='<%# Bind("数字") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("平方") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="平方" runat="server" Text='<%# Bind("平方") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("立方") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="立方" runat="server" Text='<%# Bind("立方") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Display"
Text="Display" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
((Button)e.Row.FindControl("Button1")).CommandArgument = e.Row.RowIndex.ToString();
}
}