p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Professionals (http://p2p.wrox.com/forumdisplay.php?f=352)
-   -   How do I put hyperlinks inside gridview... (http://p2p.wrox.com/showthread.php?t=82252)

murshed January 16th, 2011 12:56 AM

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.

Imar January 16th, 2011 05:36 AM

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

murshed January 16th, 2011 10:13 AM

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.

Imar January 16th, 2011 10:38 AM

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

murshed January 16th, 2011 01:44 PM

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).

Imar January 16th, 2011 02:14 PM

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

murshed January 17th, 2011 02:25 PM

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.

Imar January 17th, 2011 03:12 PM

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


All times are GMT -4. The time now is 01:11 PM.

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