Reputation: 5022
I want to process a field retrieved from the database for a Gridview to check to see if the information in it is a hyperlink. If it is, I want to generate a link from it, otherwise just leave it as raw text. At the moment the field in the Gridview looks like:
<asp:TemplateField HeaderText="Reference">
<EditItemTemplate>
<asp:TextBox ID="txtReference" runat="server" Text='<%# Bind("Reference") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Reference") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I tried modifying what's in the 'Text' attribute of the ItemTemplate but no matter what I put in I get errors on pageload. How can I dynamically modify what's sent to the browser based on the specific info bound in this field?
Thanks a lot!
Upvotes: 3
Views: 816
Reputation: 21864
You can use the GridView.OnRowDataBound method like
protected virtual void yourGV_OnRowDataBound(object sender, gridViewRowEventArgs e)
{
GridViewRow row = e.Row
}
inside that you can access the controls in the row with FindControl
for example.
Upvotes: 1
Reputation: 5022
I fixed this by using both a Label
and a HyperLink
in my TemplateField
, showing the HyperLink
(and hiding the Label
) if the bound text qualifies as a proper URL.
The following ASP.NET sets up the potential HTML to output:
ASP.NET
<asp:TemplateField HeaderText="Reference">
<ItemTemplate>
<asp:Label ID="lblReference" runat="server" Visible='<%# !isTextHyperlink(Eval("Reference")) %>' Text='<%# Bind("Reference") %>'/>
<asp:HyperLink ID="hlReference" runat="server" Visible='<%# isTextHyperlink(Eval("Reference")) %>' Text='<%# Bind("Reference") %>' NavigateUrl='<%# Bind("Reference") %>'/>
</ItemTemplate>
</asp:TemplateField>
I added a function in the pages 'codebehind' like this:
C#
protected bool isTextHyperlink(object refobj)
{
string refstring = refobj.ToString();
try
{
Uri uri = new Uri(refstring);
return Uri.CheckSchemeName(uri.Scheme);
}
catch
{
// not a valid uri (that Uri can construct with)
return false;
}
}
Many thanks to Brissles for the suggestion.
Upvotes: 0
Reputation: 3891
You could use both a Label
and a HyperLink
in your TemplateField
and show the HyperLink
(and hide the Label
) if the bound text qualifies as a proper URL. You can do this using a codebehind function that returns a bool to the Visible
property, something like this:
ASP.NET
<asp:TemplateField HeaderText="Reference">
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Visible='<%# IsTextHyperlink(Eval("Reference")) %>' Text='<%# Bind("Reference") %>'/>
<asp:HyperLink ID="hl" runat="server" Visible='<%# !IsTextHyperlink(Eval("Reference")) %>' NavigateUrl='<%# Bind("Reference") %>'/>
</ItemTemplate>
</asp:TemplateField>
C#
protected bool IsTextHyperlink(object text)
{
bool IsHyperLink = false;
...
// check if text qualifies as hyperlink
...
return IsHyperLink ;
}
Note using type object
for the IsHyperLink function parameter, as Eval() returns an object, just cast it to a String.
You'd want to also format your Text
property of your HyperLink
to something meaningful.
Upvotes: 2