ASP.NET Compile behavior changes depending on page content? - asp.net

I've been struggling with some issues relating to referencing child controls within a FormView. Another developer wrote an ASPX page that is compiling and working and in that code, he references child controls within a FormView directly as properties of the page object. The page is part of an ASP.NET Web SITE Project (as opposed to a Web Application Project). We decided to convert the project to the Web Application Project model and noticed that these property references now don't compile. The code behind file does not generate the controls within the form view.
While researching that issue (I had a separate post here regarding those problems), I came across something baffling. From all the posts I've read, you should always need to reference child controls within a FormView's template using FindControl -- i.e. it is supposedly not possible to do through a simple generated property regardless of whether you're in the Web Site Project model or the Web Application Project model.
I was puzzled as to how my colleague's code compiled and ran. As I indicated, he is referencing the FormView's contained child controls through simple properties in the page and did not have to resort to FindControl calls. So to get to the bottom of this mystery, I cooked up the shortest example that demonstrates this phenomenon.
What I found was very strange. The code I have here has a ASP:FormView with a few label controls within it's ItemTemplate. One of those labels has the ID MyComment. When the FormView databinds (to the Northwind's Products table), I simply set some text.
using System;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.FormView1.ChangeMode(FormViewMode.ReadOnly);
}
protected void FormView1_DataBound(object sender, EventArgs e) {
MyComment.Text = "Data bound at " + DateTime.Now.ToString();
}
}
This code will not compile because MyComment is not a valid property. Here comes the strange part. If I embed within the FormView's ItemTemplate a TabContainer control from the Ajax Control Toolkit library, the code above does compile and runs correctly.
So the reason my colleague's code is compiling is because of the embedded TabContainer control within the FormView?? Why this should change the behavior of the compiler and the mechanisms by which you can get access to the FormView's child controls is a mystery to me. Incidentally, despite the fact that it compiles cleanly and runs correctly, Intellisense does not see these properties and ReSharper reports them as compile errors (by the red light in the indicator bar).
Here is the markup for the page. Can anyone shed some light on this behavior? Incidentally, I'm not complaining about the fact that ASP.NET creates these properties in this circumstance. (Unfortunately, this happy, but strange, behavior, only seems to apply if the project is a Web Site Project; as a Web Application Project, property accessors don't work within the FormView even with the embedded TabControl).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ProductID" DataSourceID="SqlDataSource1"
OnDataBound="FormView1_DataBound">
<ItemTemplate>
<ajaxToolkit:TabContainer runat="server" ID="TabsItem">
<ajaxToolkit:TabPanel runat="Server" ID="PanelBasicsItem" HeaderText="Basics">
<ContentTemplate>
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
<br />
ProductName:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>' />
<br />
My Comment:
<asp:Label ID="MyComment" runat="server"></asp:Label>
<br />
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName] FROM [Alphabetical list of products]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>

