Using Src Rather Than CodeBehind in Web User Controls

  • Thread starter Thread starter Nehal Shah
  • Start date Start date
N

Nehal Shah

I've read that in the Page Directive of an aspx page, changing the
CodeBehind attribute to Src saves you from having to compile the page before
refreshing. This is preferable in a large development environment where
rather than having each developer running a local copy of a website, they
can make changes simultaneously to a central web server without stepping on
each other's toes as far as compilation is concerned.

ANYWAY, this is fine for aspx files, but when you try this with ascx files
(user controls), I get the following error:

The base class includes the field 'uc', but its type
(TestWeb.WebUserControl1) is not compatible with the type of control
(ASP.WebUserControl1_ascx).

It seems to be having a problem with the declaration of the user control on
the CodeBehind of the containing aspx page. When I remove it, it works
fine. But then I can't really do anything with it programmatically.

Does anyone know how to make it work so that I can declare and manipulate
the user control in the CodeBehind under these circumstances?

HERE'S THE ASPX PAGE:
<%@ Page Language="vb" AutoEventWireup="false" Src="WebForm1.aspx.vb"
Inherits="WebForm1"%>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>
<body>
<form id="Form1" method="post" runat="server">
<uc1:WebUserControl1 id=uc runat="server"
txt="source"></uc1:WebUserControl1>
</form>
</body>
</html>

I ADDED THE FOLLOWING DECLARATION TO THE "Web Form Designer Generated Code"
REGION OF THE ASPX CODEBEHIND:
Protected WithEvents uc As WebUserControl1

HERE'S THE USER CONTROL:
<%@ Control Language="vb" AutoEventWireup="false"
Src="WebUserControl1.ascx.vb" Inherits="WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<span>Santa Clause doesn't exist.</span>


Thanks,
Nehal
 
Hi Nehal,

Thank you for posting to the MSDN newsgroups.

I am interested in this issue and researching it now. More time is needed.
I will update you as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Nehal,

I am sorry if there is any misunderstanding.

As I understand, what your concern is that when you use the following
syntax to register a user control inside a consumer aspx page, an error
occurred.

<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>

However, if you change the property name of "Src" to "codebehind", it works
ok. However with the "codebehind" syntax, you cannot declare an instance of
the user control in code-behind of the consuming aspx, right? Please
correct me if I am wrong.

I created a user control and tried to drag it onto an aspx page. It was
noticed that by default, the "src" was used.

<%@ Register TagPrefix="uc1" TagName="calendar" Src="calendar.ascx" %>

and I also declared a variable to point to the user control

Protected uc As calendar ' calendar is the class of my user control

And in the Init event of the consuming aspx page, I used the following code
to bind the uc variable to the user control on this aspx.

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()

uc = Me.FindControl("Calendar1")
AddHandler uc.Load, uc_Load()
End Sub

Private Function uc_Load()

End Function

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Nehal,

I have done a lot of research regarding this issue. Based on my research
and experience, this issue is by design.

The reason is that after changing all the "CodeBehind=" to "Src=", I got
the following error when I closed and reopened the solution.

"...The @Page or @Control directive contains a src= attribute, which is not
supported in Visual Studio..."

I am using VS.NET 2003.

Please let me know if it makes sense.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Yes, Visual Studio.NET doesn't like it, but it still works as far as
aspnet_wp is concerned. CodeBehind is an attribute used only by the IDE to
build the solution. I'm using VS.NET too, but I just ignore the error
message. Maybe it'll be fixed in Whidbey...
 
Hi Nehal,

I cannot guarantee if this issue will be changed in Windbey. It is
determined by the product team. Thank you for your understanding.

You can send the feedback of our products to Microsoft directly. Please
refer to the following URL.

Contact Microsoft
http://support.microsoft.com/default.aspx?pr=cntactms
"...
Microsoft Wish

Make a product suggestion or feature request. Product enhancement
suggestions can include:
Improvements on existing products
Suggestions for additional features
Ways to make our products easier to use
..."

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top