Cross Page Posting with ASP.NET value not available on target page - asp.net

I am trying to pass a value between two pages. However on my target page, the value is not found.
My source page includes the following code.
Public ReadOnly Property SQLString() As String
Get
Return "SELECT * FROM City"
End Get
End Property
On my target aspx page I have included the following directive:
<%# PreviousPageType VirtualPath="~/tools/SearchResults.aspx"%>
In the target page code behind I have included the following in the page load:
Me.Master.Page.PreviousPage.SQLString
However, Visual studio complains that, SQLString is not a member of System.Web.Ui.page.
I must note I am useing master pages, and vaguely recall this causing an issue when accomplishing this in the past. Anyone have any ideas?

You need to cast the page to whatever type it actually is.
CType(Me.Master.Page.PreviousPage, ActualPageType).SqlString
In the code behind of your source page you will have a class declaration like the following:
Public Class ActualPageType
Inherits Page

Related

ASPX Page 'Could not load type' Error

I have an .aspx page, we’ll call it Data.aspx, and it is going to use an ajax request to retrieve a response from a function called GenerateDocument in another .aspx page in the project, we’ll call this Document.aspx. It submits its ajax request, but the request fails, and I’m trying to decipher if it has to do with my page directives on Document.aspx. The entirety of the content on Document.aspx is this (it is not meant to generate a page, only generate documents based on data passed to it):
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Document.aspx.vb" Inherits="NameSpace.Document" %>
In Document.aspx.vb, we have:
Public Class Document
Inherits Generic.WebPage
<System.Web.Services.WebMethod()> _
Public Shared Function GenerateDocument
‘Process data and return as document
End Function
End Class
(There's more than just this; there's also a page load function, etc, but this is the relevant function I'm trying to hit.)
With this configuration, the error we get is complaining about ‘Could not load type NameSpace.Document’
I didn't think I should have to use a namespace that the class does not seem to be contained within, so I tried just saying Inherits="Document", but with the same error loading type message coming back.
I am building the project after I make fixes and getting no build errors, but if I try and change the CodeBehind to a CodeFile attribute, I get runtime compilation errors instead of type errors (I am tracking this through Fiddler).
This seems like it's probably an obvious issue where the function is just not being hit, but I can't seem to parse it. Any help? Thanks.
I figured out the issue. Thanks to all those who responded!
The problem was that while the files were all in the proper directory, one was not added to the project within the solution, and despite the references being in order (Inherits I left pointing to a namespace the class inherited from, plus the class), the program was not finding them at run time. This became apparent when I tried using CodeFile and got compilation errors and realized that it simply did not know what class to point to.
TL;DR - I added the .aspx file to the project and the whole thing worked fine.

Getting hidden field value from content-page after cross-page post

I have the following situation:
Page1.aspx is based on a master page and has a hidden field called "hdFlotaID" in it. On the same page I have a button for which I set PostBackUrl="Page2.aspx". When I click the buton, the Page2 is loaded but I can't get the hidden field. I tried both (as I saw on msdn or other posts):
this.PreviousPage.Controls[0].FindControl("hdFlotaID")
and
this.PreviousPage.FindControl("hdFlotaID")
but they return null.
This.PreviousPage returns a value, but Controls[0] of that value seems to return the masterpage and I want the content page.
I also checked and the hidden field has runat server value and ClientID mode is set to static (I even checked the generated HTML and the ID is correct)
Can you please help me! Thank you
SOLUTION: Ok,so based on your help I got it to work like this
this.PreviousPage.Controls[0].FindControl("CPH").FindControl("hdFlotaID")
where CPH is the ID of the ContentPlaceHolder from the master page.
Also the ideea with the public property is very good allthough in my case adding the PreviousPageType directive was giving me some errors about namespaces. If I removed the directive and cast it in codebehind it works fine.
Thank you all very much
FindControl searches only one level i.e. top level container and all the controls of your content page are not directly in Master control collection rather inside master's content control collection.
To achieve this
1) you need to write a recursive version of FindControl. try like this (did not tested it):
((Page1)this.PreviousPage).FindControl("hdFlotaID")
or
2) Typecast the previous page to the type of page it is. Then you can access controls.
Set up a property in page1.aspx that returns value of hidden field using this.Page.MasterPage.FindControl("hdFlotaID"). In Page2.aspx, add "PreviousPageType" tag in ASPX file. This way you can access previos page properties in a typesafe way e.g. this.PreviousPage.hdFlotaID_property

