I know we've done this before in another .aspx page that's using this master page. So I tried this in a new .aspx but for some reason, it is not recognizing the Master object. And the .aspx definitely is set to the master page in the page directive correctly and there's no errors to that effect:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class LandingPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Master.HideNavbar();
}
}
Try to reference your masterpage at your aspx file :
<%# MasterType virtualpath="~/YourMasterPage.master" %>
to get typed Master page class in your code you need to define what type is it, either in Page directive or with MasterType directive on page:
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
If you don't have these directives, you can always cast Master property in code:
(MasterPage)this.Master
see also here
http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx
Edit:
is HideNavbar() method public?
Related
I tried something by mistake and i do not understand why this is working:
I've created 2 asp.net web forms called page1.aspx and page2.aspx
On page1.aspx:
In code behind i declare a static string: field1.
I put a simple button.
When I click on this button:
field1="Hello world"
Response.Redirect("page2.aspx")
On page2.aspx, in Page_load, i display page1.field1 value.
When page2.aspx is loaded, page1.aspx should not be loaded in memory. I do not understand why page1.field1 still contains "Hello world value !"
Can anyone explain me why this code works ? Is it a good thing to work this way ?
Does asp store static fields in viewstate or session ?
Thanks
Here is page1.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="page1.aspx.cs" Inherits="WebApplication7.page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Here is page1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class page1 : System.Web.UI.Page
{
public static string field1;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
field1 = "Hello world";
Response.Redirect("page2.aspx");
}
}
}
Here is page2.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(page1.field1);
}
}
}
The keyword "static" means that only one instance of a given variable exists for a class. Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it.
Static variables can be initialized outside the member function or class definition.
Note
Unlike other member variables, only one copy of the static variable exists in memory for all the objects of that class. Therefore, all objects share one copy of the static variable in memory.
In your situation, you had set field1 = "Hello World" in Page1 and on button click you will be redirected on Page2 and write that field1 variable on your page which is the static variable of your Page1 class.
Once you make a field static, it will be in memory till app pool is recycled(full application memory is recycled). I don't know what you are trying, but declaring a static field in a code behind pages is not a good idea.
I have an asp.net web form website in which i use URL routing
the thing is when ever I try ti navigate to a matched route e.g "http://localhost:51878/brand/adidas" it doesn't open the specified aspx file it shows HTTP Error 404.0 - Not Found and navigates to D:\Websites\Website\brand\adidas
update: I tried adding break points to the global.asax.cs file apparently it doesn't even go through the Application_Start function
here's my
global.asax`
<%# Application Language="C#" CodeBehind="Global.asax.cs" %>
and here's my global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
public partial class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
protected void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("brandsRoute", "brand/{brand}", "~/brand.aspx");
}
}
would u please tell me what I'm doing wrong
The code you posted looks fine, except for the erroneous > character under your using statements, which may be causing your code not to build, and perhaps you are running a last good build version that doesn't include your new route.
I have an asp.net page called prequal.aspx with a codebehind of prequal.aspx.cs. It works. I want to separate out each client code from this page into their own partial files (to reduce chance of modifying the wrong one by mistake later.) Both .cs files begin as such:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 System.Data.SqlClient;
public partial class prequal : System.Web.UI.Page
{
When I put one of the clients code into its own file, it compiles fine but I get the following error when I try to view the page:
CS1061: 'ASP.prequal_aspx' does not contain a definition for 'lnkLanguage_Click' and no extension method 'lnkLanguage_Click' accepting a first argument of type 'ASP.prequal_aspx' could be found (are you missing a using directive or an assembly reference?)
prequal.aspx has this:
<asp:LinkButton id="lnkLanguage" onclick="lnkLanguage_Click" runat="server" CausesValidation="False">English / En EspaƱol</asp:LinkButton>
prequal.aspx.cs has this:
protected void lnkLanguage_Click()
{
// alternate preferred language
if (Session["lang"].ToString() == "spa")
{
Session["lang"] = "eng";
}
else
{
Session["lang"] = "spa";
}
populateQuestions();
}
populateQuestions() will call other code in prequal.aspx.cs which calls code in prequal-client1.aspx.cs. The code works before I split it up so am I going about creating separate partial class files incorrectly? Or is the issue something else that I am unaware of yet?
I believe the signature for lnkLanguage has to be:
protected void lnkLanguage_Click(object sender, EventArgs e)
{
//...
}
I have an .aspx file that has 3 drop down lists:
ddlMake
ddlModel
ddlColour
i have a Page_Load function but i cant acces them in the Page_Load function...
using System;
using System.Collections.Generic;
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;
namespace NorthwindCascading
{
public partial class _IndexBasic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CarService service = new CarService();
List<string> Makes = service.GetCarMakes();
ddlMake.DataSource = Makes;
ddlMake.DataBind();
ddlMake.Items.Insert(0, " -- Select Make -- ");
}
}
}
}
I have added the code-behind file manually so i guess i am missing something... it just says that the ddlMake element is not defined in current context...any suggestions?
Rather than figure out what went wrong. I suggest you just simply delete the file and re-do what you have done again. Will save your time....
Make sure your CodeFile/CodeBehind attribute in the page directive is pointing to the correct file. If so, make sure the Inherits attribute in the page directive is naming the correct class name.
If you added the code behind manually, then the _IndexBasic.designer.cs probably doesn't contain the protected members, which would be why you cannot see them here. Or, your aspx is not referencing this as your codebehind.
Right-click on your .aspx page and hit Convert to Web Application - that will create and populate the designer file.
Is it possible to write a System.Web.UI.Page and stored in an assembly?
And how can I make iis call that page?
So I will go deeply...
I'm writing a class like that:
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 System.Runtime.InteropServices;
using System.Reflection;
using WRCSDK;
using System.IO;
public partial class _Test : System.Web.UI.Page
{
public _Test()
{
this.AppRelativeVirtualPath = "~/WRC/test.aspx";
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("You are very lucky!!!");
}
}
That are stored into an assembly.
So Now How can I register that assemply and obtain that http://localhost/test.aspx invoke that class?
Thanks.
Bye.
You'll want to use an HttpHandler or HttpModule to do this.
Registering the assembly is just like registering any assembly -- just define that class in a code file and have the compiled DLL in your bin directory.
Then, as an example, you can create a IHttpHandlerFactory:
public class MyHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, ........)
{
// This is saying, "if they requested this URL, use this Page class to render it"
if (context.Request.AppRelativeCurrentExecutionFilePath.ToUpper() == "~/WRC/TEST.ASPX")
{
return new MyProject.Code._Test();
}
else
{
//other urls can do other things
}
}
.....
}
Your web.config will include something like this in the httpHandlers section
<add verb="POST,GET,HEAD" path="WRC/*" type="MyProject.Code.MyHandlerFactory, MyProject"/>
Not sure what you're after here. If you set up a deployment project, there's a setting to have it merge all the dll files into a single assembly. Is that what you want? Either way, if you want to reuse the same code behind class for several aspx pages, it is the page declarative (1st line of code in the aspx) that you must change.
Few options
1. You can refer to this assembly as part of visual studio references
2. Use relfection to load the assembly and class from your test ASAPX page.