Text from a Hyperlink column in a datagrid

G

GaryB

Does anyone have any idea how to get the text out of a hyperlink column in a
web datagrid?

It's a hyperlink column, not a template column so findcontrol does not work
and there is no apparent name to look for.

myGridItem.cells(0).controls(0) shows no text properties or anything that
points to or contains the text.

It seems quite hidden.

G
 
G

GaryB

No, it does not. That's the whole point of the question. When a column is
a hyperlink column, the text is not present in cell(n).text.
G
 
S

Scott Allen

Im my experiment it renders into the second control. You can cast
item.Cells(0).Controls(1) to a HyperLink control to pull the Text and
NavigateUrl properties.

Indexing into the control array always makes me nervous. I don't know
when MS might change how the controls render and that would break some
code. If I need to dig something out I stick to templated columns with
named controls. Know what I mean?
 
S

Steven Cheng[MSFT]

Thanks for Scott's informative suggestion.

Hi Gary,

As Scott has mentioned, since the HyperLink columns will contain a
HyperLink control in it's Cell, we need to retrieve the HyperLink control
first , then access its properties. For example:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in dgData.Items)
{
HyperLink hl = dgi.Cells[0].Controls[0] as HyperLink;
if(hl!=null)
{

Response.Write("<br>Text: " + hl.Text + " Link: " + hl.NavigateUrl);
}
}
}

thanks.


Regards,

Steven Cheng
Microsoft Online Support

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

GaryB

Thats it! I knew it was in cells(3).controls(0) but I was looking for it
directly there. Of course I have to dimension it as a hyperlink and there
it is.
Thanks.

About what Scott said, do you think this kind of thing might change?
Gary
 
S

Steven Cheng[MSFT]

Hi Gary,

Thanks for the followup. Yes, Scott's worry is reasonable since the
internal implemention of the datagrid's Buildin ColumnType may changed in
new versions. So it's better that we use a custom Template Column and put a
HyperLink control which has a specified "id" property so that we can
reference it via "FindControl".
Also, do need to put the following code when accessing sub controls via
index on webform so as not to get "null reference exception.".
HyperLink hl = dgi.Cells[0].Controls[0] as HyperLink;
if(hl!=null)
{

}

Thanks.


Regards,

Steven Cheng
Microsoft Online Support

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top