'txtName' is not declared. It may be inaccessible due to its protection level

After putting a textbox on my page when I compile it I get the above error:
'txtName' is not declared. It may be inaccessible due to its protection level.
This is happening when I try to read the value of the textbox in the codebehind page.
I'm not sure what's causing this...any ideas?
In the aspx file I have:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="signup.aspx.vb" Inherits="signup" %>
In the codebehind aspx.vb file I have:
Imports System.Data.SqlClient
Partial Class signup
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Ok, I have this resolved now. It was becuase I had renamed a copy of the .aspx file and which still using the newer versions code behind file.
The code behind file was looking for txtName.text but on the older version of the .aspx file txtName didnt exist!
I have excluded the older version of the page from the project and it appears to run ok now.
Thanks everyone for you help.
Is the textbox directly on the page? Also what type of project is that? Is it a web application or web site opened from file system? If it's a Web Application, the designer may have failed to update YourPage.aspx.designer.cs file. Check that file to make sure you have a definition for txtName something like:
protected global::System.Web.UI.WebControls.TextBox txtName;
If its not there, delete .design.cs file, right click on the page and choose "Convert To Web Application. This will recreate .design.cs file correctly.
May be this is missing in the textbox declaration:
runat="server"
I had a different cause for the exact same error and it is similar to what the OP found but not quite. My page.aspx.designer.vb got out of sync and had an error. One of my controls was being defined twice (I think because I copy/pasted an existing control without editing the ID before pasting). Once I deleted the 2nd declaration I could build and the errors were gone. Interestingly, VS 2010 SP1 didn't list THAT error along with the others resulting from the lack of that page building. Hope this helps someone else.
The way the classes are ordered is that your page's generated class inherits from your codebehind class - they're not partials, and it's not the other way around. This means that your codebehind class has no knowledge of the controls you've placed on the page. If you want access to a property that you declare in the page itself you need to declare it in the codebehind class first:
protected TextBox txtName;
You can then access this member in the codebehind file. This will get wired up correctly to match your control in the page.
I have had that problem before. Not sure what caused it. But by deleting the offending textbox and adding it again it fixed it.
I've gotten this error when the class in the code behind doesn't specify Public. Try this:
Partial Public Class signup <-------------Make sure the Public keyword is there
Inherits System.Web.UI.Page
...
End Class
This is what happened at my case.
-I have declared the variable at behind code
Protected Friend sMessage As String = ""
-Then the aspx : <%=sMessage%>
this case appears (visual studio 2015 - asp webform project)
Then I try to :
-clean solution
-Rebuild
Then the problem is gone
If the clean solution and rebuild does not work
try to clean the obj directory
If the variable is asp component
make sure runat="server" at the aspx file
I got this issue, by removing runat="server" attribute from a div, even though I was not accessing the div on server side. The problem lied in the fact that on client side I was trying access the ClientID of the div like this $('#<%=divMultipleDaysOptions.ClientID%>').
I had the same problem when I changed my asp.net website from 3.5 to 4.0.
To solve the problem I recreated the entire website in 4.0 page by page, making sure all pages have the same names.
I copied page contents for each page leaving out the top line that starts with <%# from the 3.5 website to the 4.0 website.
I copied all the code in the page class in the code behind for for each page from the 3.5 to the 4.0 website.
I got similar error but scenario was different. Scenario: I have added existing page to my solution in a folder so i had to change the values for some properties (Inherits) in Page tag of html file. When i changed the folder structure in Inherits property i made a mistake and that caused this error. When the folder structure is different the code behind file file will not be able to access the control in .aspx file. So friends, when you get this kind of error please have a look at Inherits property value (i.e, folder structure is correct).
Another possible scenario is if the class name in the Page.aspx.vb file differs from the class name in the Page.aspx.designer.vb file. These need to be identical.
I had a similar problem after adding controls to a web sete originally written in VS 2008 .Net 3.5 to VS 2010 .Net 4 After adding controls, I got the same error. When I looked at the .designer.vb file for the page, the added controls were not there. The problem was resolved by just adding the controls to the bottom of the file like:
Protected WithEvents lblApprove As Global.System.Web.UI.WebControls.Label

