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 Professional
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional 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 June 5th, 2004, 05:07 PM
Authorized User
 
Join Date: Apr 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default LoadControl Method and the Reference Page Direct.

Greetings from Spain. First of all regret any inconvenience about my english. I'm having a problem with an user control and the method LoadControl.

I'm creating an user control which holds a DataList web server control. This DataList control, inside de <ItemTemplate> section has a 'LinkButton' control, which is used to redirect the user to another pages in the application. The problem is that if I use the "Register Directive" on my web form, using the Tag Name, Tag Prefix and Src, and I insert the user control between the <form runat="server"> tag, the user control works perfect. But if I try to use the "Reference" Page Directive instead of the mentioned before, and I insert the control using the "LoadControl Method", I get the following error message:

System.Web.HttpException: The control '_ctl0_DataList1__ctl0_LinkButton1' 'LinkButton' type must be inside a tag form with the attribute runat=server established.

Does anybody know what's going wrong?

Thanks in advance and regret any inconvenience about my english



Just reading we'll reach our goals.
__________________
Just reading we\'ll reach our goals.
 
Old June 6th, 2004, 05:23 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you post the relevant bits of code for this? I'm not clear on what you mean by "the 'Reference' Page Directive".
 
Old June 6th, 2004, 03:58 PM
Authorized User
 
Join Date: Apr 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I create an ascx file which holds a DataList. This DataList, holds a LinkButton Control inside the <ItemTemplate> Section.

Now, I want to use this User Control on a Web Form.

'I use the register directive to use the control

<%@ Page Language="VB"%>
<%@ Reference Control="myusercontrol.ascx"%>

Now in the codebehind file of my web form, inside the Page_Load Event I try to Load the control with the following Code:

