获取网格视图中所选记录的超链接字段值 [英] Get the hyperlink field value of the selected record in gridview
问题描述
我的Gridview的列上有一个超链接字段,每行都有一个复选框.我们可以通过选中该复选框来选择任何记录.
My Gridview has a hyperlink field as on of its columns and for each row there are check boxes.We can select any record by selected the check box.
问题是:我无法获取超链接字段记录.
The problem is: I am not being able to get the hyperlink field record.
执行此操作的代码是:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[1].FindControl("myCheckBox");
if (cb != null && cb.Checked)
{
String Retailer = GridView1.Rows[i].Cells[0].Text.ToString();
}
}
在Cells [0]处,我的超链接字段存在.此代码可以访问所有其他字段.当我将代码更改为GridView1.Rows [i] .Cells [2] .Text.ToString()时,我可以在Cell [2]位置获得Column值,该位置不是超链接字段.
At Cells[0] my hyperlink field is present.All the other fields can be accesss by this code.When I change the code to GridView1.Rows[i].Cells[2].Text.ToString() , I can get the Column value at Cell[2] position which is not hyperlink field.
为了获得超链接字段的值,我应该进行哪些更改. 请帮忙!
What changes am I suppose to make in order to get the hyperlink filed value. Please help!!
Gridview的代码是:
The code for Gridview is:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Col1"
Width="100%"
BackColor="#F8ECE0"
BorderColor="#8B4513"
ShowFooter="false"
CellPadding="3"
CellSpacing="0"
Font-Name="Comic Sans"
Font-Size="11pt"
HeaderStyle-BackColor="#FFA500"
EnableViewState="false"
AutoPostBack="true">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="Col1" DataNavigateUrlFormatString="Col1.aspx?Name={0}" DataTextField="Col1" HeaderText="Col1" />
<asp:BoundField DataField="Col2" HeaderText="Col2" SortExpression="Col2" />
<asp:BoundField DataField="Col3" HeaderText="Col3" SortExpression="Col3" />
<asp:BoundField DataField="Col4" HeaderText="Col4" SortExpression="Col4" />
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" HeaderText="Check to Cancel" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
推荐答案
您需要先获取控件类型,然后再获取Text
属性,如下所示:
You need to get to the control type first and then the Text
property, like this:
String Retailer = ((HyperLink)GridView1.Rows[i].Cells[0].Controls[0]).Text;
这篇关于获取网格视图中所选记录的超链接字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!