Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 16th, 2011, 12:56 AM
Authorized User
 
Join Date: Feb 2008
Location: london, , United Kingdom.
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
Unhappy How do I put hyperlinks inside gridview...

Hi,

I'm trying to find a way to insert hyperlinks within Gridview and Listview, with their value coming from one column in the database and their titles coming from another.

In classic asp I might have done something like ...<a href="<%=urlValue%> &" title='"<%=urlTitle%>"'>" & <%=urlTitle%> & "</a>"...
but I can't find a way to do this (or something similar) within controls.


I'm working in C#, if that helps.
  #2 (permalink)  
Old January 16th, 2011, 05:36 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

You have a few options. One is to do it visually using the Edit Columns dialog of the GridView. In the Available Fields you can choose a HyperLinkField and then use properties such as DataNavigateUrlFields, DataNavigateUrlFormatString, and DataTextField to format the output. You should end up with something like this:

Code:
 
<asp:GridView ....>
  <Columns>
    ...
    <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="Default.aspx?Id={0}" DataTextField="Name" HeaderText="View" />
    ...
  </Columns>
</asp:GridView>
Alternatively, you can convert a column into a TemplateField (again using the Edit Columns dialog), and manually build up a HyperLink like this:

Code:
 
<asp:GridView ....>
  <Columns>
    ...
    <asp:TemplateField HeaderText="View">
      <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# string.Format("Default.aspx?Id={0}", Eval("Id")) %>' Text='<%# Eval("Name") %>' />
      </ItemTemplate>
    </asp:TemplateField>
    ...
  </Columns>
</asp:GridView>
The latter is closer to what you're used to doing in classic ASP. Additionally, it gives you more control and flexibility over the output.

In both cases, Id and Name refer to columns from the database.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
murshed (January 16th, 2011)
  #3 (permalink)  
Old January 16th, 2011, 10:13 AM
Authorized User
 
Join Date: Feb 2008
Location: london, , United Kingdom.
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for the quick response, your first solution works a treat! Perfect :)

Out of curiosity (and the fact that I miss doing some scripting), I'm trying to implement the second solution as I write. I think I must be doing something wrong on the edit columns > template field step, as my knowledge of visual studio controls is scant.

So far, I have the first method working (where I select a hyperlinkfield and bind its data and text properties to my respective columns) but, when I tried to create a template field in the edit columns view, I got an empty properties dialog in that window, I could neither write code into it nor select another field to drop into it or something.


Thank you again, feels a relief to know that there is code behind the mass of windows and forms and buttons in Visual Studio.
  #4 (permalink)  
Old January 16th, 2011, 10:38 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
but, when I tried to create a template field in the edit columns view, I got an empty properties dialog in that window,
Yes, correct. When you create a TemplateField, you're on your own ;-) To create the code I posted, close the Edit Columns dialog and switch the page from Design View into Markup View. Then you can manually modify the contents of the TemplateField. Additionally, you can modify its content using the Design View by choosing Edit Templates on the GridView's smart tasks panel.

I talk about this and a lot more in detail in my book Beginning ASP.NET 3.5: http://www.wrox.com/WileyCDA/WroxTit...47018759X.html There's also an updated versio for ASP.NET available.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old January 16th, 2011, 01:44 PM
Authorized User
 
Join Date: Feb 2008
Location: london, , United Kingdom.
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
Default

ah! I didn't realise that, didn't even think that I had changed anything in the control, as a result of my actions. It really does feel like I'm "hacking the application", the way Visual Studio makes me work. Very odd feeling.

You know, three years ago next month, I posted I'd be going down to Borders and buying your book (I just checked out some of my old posts) in order to learn about Visual Studio. In the end I gave up and decided to stick to classic asp and also learn Linux/LAMP, because I couldn't figure out how to "break the controls open" in order to get at labels, texts, datasources etc etc... I just didn't think Visual Studio was for me.

Now I'm back at it, have to use it to create my 'proof of concept' site and, I think I'm actually beginning not to hate it (mostly thanks to reading your and your fellow authors' posts here, much better than following a video like a monkey, as a few people advised me to do).

Is there an intermediate level book that you would recommend, I tend to learn the beginner's stuff from messing with the code myself but where I come unstuck is in things like the above, which you're only going to know to expect if you understand the tool well (which I obviously do not).

I'm comfortable with setting up multi-page queries, session variables, "hacking" the login/roles controls in order to generate roles-specific content etc... I'm quite ignorant on the security implications of most of what I do, I have no real understanding of how the controls are created (and, as such, what the proper way is to extend them and to integrate them together for complex solutions).

Last edited by murshed; January 16th, 2011 at 02:02 PM.. Reason: poor english.
  #6 (permalink)  
Old January 16th, 2011, 02:14 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I think you still may want to check out my books. Some people have called it too advanced for true beginners (although other beginnners have said it's just fine). It does go into detail with many important ASP.NET concepts and goes a lot further than simply showing you how to use drag and drop.

Check out the reviews on Amazon.com and take a look at the table of contents for the book here at Wrox.com to see if the book's for you. I think it is, but you have to make up your own mind. If you do pick my book, I recommend getting the latest, ASP.NET 4 version.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #7 (permalink)  
Old January 17th, 2011, 02:25 PM
Authorized User
 
Join Date: Feb 2008
Location: london, , United Kingdom.
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
Default

You're right, I thought beginners ... would be mostly concerned with drag-n-drop, but having gone through the t-o-c and read some of the sections at Amazon, I added the 3.5 version to my basket :)

I also need to get something on mobile web applications, since I might have to create a "front-end", as part of the demo. Any books you'd recommend?

I don't have the latest Visual Studio and I'm just now getting comfortable with VWD 2008 so I'll get 4.0 when/if I move on to VWD 2010/MVC.
  #8 (permalink)  
Old January 17th, 2011, 03:12 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
I also need to get something on mobile web applications, since I might have to create a "front-end", as part of the demo. Any books you'd recommend?
Nope, sorry, nothing comes to mind.

Quote:
I don't have the latest Visual Studio and I'm just now getting comfortable with VWD 2008
In case you didn't know and care about upgrading: VWD 2010 Express is free....

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Two DropDownList Inside a Gridview ilayaraja ASP.NET 3.5 Basics 3 December 5th, 2008 06:26 PM
TextBox inside GridView michurin ASP.NET 2.0 Basics 2 January 13th, 2007 12:51 PM
Hyperlinks in GridView Columns monika.oberoi ASP.NET 2.0 Professional 2 December 22nd, 2006 04:21 AM
DropDownList inside a GridView aidoco ASP.NET 2.0 Basics 0 October 13th, 2006 04:24 AM
How to put a control inside a table cell? zach2004 ASP.NET 1.x and 2.0 Application Design 7 March 8th, 2004 03:28 PM



All times are GMT -4. The time now is 02:18 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.