Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 January 2nd, 2004, 04:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default Web User Controls

Hello,

I want to create a web user control for displaying error information. I created the control and want to display the information in the label. I added a property Message() to pass the information into. Now, I'm not real familiar with web user controls. I can't seem to access the Server class to get the last exception that occurred, and I can't seem to access the object programmatically (no intellisense is appearing). Now, am I missing something, or how do I accomplish this?

Thanks,

Brian Mains
__________________
Brian
 
Old January 3rd, 2004, 12:14 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

i also in ASP1.1 user control i can't access cache,response calss
i import the namespace required but still can't use many default asp classes

Ahmed Ali
Software Developer
 
Old January 3rd, 2004, 03:19 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Brian,

Can you elaborate on your problem? Paste some code? You should be able to access the server object from a user control, so I'm not quite sure where to start looking for the problem.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 4th, 2004, 05:20 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

sorry the problem happened to me when making a module
for example when i need to use session in one public function what i have to import???

Ahmed Ali
Software Developer
 
Old January 4th, 2004, 08:22 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If you write code in a class file that is not a descendant of either a page or user control, you won't have access to an instance of the ASP.net objects like session, application, cache, request and response that are automatically instantiated with a page instance. You need to access them by referencing the current context of the HttpContext class.

System.Web.HttpContext.Current.Application
System.Web.HttpContext.Current.Request
System.Web.HttpContext.Current.Response
System.Web.HttpContext.Current.Server
System.Web.HttpContext.Current.Session

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 5th, 2004, 03:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Here is the Web control HTML:

<%@ Control Language="vb" AutoEventWireup="false" Codebehind="Error.ascx.vb" Inherits="Project1.ErrorControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<P>
    <TABLE id="tblError" cellSpacing="0" cellPadding="0" border="0">
        <TR>
            <TD class="PageTitle" align="center">An Error Has Occurred</TD>
        </TR>
        <TR>
            <TD><BR>
            </TD>
        </TR>
        <TR>
            <TD>An error has occurred in the application.&nbsp; The following information
                contains the error that has occurred:</TD>
        </TR>
        <TR>
            <TD><BR>
            </TD>
        </TR>
        <TR>
            <TD>
                <asp:Label id="lblMessage" runat="server" CssClass="Error"></asp:Label></TD>
        </TR>
    </TABLE>
</P>

Here is the code behind page:

Public Class ErrorControl
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private m_strMessage As String

    Public Property Message() As String
        Get
            Return m_strMessage
        End Get
        Set(ByVal Value As String)
            m_strMessage = Value
        End Set
    End Property
End Class

Thanks.
 
Old January 5th, 2004, 04:45 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You just need to use your public property to access the protected label:

    Public Property Message() As String
        Get
            Return lblMessage.Text
        End Get
        Set(ByVal Value As String)
            lblMessage.Text = Value
        End Set
    End Property

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 6th, 2004, 10:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

How do I fill the Message() property dynamically though?

Brian
 
Old January 6th, 2004, 10:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

In the consumer of the control (the page) you call that property just like a property on any other object.

ucErrorControl.Message = "There was an error."

How is this control instanciated on the page? Are you including it on every page? Adding it into a controls collection at runtime?

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 6th, 2004, 01:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I dragged and dropped it onto the form (I have Visual Studio .NET). I was confused because no intellisense was popping up for the control.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Form and User Controls CSharpSeeker C# 2005 1 August 22nd, 2007 03:15 PM
Web parts and User controls capitapicard BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 0 June 26th, 2006 04:11 PM
When to employ web services vs. user controls? VictorVictor BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 March 9th, 2006 06:53 PM
User controls not showing on web page pdr ASP.NET 1.0 and 1.1 Basics 0 August 10th, 2005 09:02 AM
Accessing Web User Controls from code aquaboltar ASP.NET 2.0 Basics 0 October 26th, 2004 04:04 PM





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