When you converted the Web site to a web application, did you check that the partial classes were correctly generated for your aspx files (e.g. there should be a file called "Default.aspx.cs.designer" in your web application, under the Default.aspx and Default.aspx.cs files).
In a web site, these are generated on the fly by the server as your site runs and is compiled (and so don't exist in your project), while in a web application they are created and managed by Visual Studio - if they don't exist, then the code will potentially fail to compile because the objects haven't been instatiated - what is the actual compiler error are you seeing?
By adding a new control to the page after you've converted the project to a web application, you are forcing VS to create the partial class that was missing.

Related

Pass variable used in aspx page to user control

We are in ASP.Net Webforms. I come from a LAMP Stack mindset...
ASPX Code (part of it)
<asp:Content runat="server" ContentPlaceHolderID="PH_MainContent">
<h3 class="fleft">
<asp:Literal runat="server" ID="li_title" />
...
<Example:userControl runat="server" someVariable="text" otherVariable=<%=li_title.Text%> thirdVariable=<%=Items["sort"].toString()%> />
So, li_title is a literal set in the code behind, I want to reuse it later, passing it to a userControl, where it shall be displayed in a javascript. Items is page.Items. I have also tried this.li_title (which is suggested to me in Visual Studio 2015).
After reading "quite a few" very similar questions, this seem the solution. However obviously I'm missing the point. What would that be..?
You can create a public property for each variable on your user control and set them in aspx. See this for more information

UserControl does not have public property named ContentTemplate

This question has been asked a few dozen times before; but has never been solved.
i have an UpdatePanel
<asp:UpdatePanel ID="UpdatePanelSetupToolbar" runat="server">
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>
But the visual page designer in Visual Studio (2010 (Professional (Windows (7 (Professional (64-bit)))))) gives the error:
Error Creating Control - UpdatePanelSetupToolbar
Type 'System.Web.UI.UserControl' does not have a public property named 'ContentTemplate'.
Edit: pretty colors in pretty screenshots are added for pretty effect
Now, strictly speaking, that is true: UserControl does not have a public property called ContentTemplate.
Fortunately my UpdatePanel is an UpdatePanel, and it does have a public property named 'ContentTemplate'.
So how do i convince Visual Studio that my UpdatePanel is an UpdatePanel?
Important additional notes
The code above doesn't actually fail as is:
<asp:UpdatePanel ID="UpdatePanelSetupToolbar" runat="server">
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>
It only fails when i have content inside the ... ContentTemplate:
<asp:UpdatePanel ID="UpdatePanelSetupToolbar" runat="server">
<ContentTemplate>
<Vista:Toolbar ID="ToolbarSetup" runat="server">
<ContentTemplate>
<asp:LinkButton ID="bbSetupDays" ToolTip="Specify how many allocations will be available on these selected days"
OnClick="bbSetupDays_Click"
runat="server">Setup Selected Days</asp:LinkButton>
</ContentTemplate>
</Vista:Toolbar>
</ContentTemplate>
</asp:UpdatePanel>
but Visual Studio is not complaining about inner ToolbarSetup
it's complaining about the outer UpdatePanelSetupToolbar control
So what's the deal with
Type '%s' does not have a public property named '%s'.
?
Unimportant additional notes
The ASP.net web-site compiles, builds, and runs fine. It's just the Visual Studio (2010) designer that complains.
So what's the deal with Type '%s' does not have a public property named '%s'.?
Series
This question is one in the ongoing Stackoverflow series, "Templating user controls":
How to add a Templating to a UserControl?
How to inherit from Control, rather than UserControl?
UserControl has IsPostBack, but Control does not
UserControl does not have public property named ContentTemplate
How do i specify CodeFileBaseClass from web.config?
The update panel is really cool but you really can't make user controls with it. The good news is you can make asp.net user controls that have a javascript payload by following the same design pattern used to make the updatepanel for the ajaxcontroltoolkit.
I built this back in Mar 2009 because I needed a message box that didn't block the ui. I followed the design pattern for usercontrols using the ajaxcontroltoolkit. It has complete source and the VS intelisense works on the javascript. It's a usercontrol that you just drop on the page and it carries it's own javascript payload that you can ajaxify to your hearts content. It even works in a data repeater with no modifications. It was actually accepted as a ajaxcontroltoolkit usercontrol on the website asp.net.
It's free you could put it on github or other repository if you wanted.
http://gosylvester.com/downloads/messagebox.aspx
http://gosylvester.com/blog.aspx?id=55
You should solve this per design.
Put your update panel on a page and make the user control without an update panel.
reason: the update panel is intended to autogenerate clientside scripting for asyncronous postback as you surely know.
The user control is a building block with a self contained functionality encapsulating complexity which should be placeable anywhere without requirements to the context and in this case at least you would need a scriptmanager on the page which instantiates the control.

ViewState is not required to preserve control values so what does it do?

I have a very simply web page with ViewState disabled everywhere:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" EnableViewState="false"></asp:TextBox>
<asp:DropDownList runat="server" id="mylist" EnableViewState="false">
<asp:ListItem>my item 1</asp:ListItem>
<asp:ListItem>my item 2</asp:ListItem>
<asp:ListItem>my item 3</asp:ListItem>
<asp:ListItem>my item 4</asp:ListItem>
<asp:ListItem>my item 5</asp:ListItem>
<asp:ListItem>my item 6</asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" Text="click me"/>
</div>
</form>
</body>
</html>
Code behind
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Dropdown list value is " + mylist.SelectedValue);
}
}
As you can see, no viewstate is enabled but it does preserve controls values, see here for the running example http://www.yart.com.au/stackoverflow/viewstate/test.aspx
Edit
latr0dectus has somewhat answered my question below. But what's an example where ViewState is required practically? I can't see what you need from the form other than control values.
#Petras: ViewState is not required to preserve control values so what does it
do?
Controls that implements IPostBackDataHandler uses LoadPostData() method to assign to some properties.
Read this article : Understanding ASP.NET View State
It is a common misconception among developers that view state is
somehow responsible for having TextBoxes, CheckBoxes, DropDownLists,
and other Web controls remember their values across postback. This is
not the case, as the values are identified via posted back form field
values, and assigned in the LoadPostData() method for those controls
that implement IPostBackDataHandler.
I'm not really sure what your question is.
View state is used in the page lifecycle. After the page is served it is destroyed on the server. Then the browser posts back it also posts back the viewstate. The server can use this in combination with the posted form values to recreate the previous state of the page and then show the changes.
In some cases even with viewstate disabled certain controls will appear to work as if they have viewstate enabled. This is because some controls have what is called "Control State". It operates almost like viewstate, except it cannot be disabled. This is because some controls would cease to function properly without it.
In the example you posted I think you are observing that the selected value of the dropdown is being posted to the server during postback. Not that it was reconstructed from viewstate.
Im adding this information that I found from the following link:
http://aspnetresources.com/articles/ViewState
What's the moral of this story? You don't always need view state enabled to maintain page state. "When do I need it though? What's it for then?" Glad you asked. The prime candidates for participation in view state are those controls that don't post back with the HTTP form and controls added or populated dynamically.
Scan to that part of the document and you should find what you are looking for.

