Setting page title from # Page directive in ASP.NET using master pages - asp.net

I am using master pages and am having trouble setting page titles from the # Page directive. All of my classes inherit from a class myPage which inherits from the ASP.NET System.Web.UI.Page class. Please Note: I have runat="server" set in my master page's head tag.
Here's what my # Page directives look like for the file test.aspx.vb:
<%# Page language="VB" MasterPageFile="~/MainMaster.master"
autoeventwireup="false" CodeFile="test.aspx.vb"
Inherits="test" Title="test" %>
Here's what test.aspx.vb looks like:
Partial Class test
Inherits myPage
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
This is what my master file, MainMaster.master, looks like:
<%# Master Language="VB" CodeFile="MainMaster.master.vb" Inherits="MainMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>untitled</title>
</head>
...
Now, when you go to view test.aspx in a browser you'd expect to see the title 'test'. but instead you will see 'untitled' as per the master page. Through trial and error, I modified the test class to inherit from System.Web.UI.Page directly, instead of myPage like this:
Partial Class test
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
and everything worked just fine. Why would my pages being children of myPage instead of System.Web.UI.Page prevent the title from being set correctly in the # Page directive?
I realize that I could just set the page titles programmatically through the Page_Load methods in every page, but I'd rather do it in the # Page directives in the .aspx files.
This is a very weird and frustrating problem, and I'm at a loss!
Thanks!!!

I thank everyone for their help; I have found a solution. The problem was that the myPage class had a property for Title, but in the Set part of the property was not passing the changes on to Page.Title as it should have been.
A one line change fixed my problem :)

What methods do you have in your base page (myPage.vb).
If you are overriding any of the default methods, are you calling the base versions of those pages?
in C# I'd have something like this:
protected override void OnInit(EventArgs e)
{
// Do my custom processing.
// Don't forget to call base OnInit here:
base.OnInit(e);
}
If you don't call these methods, then the events that happen in them for you (like wiring up the masterpage's title) won't fire.

I have a very similar set up to you. I have content pages inheriting from a custom base page which is itself inheriting from page. I am not having any issues with the title being set on the aspx and being shown in the browser. The only difference I see between my code and yours is my master page has the autoeventwireup property where your master page does not, and also your master page has a property called codefile and mine has codebehind.
Content Page:
<%# Page Title="Login to Application X" Language="vb" AutoEventWireup="false" MasterPageFile="~/masterpages/mymasterpage.Master"
CodeBehind="login.aspx.vb" Inherits=".login" %>
Master Page:
<%# Master Language="VB" AutoEventWireup="false" CodeBehind="mymasterpage.master.vb"
Inherits=".mymasterpage" %>

Related

Get Control by URL

I'm currently on my first asp.net WebForms Site. To display different Sites I'm loading a control into a Placeholder in my masterpage.
example: UserControl uc = this.LoadControl("~/controls/home.ascx") as UserControl;
this goes over my default.aspx codebehind file. I'm now struggeling with fill in another control based on the URL. I know that you can get for example an ID (if my-domain.com/default.aspx?id=1 then show control blabla) but is it possible that I read out the URL and instead of the ID a string? for examples:
my-domain.com/home -> this goes over my default.aspx, but shows de home controller
my-domain.com/contact-> this goes over my default.aspx, but shows de contact controller
Can I check if it's "/home" and then show the home-control?
Has anyone an idea, how I could solve this problem?
Thanks guys :)
Here are a few code snippets:
masterpage.master:
<asp:ContentPlaceHolder id="content" runat="server"></asp:ContentPlaceHolder>
default.aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/masterpage.master" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="myproject._default" %>
default.aspx.cs:
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ContentPlaceHolder content = this.Master.FindControl("content") as ContentPlaceHolder;
displayControl(content);
}
private void displayControl(ContentPlaceHolder content)
{
UserControl uc = this.LoadControl("~/controls/home.ascx") as UserControl;
if ((content != null) && (uc != null))
{
content.Controls.Add(uc);
}
}
}
home.ascx:
my HTML-Page
My folders are in the following structure:
css
img
javascript
App_Data
bin
obj
controls
home.aspx
contact.aspx ... etc.
default.aspx (with the code behind file .cs)
masterpage.master
misc: web.config and other files and folders...
If you want your site to go from domain.com/default.aspx?id=1 to domain.com/home, or domain.com/login you need to enable Friendly URLs and setup a route in your WebForms.
Scott Hanselman has a walk-through at the following link...
http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx

How to use class code of vb.net in asp.net

