SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot davejuk's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question User Control within a GridView TemplateField ItemTemplate

    Within my GridView I have a field for the customer name. Whilst I have a name for every record, I don't have an e-mail address for every record. If I have an e-mail address I want to display the name as a link with mailto:, so it can be clicked to open the default mail client. If not, I just want to display the name as plain text.

    Code:
    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Customer" SortExpression="Name">
                        <ItemTemplate><My:CustomerName id="myCustomerName" name='<%# Eval("Name")%>' email='<%# Eval("EMail")%>' runat="server" /></ItemTemplate>
                    </asp:TemplateField>
    So I created the following user control:

    Code:
    <%@ Control Language="VB" ClassName="CustomerName" %>
    
    <script runat="server" language="VB">
    
        Public name As String
        
        Public email As String
        
        Protected Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
            
            ' If we do not have an email address
            If String.IsNullOrEmpty(email) Then
                  
                Dim lit As New Literal
                lit.Text = name
                ph.Controls.Add(lit)
                    
            Else
                    
                Dim link As New HyperLink
                link.Text = name
                link.NavigateUrl = "mailto:" & email & "?subject=WHCF Booking"
                link.ToolTip = "Send an e-mail to " & name
                ph.Controls.Add(link)
                    
            End If
                
        End Sub
        
    </script>
    
    <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
    When I first bind the DataGrid, it works fine. However subsequent binds leave the 'name' and 'email' properties blank and I get an empty field.

    I'm pretty stumped on how to troubleshoot this. Can anyone point me in the right direction? Thanks.

  2. #2
    SitePoint Zealot davejuk's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still haven't got to the bottom of this, if anyone can help? Thanks.

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,649
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You need to check Page.IsPostback . . .

  4. #4
    SitePoint Zealot davejuk's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    You need to check Page.IsPostback . . .
    In my user control?

    With or without the IsPostBack check, I get the same result:

    Code:
    <%@ Control Language="VB" ClassName="CustomerName" %>
    
    <script runat="server" language="VB">
    
        Public name As String
        
        Public email As String
        
        Protected Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
            
            If Not IsPostBack Then
            
                ' If we do not have an email address
                If String.IsNullOrEmpty(email) Then
                  
                    Dim lit As New Literal
                    lit.Text = name
                    ph.Controls.Add(lit)
                    
                Else
                    
                    Dim link As New HyperLink
                    link.Text = name
                    link.NavigateUrl = "mailto:" & email & "?subject=WHCF Booking"
                    link.ToolTip = "Send an e-mail to " & name
                    ph.Controls.Add(link)
                    
                End If
               
            End If
                
        End Sub
        
    </script>
    
    <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •