Solved

Problem Creating Dynamic Links Within ASP.NET GridView Cells

Posted on 2009-04-06
14
1,043 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
  • 7
  • 6
14 Comments
 
LVL 18

Accepted Solution

by:
Ajay Sharma earned 335 total points
Comment Utility
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 165 total points
Comment Utility
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
Comment Utility
oh sorry ajaysharmaapjs, on re-reading I see that this is essentially the same as your solution
0
 

Author Comment

by:stuengelman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
p.s. I am using .net v2.0.50727
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:stuengelman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks so much for all your help!!  Stu
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now