• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1056
  • Last Modified:

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

0
stuengelman
Asked:
stuengelman
  • 7
  • 6
2 Solutions
 
Ajay SharmaCommented:
Do it in RowDataBound event

ASPA code:
<asp:BoundField HeaderText="View " />


Code Behind:
if (e.Row.RowType == DataControlRowType.DataRow)
{
     e.Row.Cells[0].Text = @"<a href=""javascript:PopNotes(" + e.Row.Cells[0].Text.ToString() + @");"">VIEW</a>";
}


0
 
the_crazedCommented:
or try using a BoundField:

        BoundField HLView =new BoundField();
        HLView.DataField = "RecordID";
        HLView.DataFormatString = "<a href=javascript:PopNotes({0});>View</a>";
        HLView.ItemStyle.Width = 37;
        gvItems.Columns.Add(HLView)
0
 
the_crazedCommented:
oh sorry ajaysharmaapjs, on re-reading I see that this is essentially the same as your solution
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
stuengelmanAuthor Commented:
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

0
 
stuengelmanAuthor Commented:
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

0
 
the_crazedCommented:
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
0
 
the_crazedCommented:
p.s. I am using .net v2.0.50727
0
 
stuengelmanAuthor Commented:
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
0
 
the_crazedCommented:
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?
0
 
stuengelmanAuthor Commented:
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
0
 
the_crazedCommented:
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

0
 
stuengelmanAuthor Commented:
Hi,

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

Stu
0
 
stuengelmanAuthor Commented:
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
0
 
stuengelmanAuthor Commented:
Thanks so much for all your help!!  Stu
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now