The name 'GridView1' does not exist in the current context

I have two files named as TimeSheet.aspx.cs and TimSheet.aspx ,code of the file are given below for your reference.
when i build the application im getting error "The name 'GridView1' does not exist in the current context" even thought i have a control with the id GridView1 and i have added the runat="server" as well.
Im not able to figure out what is causing this issue.Can any one figure whats happen here.
Thanks & Regards,
=======================================
TimeSheet.aspx.cs
=======================================
#region Using directives
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSMS.Web.UI;
#endregion
public partial class TimeSheets: Page
{
protected void Page_Load(object sender, EventArgs e)
{
FormUtil.RedirectAfterUpdate(GridView1, "TimeSheets.aspx?page={0}");
FormUtil.SetPageIndex(GridView1, "page");
FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string urlParams = string.Format("TimeSheetId={0}", GridView1.SelectedDataKey.Values[0]);
Response.Redirect("TimeSheetsEdit.aspx?" + urlParams, true);
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
}
}
=======================================================
TimeSheet.aspx
=======================================================
<%# Page Language="C#" Theme="Default" MasterPageFile="~/MasterPages/admin.master" AutoEventWireup="true" CodeFile="TimeSheets.aspx.cs" Inherits="TimeSheets" Title="TimeSheets List" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">Time Sheets List</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<data:GridViewSearchPanel ID="GridViewSearchPanel1" runat="server" GridViewControlID="GridView1" PersistenceMethod="Session" />
<br />
<data:EntityGridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataSourceID="TimeSheetsDataSource"
DataKeyNames="TimeSheetId"
AllowMultiColumnSorting="false"
DefaultSortColumnName=""
DefaultSortDirection="Ascending"
ExcelExportFileName="Export_TimeSheets.xls" onrowcommand="GridView1_RowCommand"
>
<Columns>
<asp:CommandField ShowSelectButton="True" ShowEditButton="True" />
<asp:BoundField DataField="TimeSheetId" HeaderText="Time Sheet Id" SortExpression="[TimeSheetID]" ReadOnly="True" />
<asp:BoundField DataField="TimeSheetTitle" HeaderText="Time Sheet Title" SortExpression="[TimeSheetTitle]" />
<asp:BoundField DataField="StartDate" DataFormatString="{0:d}" HtmlEncode="False" HeaderText="Start Date" SortExpression="[StartDate]" />
<asp:BoundField DataField="EndDate" DataFormatString="{0:d}" HtmlEncode="False" HeaderText="End Date" SortExpression="[EndDate]" />
<asp:BoundField DataField="DateOfCreation" DataFormatString="{0:d}" HtmlEncode="False" HeaderText="Date Of Creation" SortExpression="[DateOfCreation]" />
<data:BoundRadioButtonField DataField="Locked" HeaderText="Locked" SortExpression="[Locked]" />
<asp:BoundField DataField="ReviewedBy" HeaderText="Reviewed By" SortExpression="[ReviewedBy]" />
<data:HyperLinkField HeaderText="Employee Id" DataNavigateUrlFormatString="EmployeesEdit.aspx?EmployeeId={0}" DataNavigateUrlFields="EmployeeId" DataContainer="EmployeeIdSource" DataTextField="LastName" />
</Columns>
<EmptyDataTemplate>
<b>No TimeSheets Found!</b>
</EmptyDataTemplate>
</data:EntityGridView>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
<br />
<asp:Button runat="server" ID="btnTimeSheets" OnClientClick="javascript:location.href='TimeSheetsEdit.aspx'; return false;" Text="Add New"></asp:Button>
<data:TimeSheetsDataSource ID="TimeSheetsDataSource" runat="server"
SelectMethod="GetPaged"
EnablePaging="True"
EnableSorting="True"
EnableDeepLoad="True"
>
<DeepLoadProperties Method="IncludeChildren" Recursive="False">
<Types>
<data:TimeSheetsProperty Name="Employees"/>
<%--<data:TimeSheetsProperty Name="TimeSheetDetailsCollection" />--%>
</Types>
</DeepLoadProperties>
<Parameters>
<data:CustomParameter Name="WhereClause" Value="" ConvertEmptyStringToNull="false" />
<data:CustomParameter Name="OrderByClause" Value="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter Name="PageIndex" ControlID="GridView1" PropertyName="PageIndex" Type="Int32" />
<asp:ControlParameter Name="PageSize" ControlID="GridView1" PropertyName="PageSize" Type="Int32" />
<data:CustomParameter Name="RecordCount" Value="0" Type="Int32" />
</Parameters>
</data:TimeSheetsDataSource>
</asp:Content>
Problem can be that GridView1 is not automatically added in designer.cs file. If that is case add it in designer manually.
Assuming a WebSite project verify that when building it you do not get Warnings like:
Generation of designer file failed: [Failure Reason]
It seems that you're not registering the custom control EntityGridView. See the Register directive to see how you can do it.
I've had this problem before when I've 'added' an existing file (.aspx + .aspx.cs) to a project and the designer file hasn't updated itself. I've tried many/all of the things written here, but I find that creating a new file, copying the code-front code in, then the code-behind and then rebuilding essentially does the trick. Yes, it's a pain in the * depending on the size of the file(s) you're working with, but this typically happens when I want to quickly test some demo code I've come across (some new project etc.) and throw it into my local VS.
It is nested, so some things don't happen automatically.
You might have to manually add it to the designer, or else (in VB) explicitly use the handles keyword or (in C#) explicitly wire up with "+=" operator.
Make sure all events or explicitly stated in the control mark-up
Since I see you list the event explicitly, I'd check the designer.
I had this same problem in Visual Studio 2010. The design.cs file was correctly generated. I closed Visual Studio, and reopened it. This resolved this issue for me (after much frustration).
I don't know if this will help, but I've been fighting with a similar situation.
The situation: I have included some code from the modified some of the templated asp.net web project into my project - specifically the login markup. For some reason, one of the "UserName" Textbox control refuses to be recognized in the designer. Strangely enough, the "UserNameLabel" control on the next line of markup is recognized:
<%# Page Language="C#" AutoEventWireup="True" CodeBehind="Logon.aspx.cs" MasterPageFile="~/Site.master" Inherits="SimpleWebApp.Logon" %>
<asp:Content ID="LogonRegister" ContentPlaceHolderID="MainContent" runat="server">
<div></div>
<table align="center">
<tr>
<td valign="top">
*<asp:TextBox ID="TextBox1" runat="server" CssClass="textEntry"></asp:TextBox>*
<asp:Login ID="LoginUser" runat="server" EnableViewState="true" RenderOuterTable="false">
<LayoutTemplate>
<span class="failureNotification">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
</span>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"/>
<div class="accountInfo">
<fieldset class="login">
<legend>Log In</legend>
<p>
**<asp:Label ID="lblUserNameLabel" runat="server">Username:</asp:Label>**
***<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>***
</p>
...
What I've tried:
I've restarted VWD 2010,
deleted the designer file and recreated it through converting the page to a web application
changed the name of the Textbox,
deleted and recreated the Textbox.
I decided to experiment a little, and discovered that adding a typical textbox just outside the asp:Login tag is recognized, while adding it just inside that tag leaves it unrecognized in the designer.
Figured this might help myself or someone else to piece together what might be going on.
Anyone have any idea what might cause this behavior around the asp:Login tag?
For closely related answers, visit a slightly similar question, How do you force Visual Studio to regenerate the .designer files for aspx/ascx files?.
#sameer: I'd be interested to hear if you tried to replace CodeFile="TimeSheets.aspx.cs" with CodeBehind="TimeSheets.aspx.cs" before converting the project.
I know this is an old thread, but I just struggled with an issue similar to this. After a couple of days, I figured out that I had a backup copy of the problem page and code behind in my project. I kept getting the error "does not exist in the current context" at compile time on objects placed on the new page, even though intellisense recognized them. I guess VS was getting confused because of the duplicate pages in the default namespace. Once I got rid of the backup, I magically stopped getting the errors.
I had the same problem, none of the solutions worked for me. I figured out without wasting too much time, that many of the ASPX pages in the Admin section were being given the same class name as the entities in the project entity files. Also, each of the pages listed the Entities namespace causing conflicts since after adding the entity namespace in the using directives, there were namespace conflicts. I went through and added "Page" to each of the ASPX page, recompiled and everything worked fine.
I know this thread has already been answered, but I wanted to include this description in case there are others that have this same problem and none of the above solutions worked, to give them something else that might be the issue.
I had 21 pages to change in my project, here is an example using the UserEntity and the Admin/UserEntity.aspx:
in UsersEntity.aspx front side aspx page, changed:
<%# Page Language="C#"
Theme="Default"
MasterPageFile="~/MasterPages/admin.master"
AutoEventWireup="true"
Inherits="UsersEntity"
Title="UsersEntity"
Codebehind="UsersEntity.aspx.cs"
%>
to:
<%# Page Language="C#"
Theme="Default"
MasterPageFile="~/MasterPages/admin.master"
AutoEventWireup="true"
Inherits="UsersEntityPage"
Title="UsersEntity"
Codebehind="UsersEntity.aspx.cs"
%>
in the UsersEntity.aspx.cs code behind, I changed:
public partial class UsersEntity : System.Web.UI.Page
to:
public partial class UsersEntityEntityPage : System.Web.UI.Page
and in the UsersEntity.aspx.designer.cs (Designer Page):
That page got automatically changed when I changed the code behind page to:
public partial class UsersEntityPage {
I did that for each of the other offending pages, which were all of them except for the "Edit" pages.
-- I guess I could have just removed the using directive to the Entity name space, but I really want to be able to have access to that in my pages, plus I think it is ungood for the page classes to have the exact same name as my entity classes. It causes confusion to me to have it like that.
copy the _.aspx file code and paste it on the _.aspx.cs page.
if it is not then import namespaces all the way top.
<%# Page Language="C#" AutoEventWireup="true" %> <%# Import
Namespace="System.Data" %> <%# Import
Namespace="System.Data.SqlClient" %>
I found that there was an incorrect file reference name in the designer.cs file. It was the code filename with a 1 added to the end. I removed the 1 and the code ran without error. Prior to this I deleted and rebuilt the designer file without resolution.
The easiest way is to:
Delete the designer file (YourFilename.aspx.designer.cs)
Select the ASPX file in Solution Explorer
Under Projects menu look for the item "Convert to Web Application" and click on it. Click Yes on the dialog window.
Open up the newly created designer file and make sure the namespace matches the namespace on the primary aspx.cs file (VERY IMPORTANT)
Close the designer file.
Save the solution. Close it and then reopen.

ASP.net scriptmanager and AJAX control

im making a username availability check in ASP.net using ajax updatepanel, but i keep getting the following error
The control with ID 'UserNameUP' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.]
im using the create user wizard, to create a new user but i want to check if the user already exists, i have the following code in the aspx file
<asp:UpdatePanel runat="server" ID="UserNameUP">
<ContentTemplate>
<asp:TextBox ID="UserName" runat="server" Width="200px" AutoPostBack="true" OnTextChanged="Username_Changed"></asp:TextBox></ContentTemplate> </asp:UpdatePanel><br />
but i keep getting that error, can any one help
thanks
Before your update panel, make sure you have a scriptmanager:
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
Do you have a script manager inside a master page? That can cause the "Only one instance of a ScriptManager can be added to the page" error?
Or, if its DNN, there is one on each "page" by default.

Resources