Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 19th, 2004, 09:52 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default Web User Control Problem

Hi,

I have a Web User Control that contains the following code:

<form id="frmSearch" name="frmSearch" method="post" action="SearchResults.aspx">
<table>
<tr>
<td valign="bottom" align="right" width="94"><IMG src="images/search.gif">
</td>
<td valign="bottom" align="right" width="120">
<input type="text" name="txtSearch" id="txtSearch" size="20">
</td>
<td valign="bottom" align="left">&nbsp; <input type="image" src="images/arrowbutton.gif" id="image1" name="image1" border="0">
&nbsp;
</td>
<td>&nbsp;
</td>
</tr>
</table>
</form>

I expect the program to go to SearchResults.aspx with the contents of txtSearch. But what is does is try to display the aspx page without first executing the Page_load on the cs page (if I put a breakpoint on that line it never hits ) and so I get a Complier error:

Compiler Error Message: CS0117: 'System.Web.UI.Control' does not contain a definition for 'DataItem'

Source Error:

Line 34:
Line 35: <TD vAlign="top">
Line 36: <IMG class=ProductPicture src='Pictures/<%# DataBinder.Eval(Container.DataItem, "visual") %>' border=3>
Line 37: </TD>
Line 38:

What am I doing wrong?


Sandy
__________________
Sandy
Reply With Quote
  #2 (permalink)  
Old February 20th, 2004, 04:44 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yes, that makes sense. As far as the browser is concerned, the code in the UC is just a standard form that submits to a specific page. Therefore, it will never hit the Page_Load of the original page, or the UC.

Instead, remove the <form> tag from the UC. Use ASP.NET controls for the textboxes and submit image (that is, make the UC a true ASP.NET UC). That way, the page will post back to itself when you click the Search image. Page_load will fire for the page and the UC.

Hth,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old February 20th, 2004, 01:49 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.

Having made those changes:

<table>
<tr>
<td valign="bottom" align="right" width="94">
<IMG src="images/search.gif">
</td>
<td valign="bottom" align="right" width="120">
<asp:TextBox id="TextBoxSearch" runat="server" Width="110px"></asp:TextBox>&nbsp;
</td>
<td valign="bottom" align="left">&nbsp;
<asp:ImageButton id="SubmitSearchButton" runat="server" ImageUrl="images/arrowbutton.gif"></asp:ImageButton>&nbsp;
</td>
<td>&nbsp;
</td>
</tr>
</table>

results in this error:

Server Error in '/NoLimitEngin' Application.
----------------------------------------------------------------------

Control 'Header_TextBoxSearch' of type 'TextBox' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'Header_TextBoxSearch' of type 'TextBox' must be placed inside a form tag with runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Which certainly seems odd. Can you help?




Sandy
Reply With Quote
  #4 (permalink)  
Old February 20th, 2004, 01:58 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

