当按钮单击行时,在C#Gridview中获取单元格值
我有一个gridviewMain。每当我单击第1行的“关闭链接”按钮时,我都希望得到值aa。单击第2行上的“关闭”,获取bb的值。任何想法当按钮单击行时,在C#Gridview中获取单元格值,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridviewMain。每当我单击第1行的“关闭链接”按钮时,我都希望得到值aa。单击第2行上的“关闭”,获取bb的值。任何想法 A B C aa xx 3 CLOSE bb yy 4 CLOSE cc zz 5 CLOSE aspx 假设您对前三列使用了BoundFields,并且希望处理LinkButton-单击事件(而不是GridView的row命令): 如果您使用的是TemplateFields,并且aa值位于标签中(例如LblValu
A B C
aa xx 3 CLOSE
bb yy 4 CLOSE
cc zz 5 CLOSE
aspx
假设您对前三列使用了
BoundFields
,并且希望处理LinkButton
-单击事件(而不是GridView
的row命令
):
如果您使用的是TemplateFields
,并且aa
值位于标签中(例如LblValue
):
您需要将
CommandName
分配给GridView
标记中的LinkButton
列。从那里,您还需要连接OnRowCommand
事件来处理Close
命令
下面是在GridView
上使用Add
命令的示例:
protected void CloseLinkClicked(Object sender, EventArgs e)
{
var closeLink = (Control) sender;
GridViewRow row = (GridViewRow) closeLink.NamingContainer;
string firstCellText = row.Cells[0].Text; // here we are
}
Markup:
<asp:gridview id="ContactsGridView" datasourceid="ContactsSource" allowpaging="true"
autogeneratecolumns="false" onrowcommand="ContactsGridView_RowCommand" runat="server">
<columns>
<asp:buttonfield buttontype="Link" commandname="Add" text="Add"/>
<asp:boundfield datafield="ContactID" headertext="Contact ID"/>
<asp:boundfield datafield="FirstName" headertext="First Name"/>
<asp:boundfield datafield="LastName" headertext="Last Name"/>
</columns>
</asp:gridview>
Code-Behind:
void ContactsGridView_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 = ContactsGridView.Rows[index];
// Create a new ListItem object for the contact in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
Server.HtmlDecode(row.Cells[3].Text);
// If the contact is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!ContactsListBox.Items.Contains(item))
{
ContactsListBox.Items.Add(item);
}
}
}
标记:
代码隐藏:
void ContactsGridView_row命令(对象发送方,GridViewCommandEventArgs e)
{
//如果GridView控件中使用了多个按钮,请使用
//CommandName属性来确定单击了哪个按钮。
如果(如CommandName==“添加”)
{
//转换存储在CommandArgument中的行索引
//属性设置为整数。
int index=Convert.ToInt32(e.CommandArgument);
//检索包含单击的按钮的行
//由用户从“行”集合中选择。
GridViewRow行=ContactsGridView.Rows[索引];
//为行中的联系人创建新的ListItem对象。
ListItem=新建ListItem();
item.Text=Server.HtmlDecode(row.Cells[2].Text)+“+
Server.HtmlDecode(row.Cells[3].Text);
//如果联系人不在列表框中,请添加列表项
//对象添加到ListBox控件的Items集合。
如果(!ContactsListBox.Items.Contains(item))
{
ContactsListBox.Items.Add(项目);
}
}
}
为此使用数据键。简单得多:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SomeValue, AnotherValue" ... >
你可以这样
<asp:GridView ID="grid" runat="server"
AutoGenerateColumns="false" onrowcommand="grid_RowCommand" >
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="txt" runat="server" Text='<%#Eval("xxx")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnk" CommandArgument=<%# Container.DataItemIndex + 1 %>
CommandName="arg">Click</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
参考号
@Apollo:您不需要处理
行命令
,只需处理链接按钮
的点击
-事件:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SomeValue, AnotherValue" ... >
var rowIndex = 0;
var someValue = GridView1.DataKeys[rowIndex]["SomeValue"] as string;
<asp:GridView ID="grid" runat="server"
AutoGenerateColumns="false" onrowcommand="grid_RowCommand" >
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="txt" runat="server" Text='<%#Eval("xxx")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnk" CommandArgument=<%# Container.DataItemIndex + 1 %>
CommandName="arg">Click</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowindex = int.Parse(e.CommandArgument.ToString());
((TextBox)(grid.Rows[rowindex].FindControl("txtgvunit"))).Text
}