获取单元格数值在C#中的GridView时,按钮上点击一行 [英] Get Cell Value in C# Gridview when Button clicked on row
本文介绍了获取单元格数值在C#中的GridView时,按钮上点击一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个gridviewMain。每当我点击关闭的LinkButton为行1我想要得到的值AA。点击行关闭2获得BB的价值。任何想法。
A B C
AA XX 3 CLOSE
BB YY 4 CLOSE
CC ZZ 5 CLOSE
ASPX
< ASP:BoundField的数据字段=更远的HeaderText =A
SORTEX pression =远处>
< ItemStyle HorizontalAlign =中心/>
< / ASP:BoundField的>
< ASP:BoundField的数据字段=bfield的HeaderText =BSORTEX pression =cfield>
< ItemStyle HorizontalAlign =中心/>
< / ASP:BoundField的>
< ASP:BoundField的数据字段=cfield的HeaderText =CSORTEX pression =cfield>
< ItemStyle HorizontalAlign =中心/>
< / ASP:BoundField的>
< ASP:的TemplateField ShowHeader =FALSE> <&ItemTemplate中GT;
< ASP:LinkButton的ID =lbClose=服务器的CausesValidation =FALSE的CommandName =CloseClicked文本=关闭>< / ASP:LinkButton的> < / ItemTemplate中> < / ASP:的TemplateField>
解决方案
Asssuming您正在使用绑定列
的前三列,你要处理的的LinkButton
-Click事件(而不是的如 RowCommand
的 GridView控件
)
保护无效CloseLinkClicked(对象发件人,EventArgs的发送)
{
VAR closeLink =(控制)发送;
GridViewRow行=(GridViewRow)closeLink.NamingContainer;
字符串firstCellText = row.Cells [0]。文本; // 我们到了
}
如果您使用的是模板列
和值 AA
是一个标签(如 LblValue
)
标签lblValue =(标签)row.FindControl(LblValue); // lblValue.Text =AA
I have a gridviewMain. Whenever I click the CLOSE linkbutton for row 1 I want to get the value aa. Click Close on row 2 get the value of bb. Any ideas.
A B C
aa xx 3 CLOSE
bb yy 4 CLOSE
cc zz 5 CLOSE
aspx
<asp:BoundField DataField="afield" HeaderText="A"
SortExpression="afield" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="bfield" HeaderText="B" SortExpression="cfield" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="cfield" HeaderText="C" SortExpression="cfield" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" CommandName="CloseClicked" Text="Close"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
解决方案
Asssuming that you are using BoundFields
for the first three columns and you want to handle the LinkButton
-click event (instead of the e.g. RowCommand
of the GridView
):
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
}
If you are using TemplateFields
and the value aa
is in a Label (e.g. LblValue
):
Label lblValue = (Label) row.FindControl("LblValue"); // lblValue.Text = "aa"
这篇关于获取单元格数值在C#中的GridView时,按钮上点击一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文