I'm having trouble with using my vb.net code in asp.net webpage.
<%# Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm1.aspx.vb" Inherits="Banking_Application.WebForm1" CodeFile="~/WebForm1.aspx.vb" %>
class name is WebForm1. I've written code in a separate file and I want to use it in page.
Like
<% Dim total As Integer
Dim val As tests.WebForm1
val = New tests.WebForm1
total = val.TotalBranches()
total.ToString()
%>
I'm new to vb.net and asp.net.
All suggestions are welcome.
Thanks!
Well first of all i need to clarify a few things for you:
in Asp.net, you can embed code in your aspx page using <% %> code block, or write it in a separate file and use it from there.
in your page declaration, you specify the code behind of your page using the CodeBehind= attribute, so if you want to place your code in WebForm1.vb, your page declaration should include CodeBehind="WebForm1.vb" .
note: CodeFile="~/WebForm1.aspx.vb" is not needed.
the structure of the code behind for you aspx page, should look like that:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function test1()
Return "test"
End Function
End Class
and you can add more functions as needed. in the example above, i have added a function called test1 for the purpose of this example.
now, after you have created your code behind in the correct structure, you can call its methods from the aspx page as if it was the same page:
<% =test1()
%>
this will return "test" as specified by the function in code behind.
you do not need to instantiate the class.
I think you are missing what is happening with the mechanics of asp.net.
You either:
Write code directly in the aspx page (within the html)
Or, you wrtie code in the code behind page... your WebForm1.aspx.vb page. From the code behind page, you can access and manipulate the server controls that is on the aspx (html) side...

Can not access control from nested master page in content page

I am using nested master pages where i want to use Label control from nested master page and update its text. but it is not accessing. When i removed outer master page then it is working fine. Following is the markup and code.
OUTER MASTER
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Roster.Site" %>
NESTED MASTER
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="RoasterMaster.master.cs" Inherits="Roster.RoasterMaster" MasterPageFile="~/Site.Master" %>
<%# MasterType VirtualPath ="~/Site.Master" %>
CONTENT PAGE
<%# Page Language="C#" AutoEventWireup="true" Inherits="RequestsView" CodeBehind="ViewRequestsByPM.aspx.cs" MasterPageFile ="~/Roaster/RoasterMaster.Master" Title ="Roaster- View Requests by PM" %>
<%# MasterType VirtualPath ="~/Roaster/RoasterMaster.Master" %>
CONTENT PAGE CODE
protected void Page_Load(object sender, EventArgs e)
{
Label lblTitle = new Label();
lblTitle =(Label)Master.FindControl("lblTitle");
lblTitle.Text = "View Roaster Request";
}
What is going wrong with the implementation. Please help. Thanks
You can add the below code snippet in
NESTED MASTER PAGE
public string PageTitle { get; set; } // In page_load
lblTitle.Text = PageTitle;
CONTENT PAGE CODE
this.Master.PageTitle = "YOUR TEXT";
This will work for you...
Assuming that your label is in Roster master page, you can simply add method to set the text in master page code behind. For example,
in RoasterMaster.master.cs
public void SetTitle(string value)
{
this.lblTitle = value;
}
And in content page code
Master.SetTitle("View Roaster Request");
In case, your label is in outer master then you can similarly forward the call to the outer master from roster master code.
EDIT
Your code does not work in nested master case scenarios because Master page contents get added within page control hierarchy with different naming container. FindControl method does not span multiple naming containers which is the case here - because of nesting you have nested naming containers. Page.Master would give you outer naming container but your label might be lying in inner naming container. One of the way, is to write your own find control implementation that will recurs within the control tree but really it does not make sense - I would rather use above code which is more efficient and more importantly better maintainable.

ASP.NET Page does't show code behind code when clicking on Design code

Page does't show code behind code when clicking on Design code and also code behind code display error as control not found
Below code display error : System Error
<%#Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>
I don't why it fetches this type of error ,please help me
Code Behind:
using System;
The only reason for which the designer cannot open the code behind is because it cannot find the associated code behind file specified Inherits="Register"
Make sure you have a public class named Register that must be Partial as well
make sure also the the code behind file name is named as Register.aspx.cs
must check make Inherits attributed in design and code file path
Ok, I totally misunderstood your question.
If your Register.aspx file looks like this:
<%#Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>
The Register.aspx.cs should look (at least) like this:
using System;
namespace YourDefaultNamespace
{
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
I notice that you don't have a namespace in the Inherits attribute of your <%# Page %> directive. It should have the same namespace as your codebehind file. In my example that's YourDefaultNamespace, so my <%# Page %> directive should be this:
<%#Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="YourDefaultNamespace.Register" %>

Show Image/HTML before Page_Load is completed

I have following code in aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" nherits="test" %>
<!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"></head>
<body>
<img src="images/loading_anim.gif" />Please wait...
</body>
</html>
In test.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
......Do some processing here .
Response.redirect("Next.aspx")
End Sub
Code Behind I do some processing in Page_load method and redirect to other page but it might take some time so I want to show user loading image.But it shows that after page_load is completed.How to handle this ?
I will suggest that you use a generic handler (ashx) and use un-buffered response. For example:
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
context.Response.BufferOutput = false;
context.Response.Write("<html><head></head><body><img src=\"images/loading_anim.gif\" />Please wait...</body></html>"
context.Response.Flush();
// do your processing
...
// redirect
}
...
}
Yet another way to first show image on the client side (using java-script) and then do redirection (or post) from client side.
I don't think it will work because always the server side code works first, then only HTML rendering starts. Better option will be using Ajax.
More details here : ASP.NET Integration with IIS 7

Resources