Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem Creating Dynamic Links Within ASP.NET GridView Cells

Posted on 2009-04-06
14
Medium Priority
?
1,052 Views
Last Modified: 2013-11-26
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
Comment
Question by:stuengelman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 18

Accepted Solution

by:
Ajay Sharma earned 1005 total points
ID: 24085570
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
 
LVL 8

Assisted Solution

by:the_crazed
the_crazed earned 495 total points
ID: 24085758
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
 
LVL 8

Expert Comment

by:the_crazed
ID: 24085763
oh sorry ajaysharmaapjs, on re-reading I see that this is essentially the same as your solution
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:stuengelman
ID: 24100096
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
 

Author Comment

by:stuengelman
ID: 24100190
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
 
LVL 8

Expert Comment

by:the_crazed
ID: 24105642
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
 
LVL 8

Expert Comment

by:the_crazed
ID: 24105648
p.s. I am using .net v2.0.50727
0
 

Author Comment

by:stuengelman
ID: 24108386
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
 
LVL 8

Expert Comment

by:the_crazed
ID: 24108862
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
 

Author Comment

by:stuengelman
ID: 24108920
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
 
LVL 8

Expert Comment

by:the_crazed
ID: 24109010
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
 

Author Comment

by:stuengelman
ID: 24109311
Hi,

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

Stu
0
 

Author Comment

by:stuengelman
ID: 24113508
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
 

Author Closing Comment

by:stuengelman
ID: 31567268
Thanks so much for all your help!!  Stu
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question