HyperLinkField and FindControl

D

David Thielen

Hi;

I have a HyperLinkField inside my GridView and it works great. I use
DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="Datasource.aspx?ID={0}" so I really want this
control.

However, I need to access this control in the RowDataBound in the
code-behind. There is no ID="name" for HyperLinkField so how do I get this
control in the code behind for each row?

And if I can't - then what control should I use to link to another page
passing an ID=123 in the url?
 
S

Steven Cheng[MSFT]

Hi Dave,

Thank you for posting.

As for the Hyperlink field, since the hyperlink control within it is
autogenerated, so we can not reference it through ID, and one possible
approach is reference the control through control index, however, this is
dangerous and not recommended(you need to view the generated source(or use
some test code in RowDataBound event) to check the hyperlink control's
actual index).

For such scenario, I still think using TemplateField is the best choice.
And since VS IDE can help use convert a built-in column/field to a template
field, you can first define the hyperlink field (and make it work
correctly), then convert it to template field through IDE(in the
EditColumns wizard). Here is a sample TemplateField (in GridView) converted
from a HyperlinkField:

===========================
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("CategoryID", "DataSourcePage.aspx?id={0}") %>'
Text='<%# Eval("CategoryName", "[{0}]")
%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
============================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top