I need to know how to access a ascx control (table data) in order to format the data, for the body text of an email message.

Posted on 2009-12-21
Last Modified: 2013-11-27
Need to know How to obtain the data from a control (ascx) and build a formatted txtfile.text for an email body text

register.aspx page --> the control tag refence
 <events:EventDetails id="ctlEvent" runat="server" ShowDetails="false" ShowDiscount="true" />

need to know how to access the control data to build a formatted string

EventDetails.ascx file
<%@ Control Language="vb" Codebehind="EventDetails.ascx.vb" Inherits="i2Integration.Modules.EventReg.CustomControls.EventDetails" AutoEventWireup="false" Explicit="true" %>
<font class="SubHead">Event Information:</font>
<table id="tblForm" runat="server" cellpadding="3" summary="Event details layout table.">
      <tr id="rowID" runat="server" visible="false" EnableViewState="False">
            <td class="SubHead" vAlign="top">ID:</td>
            <td><asp:Label ID="lblEventID" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowStatus" runat="server" EnableViewState="False">
            <td class="SubHead" vAlign="top">Status:</td>
            <td><asp:Label ID="lblStatus" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
            <td class="SubHead" vAlign="top">Title:</td>
            <td><asp:Label ID="lblTitle" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
            <td class="SubHead" vAlign="top">Date:</td>
            <td><asp:Label ID="lblDate" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
            <td class="SubHead" vAlign="top">Summary:</td>
            <td><asp:Label ID="lblSummary" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowDetails" runat="server" EnableViewState="False">
            <td class="SubHead" vAlign="top">Details:</td>
            <td><asp:Label ID="lblDetails" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowViewStart" runat="server" EnableViewState="False">
            <td class="SubHead" vAlign="top">View Start:</td>
            <td><asp:Label ID="lblViewStart" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowViewEnd" runat="server" EnableViewState="False">
            <td class="SubHead" vAlign="top">View End:</td>
            <td><asp:Label ID="lblViewEnd" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowRegStart" runat="server" EnableViewState="False">
            <td class="SubHead" vAlign="top">Registration Begins:</td>
            <td><asp:Label ID="lblRegStart" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowRegEnd" runat="server" EnableViewState="False">
            <td class="SubHead" vAlign="top">Registration Ends:</td>
            <td><asp:Label ID="lblRegEnd" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowDiscount" runat="server" EnableViewState="False">
            <td class="SubHead" vAlign="top">Discount:</td>
            <td><asp:Label ID="lblDiscount" Runat="server" CssClass="Normal" EnableViewState="False" /></td>
      <tr id="rowExport" runat="server" enableviewstate="false">
      <td class="SubHead" valign="top">Export to Outlook:</td>
      <td><asp:LinkButton ID="lnkExportToOutlook" runat="server" Text="Add to calendar"></asp:LinkButton></td>
Question by:westdh
    LVL 15

    Accepted Solution

    In the code behind of your control, declare a public property that returns the text of the control you want to get, then in your page (aspx) get that property.

    Author Comment

    I am trying to access it from my aspx codebehind page:
    this does not work 'MyUserControl" is in error

     Dim u As MyUserControl = TryCast(FindControl("EventDetails"), MyUserControl)
               Dim b As TextBox = TryCast(u.FindControl("lblTitle"), TextBox)
               If b IsNot Nothing Then
                   Response.Write("Found lblTitle + b + ")
               End If
    LVL 15

    Expert Comment

    You must add a "Reference" directive in your page pointing to your control through virtual path attribute
    LVL 2

    Expert Comment

    See problem is - you are accessing usercontrol with wrong identifier.
    Your control's id is in page: ctlEvent

    so try this

    Dim u As MyUserControl = TryCast(FindControl("ctlEvent"), MyUserControl)
    Once you got the control, you can access further controls inside that using findcontrol again.

    Let me know if it works or you need more info.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 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

    14 Experts available now in Live!

    Get 1:1 Help Now