.Net 3.5 NameSpace Problem?

Got an asp.net 3.5 app - compiles fine, but when browse to page I get the error below.
Searches for this all seem to indicate it is a namespace problem, but all of my pages seem to declare the proper namespace.
Any assistance appreciated!
Phil J.
Server Error in '/Internet/bm2/bm2' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: 'bm2.PolicyLookup' is not allowed here because it does not extend class 'System.Web.UI.Page'.
Source Error:
Line 1: <%# Page Language="vb" AutoEventWireup="false" CodeBehind="PolicyLookup.aspx.vb" Inherits="bm2.PolicyLookup" validateRequest="false" aspcompat="true"%>
Line 2:
Line 3:
Source File: /internet/bm2/bm2/policylookup.aspx Line: 1
=======
The System.Web.UI.Page is indeed inherited from in the code-behind:
Namespace bm2
Public Class PolicyLookup
Inherits System.Web.UI.Page
Protected WithEvents lblResponse As Label
Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
Dim myModel As New bm2.Model
Dim postData
Public objMessage
If this is a namespace problem (very unlikely), you'll either need to add an #import statement to your page:
<%# Import namespace=”My.Namespace.Where.bm2.PolicyLookup.Exists” %>
or modify your #Page directive to include the fully-qualified class name
....inherits="My.Namespace.bm2.PolicyLookup"
Of course, this only works if bm2.PolicyLookup descends from System.Web.UI.Page.
The #Page attribute
inherits="bm2.PolicyLookup"
tells ASP.NET that the class bm2.PolicyLookup inherits from System.Web.UI.Page, and that it should be instantiated when processing the page to handle on_load(), render(), etc events. However, ASP.NET believes that your class bm2.PolicyLookup does not inherit from System.Web.UI.Page, thus the error.
In order for the webforms engine to be able to process your page, it has to descend from the existing Page class. That class provides all of the event handlers, rendering methods etc. that are required.
Also, a bit of unrelated advice: Visual Studio hides a lot of functionality when working with a VB.Net site. Consider moving to C#, and to a web application project instead of a website project.
I don't know if there is a PolicyLookup class in System.Web (I don't think there is), but I ran into a similar problem when I tried to name a page SiteMap.aspx, since there was a type named SiteMap (though it was in a different namespace, it still seemed to cause a problem). The fix was to either rename completely, or have the code behind class name be prefixed with an underscore (you may have to update the Page directive in your aspx to match the new type name.

User Controls Not seeing the Day of Light - Doesn't recognize code-behind methods

This is driving me absolutely nuts.
I created a new WAP project in VS 2008. Copied over the files in my Web Site Project. Added any required references. Tried to convert the Web Project to a Web Application using the "Convert to web application".
None of my user controls are able to see methods in their code behind. They don't even see them so I get errors everywhere saying it doesn't know what this or that method is.
Example:
<%=CreateMenu(xxx.WebMenuType.Occasion, "menuShopOccasion", "Occasion") %>;
That is in my Header.ascx
And so it errors out because it has no clue what CreateMenu is!
In my Header.ascx.cs it's there and was being referenced with no problem in my old Web Site Project:
protected string CreateMenu(xxx.WebMenuType menuType, string menuID, string title)
{
...
}
It's probably a namespace problem. Make sure that the Inherits attribute in your <%# Page ... %> declaration refers to the correct path to the code behind file, including the namespace. The designer file must also be in the same namespace as the code behind.
I am not entirely sure this is your problem but....
you may be missing the .designer.cs files. For your example above there would also be a Header.ascx.designer.cs which contains a partial class (Header) which has all the declarations of the controls in the Header.ascx file?

Resources