Dim myControl As New myUserControl
myControl = CType(LoadControl("myusercontrol.ascx", myUserControl)
Me.Controls.Add(myControl)


And I always get the Exception mentioned in my first post. If I remove the server control of the <ItemTemplate> Section, the User Control works perfect. Also, If I use the traditional method employing: <%@ Register TagPrefix... %> the control works.

But trying in the other way (which might have to work) I get the System.Web.HttpException explained.

Thanks in advance for your help

Just reading we'll reach our goals.
 
Old June 7th, 2004, 09:02 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm not sure why you are getting this error. You should be able to use "LoadControl()" without the directive in the markup. You can load a control programmatically without that. So perhaps this problem is coming from someplace else.
 
Old June 8th, 2004, 08:42 AM
Authorized User
 
Join Date: Apr 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know what's going wrong on this. I'm going to detail the process highlighting the most important steps I am following to create the control.

1 Step.- I create the User Control, based on a DataList Control. In the <ItemTemplate> Section of the DataList I insert a LinkButton Server Control.
2 Step.- Once the user control is created, I develope a class called PageBase which inherits from System.Web.UI.Page. In this class, in the Page_Load Event, I use the LoadControl Method.
3 Step.- I create a Web Form based on my PageBase Class.

If I try to load this new Web Form based on my PageBase Class (which contains the User Control developed in Step 1, I get the exception. If I remove the server control from the DataList's <ItemSection> and I use the
<%#DataBinder.Eval...%> Method to bind data retreived from a data source, it works perfect. The problem is, I guess, in the child server control which is inside DataList's <ItemTemplate> Section.

Some help?


Just reading we'll reach our goals.
 
Old June 8th, 2004, 10:06 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Where are you adding the control? Can you paste the code that contains the call to LoadControl()? It sounds like the control is loading, but it's getting added to the wrong place. This doesn't entirely make sense, because the DataList is in the user control and you should get the same error. I'm baffled.
 
Old June 9th, 2004, 08:13 AM
Authorized User
 
Join Date: Apr 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's goes all the code.

'User Control Page
'Menu.ascx
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="menu.ascx.vb" Inherits="ByReckon.Menu" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:DataList id="DataList1" BorderWidth="1px" GridLines="Vertical" CellPadding="3" BackColor="White"
    BorderStyle="None" BorderColor="#999999" runat="server" Font-Size="X-Small" Font-Names="Verdana"
    HorizontalAlign="Left" CellSpacing="3" DataKeyField="SeccionId">
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
    <AlternatingItemStyle HorizontalAlign="Left" VerticalAlign="Middle" BackColor="Gainsboro"></AlternatingItemStyle>
    <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
    <ItemTemplate>
        <asp:LinkButton id="LinkButton1" runat="server" CommandName="select">
            <%#DataBinder.Eval(Container.DataItem, "Seccion")%>
        </asp:LinkButton>
    </ItemTemplate>
    <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
</asp:DataList>

'Menu.ascx.vb file Relevant Code

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Introducir aquí el código de usuario para inicializar la página
        ShowMenu()
    End Sub
    'With the following method I use my Bussiness Layer to retrieve data from
    'a Sql Server DB through a Method of the Data Layer which returns a DataSet
    Private Sub ShowMenu()
        Dim objNegocios As New CapaNegocios
        DataList1.DataSource = objNegocios.Listar_Seccion()
        DataList1.DataBind()
    End Sub

'Code from my PageBase Class
'This is the base class for all my Web Forms

Public Class PaginaBase
    Inherits System.Web.UI.Page
    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ShowMenu()
    End Sub
    Private Sub CargarMenu()
        Dim objMenu As New Menu
        objMenu = (CType(LoadControl("menu.ascx"), Menu))
        Page.Controls.Add(objMenu)
    End Sub

Now each Web Form I create inherits of my PageBase Class.

If remove the ButtonList from the DataList settled in the Menu.ascx file, in the <ItemTemplate> Section IT ALWAYS WORK, even if I just use the <%#DataBinder.Eval...%> Method to show plain text from the SqlServer DataBase in the DataLIst. But if I insert any Web Server Control, it doesn't work.

Greetings, and thanks for your help.

Just reading we'll reach our goals.
 
Old June 9th, 2004, 08:25 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I think the problem is in this line:
 Page.Controls.Add(objMenu)

The form is a control within Page.Controls. If you add a control to Page.Controls it will be after the form (or before) depending on when things happen, but most likely after. So you need to add the menu control to the form control that is contained in the Page.Controls collection.

If the function in the PaginaBase class, loop thru the Page.Controls collection, find the HtmlForm object (If objControl Is HtmlForm...) then add the menu control to that control's Controls collection.
 
Old June 12th, 2004, 09:02 AM
Authorized User
 
Join Date: Apr 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just to let you know that your last answer was correct and now I add the control to the <form> Control. I amend my code in the PaginaBase class to read:

'Code from my PageBase Class
'This is the base class for all my Web Forms

Public Class PaginaBase
    Inherits System.Web.UI.Page
    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ShowMenu()
    End Sub
    Private Sub ShowMenu()
        'I search the <form> control in the page using
        'the FindControl Method
        Dim myform as Control = FindType("form1")
        Dim objMenu As New Menu
        objMenu = (CType(LoadControl("menu.ascx"), Menu))
        'And now I add my User Control to the <form>
        myform.Controls.Add(objMenu)
    End Sub

Now it works, perfect.

Thanks. I appreciatte your patience and attention.

Greetings.


Just reading we'll reach our goals.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Master Page Method: Code-Behind for Content Page kwilliams ASP.NET 2.0 Professional 3 June 2nd, 2008 12:57 PM
Direct selected users to edit page sadiashoiab ASP.NET 1.0 and 1.1 Basics 0 May 3rd, 2007 11:12 AM
Master Page File Reference bandrsolutions ASP.NET 2.0 Basics 2 February 7th, 2007 12:26 PM
reference of prev. page in current page kasanar ASP.NET 1.0 and 1.1 Professional 1 February 13th, 2005 02:49 PM
Page reference in cs file jacob ASP.NET 1.0 and 1.1 Basics 1 July 22nd, 2003 10:37 PM





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