Link to home
Start Free TrialLog in
Avatar of stuengelman
stuengelmanFlag for United States of America

asked on

Problem Creating Dynamic Links Within ASP.NET GridView Cells

Hello,

I am having difficulty getting an MS GridView control to properly display dynamic links within rendered cells at the browser (MSIE).

I am using Visual Studio 2008 in connection with .NET Framework 3.5 to display a GridView control on a web page.  AutoGenerateColumns is set to false in the code-behind page, and the GridView control is populated dynamically at the back end via code to create each of the columns, and a databind to a dataset object is then performed.

The front end script for the GridView control is shown in the Code snippet section (please note that this is encapsulated within an ASP UpdatePanel for AJAX functionality).  I've also included my back end script for creating the hyperlink cell contents in the Code snippet section.

When the code is compiled and rendered in MSIE, all I get for the resultant table cell is <a>View</a>, whereas what I really want is <a href="javascript:PopNotes(xxx);">View</a>, where xxx would be the operant value of the RecordID of the dataset table being utilized.

Thanks, Stu Engelman




HTML For GridView Control
----------------------------------
<asp:GridView ID="gvItems" runat="server" AllowPaging="True" CellPadding="3" 
    PageSize="15" ShowHeader="False" 
    BorderColor="Black" Font-Names="Trebuchet MS">
    <PagerSettings Position="Top" />
</asp:GridView>
 
Code-Behind Logic For Creation of Hyperlink Cell
-----------------------------------------------------------
Dim HLView As New HyperLinkField
HLView.Text = "View"
Dim hlfield As String() = {"RecordID"}
HLView.DataNavigateUrlFields = hlfield
HLView.DataNavigateUrlFormatString = "javascript:PopNotes({0});"
HLView.ItemStyle.Width = 37
gvItems.Columns.Add(HLView)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ajay Sharma
Ajay Sharma
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh sorry ajaysharmaapjs, on re-reading I see that this is essentially the same as your solution
Avatar of stuengelman

ASKER

Hello,

Please see the attached code snippet; it reflects the suggested replacement to the ButtonField I had earlier.

At the browser level, instead of displaying "View" as a hyperlink, it actually displays plain text equal to: <a href="javascript:PopNotes(xxx);">View</a>, where xxx is the value of the RecordID field.

I think the problem is that the actual HTML source rendered contains the value: <td style="width:37px;"><a href='javascript:PopNotes(1);'>View</a></td>

Is there any way to force ASP.NET to download "<" as a literal character, rather than "<"?  Same question for ">".

Thanks, Stu
Dim HLView As New BoundField()
HLView.ReadOnly = True
HLView.DataField = "RecordID"
HLView.DataFormatString = "<a href='javascript:PopNotes({0});'>View</a>"
HLView.ItemStyle.Width = 37
gvItems.Columns.Add(HLView)

Open in new window

Hello again,

My above comments may be confusing because the EE thread system converted some of my characters.  Please see the code snippet below for a clearer picture of what is happening.

Thanks, Stu
At the browser level, instead of displaying "View" as a hyperlink, it actually displays plain text equal to: <a href="javascript:PopNotes(xxx);">View</a>, where xxx is the value of the RecordID field.
 
I think the problem is that the actual HTML source rendered contains the value: <td style="width:37px;">&lt;a href='javascript:PopNotes(xxx);'&gt;View&lt;/a&gt;</td>
 
Is there any way to force ASP.NET to download "<" as a literal character, rather than "&lt;"?  Same question for ">".

Open in new window

my friend, that is bizarre; When I use exactly the same code as you, it works perfectly.
please attatch your full codebehind file (you may have to rename to .txt) - that should stop expert sexchange converting chars, and give the version of .net you are using
 
here is my working codebehind file

Default.aspx.vb.txt
p.s. I am using .net v2.0.50727
Hi Crazed,

The relevant code is in the code snippet section.

I've also attached a zip archive containing the entire ASPX and ASPX.VB files.

I am using .NET 3.5.

Thanks, Stu
Code-Behind Logic For Creation of Hyperlink Cell
------------------------------------------------
Dim HLView As New BoundField()
HLView.ReadOnly = True
HLView.DataField = "RecordID"
HLView.DataFormatString = "<a href='javascript:PopNotes({0});'>View</a>"
HLView.ItemStyle.Width = 37
gvItems.Columns.Add(HLView)
 
HTML For GridView Control
-------------------------
<asp:GridView ID="gvItems" runat="server" AllowPaging="True" CellPadding="3" PageSize="15" ShowHeader="False" 
BorderColor="Black" Font-Names="Trebuchet MS">
<PagerSettings Position="Top" />
</asp:GridView>

Open in new window

Items.zip
Hi Stu,
     I can see no problem with what you've done whatsoever.
     Sadly, I don't have your version of .net to test it; but I would hope (but not be sure) that M$ haven't broken this between v2 and v3.5.
     pehaps its because you've got it inside an <asp:UpdatePanel>.
     Just for a test, please could you try moving the GridView out and see if it behaves any better?
Hi Crazed,

I was thinking the same thing about the AJAX issue.  Unfortunately, my client needs this control to be subject to partial page rendering, and if this is the issue, then I may have an unsolvable problem.  AJAX in .NET 3.5 is definitely a different animal than for earlier versions.  It used to be a separately installable toolkit, but now is built directly into the BCL.  I guess the question is: did MS introduce some bugs when porting the delivery mechanism?

Stu
I wish it was less likely that they did intrduce bugs!

I wonder what happens if you do this?
<asp:GridView ID="gvItems" runat="server" AllowPaging="True" CellPadding="3" PageSize="15" ShowHeader="False" 
BorderColor="Black" Font-Names="Trebuchet MS">
<PagerSettings Position="Top" />
<Columns>
    <asp:BoundField  DataField="RecordID" DataFormatString="&lt;a href='javascript:PopNotes({0});'&gt;View&lt;/a&gt;"></asp:BoundField>
</Columns>
 
</asp:GridView>

Open in new window

Hi,

If I used that approach, would I have to port all column definitiions from the back end to the ASPX page?

Stu
Hello,

At long last, I found the solution.  You need to set the HtmlEncode property of the BoundField to false before rendering to the browser, in my case "HLView.HtmlEncode = False".  This prevents the "less than" character from being converted into the HTML "codec" so to speak, allowing the browser to interpret the anchor tag as a real tag, and not just plain text.

As far as points go, ajaysharmaapjs got me half way there by having me convert the ButtonField to a BoundField, which was necessary for the final solution to work.  Crazed, you've been fantastic, but I have to award most of the points to ajaysharmaapjs since he suggested the conversion first, although your comments in this regard were also helpful as they moved all the column logic to the server side, where it actually resides.

So, since your joint solution got me half way there, I'm going to award a "B", and give 2/3 of the points to ajaysharmaapjs, and 1/3 to Crazed.  I hope everyone considers this a fair decision; just let me know if there is any disagreement.

Thanks again to both of you,  Stu
Thanks so much for all your help!!  Stu