Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 18th, 2004, 02:39 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to design a HyperLinkColumn in Datagrid?

:)Hi, everybody,

I am trying to design a HyperLinkcolumn in a Datagrid. I saved user information including email address in the database. When I recall these information, I want to display email address as HTML format. Some one told HyperLinkColumn can make it. But I don't know to write the code. Is there anybody can help me out? Thanks a lot.
Ray
 
Old April 19th, 2004, 09:29 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This will show the email with a standard mailto href:

<asp:HyperLinkColumn HeaderText="Click to email"
   DataNavigateUrlField="EmailDBField"
   DataNavigateUrlFormatString="mailto:{0}"
   DataTextField="EmailDBField" />

This will show the words "Click to email" with a standard mailto href:

<asp:HyperLinkColumn HeaderText="Click to email"
   DataNavigateUrlField="EmailDBField"
   DataNavigateUrlFormatString="mailto:{0}"
   Text="Click to email" />


Peter
-------------------------
Work smarter, not harder
 
Old April 19th, 2004, 05:37 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Peter,
It works very well.
But how can I edit this hyperlinkcolumn when I place the datagrid into Edit mode? I know Boundcolumn turns into TextBox.
By the way, do you know where I can find the online library about datagrid category? Thanks a lot.

Here's my code followed.

Public Sub UpdateRecord(ByVal Sender As Object, _
                        ByVal E As DataGridCommandEventArgs)
  Dim EmailTextBox As TextBox = CType(E.Item.Cells(4).Controls(0), TextBox)
  Dim Professor_email As String = EmailTextBox.Text
End Sub

HTML:
      <asp:DataGrid id="dgProfessor" runat="server"
                    AutoGenerateColumns="False"
          <Columns>
          <asp:HyperLinkColumn HeaderText="User Email"
               DataNavigateUrlField="Professor_email"
               DataNavigateUrlFormatString="mailto:{0}"
               DataTextField="Professor_email" />
          <asp:EditCommandColumn ButtonType="LinkButton"
                        UpdateText="Save" CancelText="Cancel" EditText="Edit" />
        </Columns>
      </asp:DataGrid>



 
Old April 20th, 2004, 08:59 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Hyperlink columns are not editable. You'll have to change it to a template column.

You can find everything .net on the MSDN site. Here's the datagrid topic:
http://msdn.microsoft.com/library/de...classtopic.asp
 
Old April 22nd, 2004, 06:12 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks. Peter,
It's very helpful. By the way, do you know how can create a folder in the server under every user's name when I create them in a form submit? Or do you know where this part belongs to?
Thanks for your kindly help.
Ray
 
Old April 23rd, 2004, 11:04 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can use the System.IO namespace to handle any filesystem tasks you wish to use.

When you need to deal with the physical location of a virtual path (i.e. a web address) you need to map the virtual path using Server.MapPath. So in the case of creating a folder for a user, you could do something like this:

Dim strUserName = "JohnDoeUser"
Dim strUserNamePath = Server.MapPath("~/users/" & strUserName)
System.IO.Directory.CreateDirectory(strUserNamePat h)

(The ~ in the MapPath call handles prefixing the virtual directory, if there is one, to the virtual path).

You'll need to make sure that the IIS runtime account has permissions to create files and folders on the server.

Peter
-------------------------
Work smarter, not harder
 
Old April 27th, 2004, 09:13 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Peter,
About editable hyperlink colum, I made a templateColumn as you said. It displays fine as what I want. hyperlink email for displaying and text for editing. But it seems I can't update the record. Error information was: Specified cast is not valid
How do you think I revise the bolded part?
Thanks.
Ray

<code>
Dim EmailTextBox As TextBox = CType(E.Item.Cells(4).Controls(0), TextBox) Dim Professor_email As String = EmailTextBox.Text
</code>
<Html>
<asp:TemplateColumn>
     <HeaderTemplate>
     <a>Eamil Address</a>
     </HeaderTemplate>
     <ItemTemplate>
         <asp:HyperLink id="HyperLink1"
          Text='<%# DataBinder.Eval(Container.DataItem, "Professor_email") %>'
          NavigateUrl='mailto:<%# DataBinder.Eval(Container.DataItem, "Professor_email") %>'
          runat="server"/>
     </ItemTemplate>
     <EditItemTemplate>
         <asp:TextBox
          Text='<%# DataBinder.Eval(Container.DataItem, "Professor_email") %>'
          runat="server"/>
          </EditItemTemplate>
</asp:TemplateColumn>
</html>


 
Old April 27th, 2004, 09:24 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Peter,
I fixed it, but I don't know why.
I changed the controls(0) to controls(1). It works now.
Can you tell me why?
Thanks.
Ray

 
Old April 28th, 2004, 09:11 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I have used controls(0) in the past and it hasn't given me problems.

Everything in the page is considered to be a control. Even static text containing whitespace. The problem might be due to this. Another thing you can try is to use the "FindControl" method of a control (i.e. the cell: Cells(4).FindControl...). This way you can search for the textbox control by name and not risk it breaking of the control index somehow changes.

Peter
-------------------------
Work smarter, not harder
 
Old April 28th, 2004, 04:00 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Peter,

In my TemplateColume, the NavigateUrl part is wrong. the click-hyperlink can't give me the correct data. It gave me the following text. I need the retrieved record, such as mailto:[email protected].
Do you know how I can fix it?
Thanks.
Ray

NavigateUrl='mailto:<%# DataBinder.Eval(Container.DataItem, "Professor_email") %>'







Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing value from HyperLinkColumn to UserControl 62vette ASP.NET 1.0 and 1.1 Basics 0 April 19th, 2006 06:16 AM
HyperLinkColumn amantona Classic ASP Professional 2 April 5th, 2004 08:35 AM
HyperLinkColumn amantona ASP.NET Espanol 0 April 5th, 2004 03:35 AM
<asp:HyperLinkColumn > aadz5 ASP.NET 1.0 and 1.1 Basics 0 October 27th, 2003 04:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.