It's not that odd. Each ASP.NET control must be placed inside a form with its runat attribute set to server. All controls within the form will participate in the post back event architecture. Controls outside the form are inaccessible. If you try to put an ASP.NET control outside a form, you'll get this error (you'd get the same error if you'd place an ASP.NET Control in an ASPX page outside the form tags.

To fix this, make sure your User Control is placed somewhere between the <form>tags, e.g.:
Code:
<%@ Register stuff here
<html>
...
<body>
<form ID="Form1" Runat="Server">
  YourTagPrefix:YourControl ID="YourControl1" .... />
</form>
...
Does this help?

Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #5 (permalink)  
Old February 20th, 2004, 02:41 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help!

The _Header.ascx Web User Control is used in Catalog.aspx. So I added the form in that page, where _Header is used:

<form id="frmSearch" name="frmSearch" method="post" action="SearchResults.aspx">
<NOLIMITENGIN:HEADER id="Header" runat="server"></NOLIMITENGIN:HEADER>
</form>

and added in the Catalog.aspx.cs page the declaration:
protected System.Web.UI.HtmlControls.HtmlForm frmSearch;

The first line of code in Catalog.aspx is:
<%@ Register TagPrefix="NoLimitEngin" TagName="Header" Src="_Header.ascx" %>

But this results in exactly the same Application error message. Is this not what you meant?



Sandy
Reply With Quote
  #6 (permalink)  
Old February 20th, 2004, 02:49 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Saw I forgot runat="server" in the form and added it. Now there's no Application error. But it still doesn't hit a breakpoint in Page_Load in SearchResults.aspx.cs. Nor does it seem to do anything. It runs and returns to Catalog.aspx.



Sandy
Reply With Quote
  #7 (permalink)  
Old February 20th, 2004, 03:00 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Sandy,

Can you post the code for both the ASPX page and the UC, including the Code Behind.

I've sort of lost track of what goes where and how. You may want to cut out some irrelevant stuff from the aspx page, like HTML tables etc to minimize the size.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #8 (permalink)  
Old February 20th, 2004, 03:28 PM
Authorized User
 
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very Happy To Do So! Here's the UC:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="_Header.ascx.cs" Inherits="NoLimitEngin.__Header" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table height="136" cellSpacing="0" cellPadding="0" background="images/grid_background.gif" border="0" bottomMargin="0" rightMargin="0" leftMargin="0" topMargin="0" width="100%">
. . . . . . . . .
<td valign="bottom" align="right" width="120">
<asp:TextBox id="TextBoxSearch" runat="server" Width="110px"></asp:TextBox>&nbsp;
</td>
<td valign="bottom" align="left">&nbsp;
<asp:ImageButton id="SubmitSearchButton" runat="server" ImageUrl="images/arrowbutton.gif"></asp:ImageButton>&nbsp;
&nbsp;
</td>
. . . . . . . . . . .end

And here's Catalog.aspx:
%@ Register TagPrefix="NoLimitEngin" TagName="Header" Src="_Header.ascx" %>
<%@ Register TagPrefix="NoLimitEngin" TagName="MainNav" Src="_MainNav.ascx" %>
<%@ Page language="c#" Codebehind="Catalog.aspx.cs" AutoEventWireup="false" Inherits="NoLimitEngin.Catalog" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
. . . . . . . . . .
</HEAD>
<body bottomMargin="0" bgColor="#dbdbdd" leftMargin="0" topMargin="0">
<form id="frmSearch" name="frmSearch" method="post" action="SearchResults.aspx" runat="server">
<NOLIMITENGIN:HEADER id="Header" runat="server"></NOLIMITENGIN:HEADER>
</form>
. . . . . . . .end

_Header.aspx.cs has 2 associated declarations and no aplicable code:
protected System.Web.UI.WebControls.TextBox TextBoxSearch;
protected System.Web.UI.WebControls.ImageButton SubmitSearchButton;

Catalog.aspx.cs has 1 associated declarations and no aplicable code:
protected System.Web.UI.HtmlControls.HtmlForm frmSearch;

SearchResults.aspx.cs Page_Load has all the code to process the "txtSearch" string from the _Header UC.

Very greatly appreciate your help!


Sandy
Reply With Quote
  #9 (permalink)  
Old February 20th, 2004, 04:46 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Sandy,

Why do you have a declaration for the HtmlForm? You shouldn't need that. Can't imagine why that could be causing the problem, but who knows. From your code I can't see anything else that could be wrong.

Peter
------------------------------------------------------
Work smarter, not harder.
Reply With Quote
  #10 (permalink)  
Old February 20th, 2004, 04:52 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Well, there may be one thing (a bit late, I was out shopping):

Did you set up a Load handler in the Code Behind for your User Control?

And another, important, thing. Take a look at the form for Catalog.aspx:

<form id="frmSearch" name="frmSearch" method="post" action="SearchResults.aspx" runat="server">

As you can see, you set the action to SearchResults.aspx. However, in ASP.NET pages post back to themselves.

Remove the attribute from the form tag, and the page will post back to itself. If you setup a correct Load handler (this.Load += new System.EventHandler(this.Page_Load); inside the InitializeComponent method), everything should work.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Web User Control Problem jbenson001 ASP.NET 1.x and 2.0 Application Design 12 January 18th, 2005 03:56 PM
Web user control EvilGuyWhoEatsBrains General .NET 1 December 14th, 2004 01:21 PM
Web User Control Best Practices flyin General .NET 4 May 24th, 2004 07:57 AM



All times are GMT -4. The time now is 01:33 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.