Help with web user control

B

Brad Baker

I'm building a small web application - I started out placing all my code in
one file (config.aspx). As I continue to add code though it was becoming
very unwieldy.

After doing some searching online I found an article which suggested I might
place some of the code into web user controls. I created two new files
(general_info.ascx and store_front.ascx) and placed two chunks of code in
each.

I then registered the controls at the top of config.aspx:
<%@ Register TagPrefix="UU" TagName="general_info" Src="~/general_info.ascx"
%>
<%@ Register TagPrefix="UU" TagName="store_front" Src="~/store_front.ascx"
%>

And I also add the controls to config.aspx:
<UU:general_info id="general_info1" runat="server" />
<UU:store_front id="store_front1" runat="server" />

However I'm getting the following errors:
Element 'general_info' is not a known element. This can occur if there is a
compilation error in the Web site.

Element 'store_front' is not a known element. This can occur if there is a
compilation error in the Web site.

I can't seem to figure out where I went wrong. I'm a bit new at
csharp/asp.net so I'm sure I'm just doing something stupid wrong. Could
anyone point me in the right direction?

Thank You,
Brad Baker
 
A

Alexey Smirnov

I'm building a small web application - I started out placing all my code in
one file (config.aspx). As I continue to add code though it was becoming
very unwieldy.

After doing some searching online I found an article which suggested I might
place some of the code into web user controls. I created two new files
(general_info.ascx and store_front.ascx) and placed two chunks of code in
each.

I then registered the controls at the top of config.aspx:
<%@ Register TagPrefix="UU" TagName="general_info" Src="~/general_info.ascx"
%>
<%@ Register TagPrefix="UU" TagName="store_front" Src="~/store_front.ascx"
%>

And I also add the controls to config.aspx:
<UU:general_info id="general_info1" runat="server" />
<UU:store_front id="store_front1" runat="server" />

However I'm getting the following errors:
Element 'general_info' is not a known element. This can occur if there is a
compilation error in the Web site.

Element 'store_front' is not a known element. This can occur if there is a
compilation error in the Web site.

I can't seem to figure out where I went wrong. I'm a bit new at
csharp/asp.net so I'm sure I'm just doing something stupid wrong. Could
anyone point me in the right direction?

Thank You,
Brad Baker

Was the compilation successful?

The code you've posted here looks correct.
 
B

Brad Baker

Was the compilation successful? <

No - it produces the error mentioned in my original post (Element 'XXX' is
not a known element....)

I found a post which suggested that I needed to group my user controls into
a folder - that seemed to resolve the original problem only to create new
errors. :)

Now my problem seems to be references in my user controls to code still in
the config.aspx file. I'm a bit befuddled as to where to go from here. I'm
still searching the net and reading a few reference books but if anyone has
any ideas I would appreciate any suggestions. (Below are a couple of the
errors from detailed error output)

Thanks Again,
Brad

-----

webcontrols\general_info.ascx(48) : error BC30456:
'general_info_view_linkbutton_click' is not a member of
'ASP.webcontrols_general_info_ascx'.
AddHandler __ctrl.Click, AddressOf
Me.general_info_view_linkbutton_click
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

webcontrols\store_front.ascx(170) : error BC30456:
'store_front_edit_linkbutton_click' is not a member of
'ASP.webcontrols_store_front_ascx'.
AddHandler __ctrl.Click, AddressOf
Me.store_front_edit_linkbutton_click
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

webcontrols\store_front.ascx(179) : error BC30451: Name 'convert_to_html' is
not declared.
dataBindingExpressionBuilderTarget.Text =
System.Convert.ToString(
convert_to_html(Convert.ToString(Eval("additional_domains"))) ,
System.Globalization.CultureInfo.CurrentCulture)

-----
 
A

Alexey Smirnov

Brad Baker said:
No - it produces the error mentioned in my original post (Element 'XXX' is
not a known element....)

Well that was at run-time I guess, but ok, now I see what is the problem.

webcontrols\general_info.ascx(48) : error BC30456:
'general_info_view_linkbutton_click' is not a member of
'ASP.webcontrols_general_info_ascx'.
AddHandler __ctrl.Click, AddressOf
Me.general_info_view_linkbutton_click

It means you don't have a method called general_info_view_linkbutton_click
in general_info.ascx.vb

Either remove the line with AddHandler...., or create a method you refered.

webcontrols\store_front.ascx(170) : error BC30456:
'store_front_edit_linkbutton_click' is not a member of
'ASP.webcontrols_store_front_ascx'.
AddHandler __ctrl.Click, AddressOf
Me.store_front_edit_linkbutton_click

The same problem.

webcontrols\store_front.ascx(179) : error BC30451: Name 'convert_to_html'
is
not declared.
dataBindingExpressionBuilderTarget.Text =
System.Convert.ToString(
convert_to_html(Convert.ToString(Eval("additional_domains"))) ,
System.Globalization.CultureInfo.CurrentCulture)

The same problem, a function with name convert_to_html cannot be found
 
W

Walter Wang [MSFT]

Hi Brad,

Welcome to ASP.NET programming world!

For your specific issue, I think Alexey's suggestion should work. Please
feel free to let us know if you still have trouble to run your code.

Usually I would recommend new ASP.NET developer to following resources when
getting started with ASP.NET:

* Microsoft ASP.NET QuickStarts Tutorial
(http://www.dotnetjunkies.com/quickstart/aspplus/doc/quickstart.aspx)

Although it's for ASP.NET 1.1, but it's from startup and should get you
familiar with ASP.NET. For example, you specific question is about Web User
Control and this is the specific tutorial for it:
http://www.dotnetjunkies.com/quickstart/aspplus/doc/webpagelets.aspx

* ASP.NET 2.0 QuickStart Tutorial
(http://quickstarts.asp.net/QuickStartv20/aspnet/Default.aspx)

This covers some new features available in ASP.NET 2.0.

* INFO: ASP.NET Roadmap (http://support.microsoft.com/kb/305140)

Although it's written for ASP.NET 1.1, but it contains many useful KBs to
various topics in ASP.NET world.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Brad,

Have you solved the issue? Please feel free to let us know if there's
anything unclear. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad Baker

No - I'm still confused.:) As mentioned previously I had all my code in one
file (config.aspx) - this includes functions, and two formview controls
which contain several other controls.

Anyway I pulled the two formview controls out and made each a user control
(general_info.ascx and storefront_info.ascx). Now of course my application
no longer works.

So the part I am confused about is weather I should:

- Pull my functions out of config.aspx and place them inside of my ascx
files - the same code in both files between <script> and </script> tags

- Pull my functions out of config.aspx and place them inside of ascx.cs
files?

And do I need to do anything with my functions? I.E. do they need to be
re-formated as properties and sub routines?

Alternatively I wonder if user controls are really what I want - maybe I
should be looking at web parts instead?

I'm sorry - I'm sure these must seem like silly questions. If you have any
suggestions I would appreciate the assistance.

Thanks
Brad

BTW - In the link you included below
(http://www.dotnetjunkies.com/quickstart/aspplus/doc/webpagelets.aspx) all
of the samples are broken links.
 
W

Walter Wang [MSFT]

Hi Brad,

There're no silly questions, :)

Getting started with ASP.NET can be daunting at first, especially when your
mind is used to the stateful world of client/server applications. That's
why I suggested to start with the tutorial and the roadmap to learn it step
by step, :)

Anyway, let's get back to the issue in this post: based on my
understanding, you was using a WebForm and you later decided to move some
controls into its own Web User Control and use this Web User Control on the
WebForm.

If your FormView will be used by your WebForm, by moving it into a User
Control, it will no longer be referenced directly by your WebForm.
Therefore your code must need have some changes accordingly.

Yes some of your functions, such as the event handlers of the FormView will
be needed to move along with the FormView into the User Control's code
behind class.

I think it's not easy to answer your question easily since the full code
listing is not included here. Anyway, I suggest you first to understand the
Web User Control: the tutorial and documentation should help you. By the
way, I just checked the tutorial URL, it's working on my side, what error
or exception are you seeing when clicking on the samples links?

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad Baker

On this page:
http://www.dotnetjunkies.com/quickstart/aspplus/doc/webpagelets.aspx
All of the "view source" links are broken for me.

Here are two examples of the functions I am talking about:

===========
/*
* Switch the formview mode to view
*/
protected void store_front_view_linkbutton_click(object sender, EventArgs e)
{
store_front_formview.ChangeMode(FormViewMode.ReadOnly);
}

/*
* Takes data stored in the database and formats it for output on the page.
* For instance new lines, commas, and spaces are converted to line breaks,
*/
protected string htmlize(string additional_domains) {

string strContent = additional_domains;

// Try to strip out extraneous characters and add in html line breaks
strContent = strContent.Replace("\n", "<br/>");
strContent = strContent.Replace(", ", "<br/>");
strContent = strContent.Replace(",", "<br/>");
strContent = strContent.Replace(" ", "<br/>");

// Regular Expresson to find URLs and "html-ize" them
Regex urlregex = new Regex(@"(([\w.]+\/?)\S*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
strContent = urlregex.Replace(strContent,"<a href=\"http://$1\"
target=\"_blank\">$1</a>");
return strContent;
}
===========


These functions are referenced in my user controls (as part of a formview):


===========
<td class="right_labels"> Mode:</td>
<td>
<asp:LinkButton ID="ViewLinkButton" runat="server"
OnClick="store_front_view_linkbutton_click">View</asp:LinkButton>
</td>

.... more code ....

<td class="right_labels">URLS:</td>
<td><asp:Label ID="additional_domains" runat="server" Text='<%#
htmlize(Convert.ToString(Eval("additional_domains"))) %>'></asp:Label> </td>
===========

So would I move this function to the codebehind for my user control? If so
don't I have to rewrite my function? I'm hesitant to do that as I have
around 10 functions - some of which are quite complex. Also both of my new
user controls use a lot of the same functions - so there would be quite a
lot of code duplication.

My original goal was to compartmentalize my code into different peices so
that it was more managable. User controls seemed like the right solution but
if I have to re-write all my code I wonder if there isn't a better way of
acheiving the same result. Or if I am better off just leaving all the code
in one file. :-\

Best Regards
Brad Baker
 
B

Brad Baker

Alright - you'll love this.

The source of all my headaches was this: <script runat="server">
Instead of this: <script language="C#" runat="server">

What was I thinking? :) Everything is now working as expected. Thanks for
all your help and patience. I truelly appreciate it.

Warm Regards,
Brad Baker

By the way - This site:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/userctrl/default.aspx
was very useful: I missed it the first time I went through the links.


Brad Baker said:
On this page:
http://www.dotnetjunkies.com/quickstart/aspplus/doc/webpagelets.aspx
All of the "view source" links are broken for me.

Here are two examples of the functions I am talking about:

===========
/*
* Switch the formview mode to view
*/
protected void store_front_view_linkbutton_click(object sender, EventArgs
e) {
store_front_formview.ChangeMode(FormViewMode.ReadOnly);
}

/*
* Takes data stored in the database and formats it for output on the page.
* For instance new lines, commas, and spaces are converted to line breaks,
*/
protected string htmlize(string additional_domains) {

string strContent = additional_domains;

// Try to strip out extraneous characters and add in html line breaks
strContent = strContent.Replace("\n", "<br/>");
strContent = strContent.Replace(", ", "<br/>");
strContent = strContent.Replace(",", "<br/>");
strContent = strContent.Replace(" ", "<br/>");

// Regular Expresson to find URLs and "html-ize" them
Regex urlregex = new Regex(@"(([\w.]+\/?)\S*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
strContent = urlregex.Replace(strContent,"<a href=\"http://$1\"
target=\"_blank\">$1</a>");
return strContent;
}
===========


These functions are referenced in my user controls (as part of a
formview):


===========
<td class="right_labels"> Mode:</td>
<td>
<asp:LinkButton ID="ViewLinkButton" runat="server"
OnClick="store_front_view_linkbutton_click">View</asp:LinkButton>
</td>

... more code ....

<td class="right_labels">URLS:</td>
<td><asp:Label ID="additional_domains" runat="server" Text='<%#
htmlize(Convert.ToString(Eval("additional_domains"))) %>'></asp:Label>
</td>
===========

So would I move this function to the codebehind for my user control? If so
don't I have to rewrite my function? I'm hesitant to do that as I have
around 10 functions - some of which are quite complex. Also both of my new
user controls use a lot of the same functions - so there would be quite a
lot of code duplication.

My original goal was to compartmentalize my code into different peices so
that it was more managable. User controls seemed like the right solution
but if I have to re-write all my code I wonder if there isn't a better way
of acheiving the same result. Or if I am better off just leaving all the
code in one file. :-\

Best Regards
Brad Baker



Walter Wang said:
Hi Brad,

There're no silly questions, :)

Getting started with ASP.NET can be daunting at first, especially when
your
mind is used to the stateful world of client/server applications. That's
why I suggested to start with the tutorial and the roadmap to learn it
step
by step, :)

Anyway, let's get back to the issue in this post: based on my
understanding, you was using a WebForm and you later decided to move some
controls into its own Web User Control and use this Web User Control on
the
WebForm.

If your FormView will be used by your WebForm, by moving it into a User
Control, it will no longer be referenced directly by your WebForm.
Therefore your code must need have some changes accordingly.

Yes some of your functions, such as the event handlers of the FormView
will
be needed to move along with the FormView into the User Control's code
behind class.

I think it's not easy to answer your question easily since the full code
listing is not included here. Anyway, I suggest you first to understand
the
Web User Control: the tutorial and documentation should help you. By the
way, I just checked the tutorial URL, it's working on my side, what error
or exception are you seeing when clicking on the samples links?

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
W

Walter Wang [MSFT]

Hi Brad,

Thank you very much for the follow-up. I'm glad the issue is finally
solved.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top