First of all, I'm very new to asp.net (Few days worth of experience).
I am using the default content provided by Visual Studio 2013 to work on a Web Forms Application.
Scenario: I would like to change the text of a Histamine(h2) after clicking a button.
Problem: I am using the default master page provided by Visual Studio but it is not loaded correctly on postBack. From what I can tell the resources it uses for it's layout can't be accessed? I'm not entirely sure what I'm doing wrong. If someone could enlighten me I would very much appreciate it.
My code so far:
Default2.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content runat="server" ContentPlaceHolderID="MainContent">
<h2 id="h1" runat="server">Change me please.</h2>
<asp:Button ID="b1" Text="Submit" runat="server" OnClick="dothis" />
</asp:Content>
Default2.aspx.cs:
public partial class Default2 : Page
{
protected void dothis(object sender, EventArgs e)
{
Header.InnerHtml = "Hello world.";
}
}
See change html tags text , on server side (C#, ASP.NET) for the code you need to implement. In your case:
h1.InnerHtml = "Hello";
As an alternative, instead of using an <h2> server control, you can wrap something like a label or literal control within the header tag, like this:
<h2><asp:LiteralControl runat="server" id="HeaderLiteral">Change me please</asp:LiteralControl></h2>
Then in your code-behind you can do this:
HeaderLiteral.Text = "Hello World!";
Personal preference but I generally shy away from Server Controls. No particular reason, but offering this suggestion for completeness sake.
Related
I am using server side controls in web form application. In this i am appending my HTML elements and server side elements out side of the form (asp:content) but its appending to body of that page. In server side (in button click) the value/ text of the controls return null. Is there any specific reason for this for returning null when control kept outside of the asp:content tag.
I know its may its not kept inside the form but why i am not posting just taking the values in server side event for further process
Thanks for any suggestion
Here below i have added the textbox to a page.Later via script i will append to body tag. now while checking the server side its return null
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CheckboxIssue._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<script>
$("#MainContent_TextBox2").appendTo("body");
</script>
}
server side
protected void btn_Click(object sender, EventArgs e)
{
var text1 = TextBox1.Text;
}
They called it WebForms for a reason. The concept is to keep server controls inside a form and post it back all the times on an ui event with a server side handler. So you have to follow that rule if you are using webforms.
You may get the controls placed outside a form rendered, but aspx engine won't pick their values on the postback.
This question most likely has an easy solution, but I cannot figure it out. I have a .aspx page with the following code:
<%# Page Title="Update ASV Information" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UpdateASV.aspx.cs" Inherits="COAF_Process_to_ASV_Relation_Tool.UpdateASV" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" >
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<asp:Button runat="server" ID="asv_update" OnClick="asvUpdate_Click" Text="Update ASV" />
</asp:Content>
I want to be able to write text from the C# code behind this page (UpdateASV.aspx.cs). Whenever I try:
Response.Write("some text");
It puts the code behind the content2 placeholder. I want it inside. Is there an easy way to do this?
Response.Write directly modifies the response. You don't want to do that in your web forms application. You should be modifying the contents of a Label, Literal, or PlaceHolder control from the code-behind.
Use Label to put text into the page
Use Literal to put raw html into the page
Use PlaceHolder to add new controls to the page dynamically.
Either way, the placement of the control(Label, Literal, or PlaceHolder) on your page determines where on the page your output will be rendered.
If you are determined to stick to server side controls:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
Literal myText = new Literal()
{
Text = "some text"
};
Content2.Controls.Add(myText);
}
}
Otherwise, just put the code inline
<%=Response.Write("some text") %>
I'm having a really frustrating issue with a TreeView control in a Web Form. I'm using C# on Visual Studio, and, admittedly, this is my first project on the platform, so there are a lot of caveats I'm finding I'm not aware of.
The page I'm working on isn't that complicated. In the body of WebPage.aspx, I have an empty TreeView control declared, along with a Label to display output (for testing) and a Button to cause a postback:
<asp:TreeView ID="SimpleTreeView" runat="server">
</asp:TreeView>
<asp:Label ID="StatusLabel" runat="server">No change yet.</asp:Label>
<asp:Button ID="SaveButton" runat="server" Text="Save Changes" PostBackUrl="~/WebPage.aspx" />
I populate the TreeView programmatically within the Page_Load method of my WebPage class, based on data in our SQL server. That part works like a charm.
Now, I want to define the event handler for the TreeView's TreeNodeCheckChanged event. Firstly, YES, I know that the event does not fire immediately when a checkbox is changed; 99% of everything I can find on the Internet about this is people complaining about that. For my application, waiting until the button is clicked is fine.
I've followed what the MSDN article on the event does, by adding the OnTreeNodeCheckChanged attribute to the TreeView control:
<asp:TreeView ... OnTreeNodeCheckChanged="CheckChangedMethod">
and added a corresponding method to the WebPage class in WebPage.aspx.cs:
protected void CheckChangedMethod( object sender, TreeNodeEventArgs e )
{
StatusLabel.Text = "A checkbox was changed!";
}
When I launch the page, though, I get an error screen that says:
CS1061: 'ASP.webpage_aspx' does not contain a definition for 'CheckChangedMethod' and no extension method 'CheckChangedMethod' accepting a first argument of type 'ASP.webpage_aspx' could be found (are you missing a using directive or an assembly reference?)
I have found that if I move the method to within a tag on WebPage.aspx, it works! Unfortunately, though, I need the method to access classes and methods in other parts of the site, which I can't from within the script tag (...right?). How can I get my event handler recognized by the compiler without removing it from the WebPage class?
Edit:
The total markup looks like this:
<%# Page Title="Web Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireUp="true" CodeBehind="WebPage.aspx.cs" Inherits="Project.WebPage" %>
<%# MasterType VirtualPath="~/Site.Master" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolKit" %>
<asp:Content ID="Content1" ContentPlaceHolder="HeadContent" runat="server">
<!-- This is where I placed the script tag to test the method -->
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolder="MainContent" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<h2><em>Headline</em></h2>
<br />
<asp:TreeView ID="SimpleTreeView" runat="server" ExpandDepth="0" ShowCheckBoxes="All">
</asp:TreeView>
<asp:Label ID="StatusLabel" runat="server">No change yet.</asp:Label>
<asp:Button ID="SaveButton" runat="server" Text="Save Changes" PostBackUrl="~/WebPage.aspx" />
</asp:Content>
Well... Not sure what happened, but after fixing bugs elsewhere on our site, the code above magically decided to start working. I'm not going to question it.
I have an asp.net website with a master-page, can I use the iframe so my .aspx pages will load inside the iframes. (Meaning it wont load the master-page)
Kinda like my iframe will be the contentplaceholder or maybe the contentplaceholder will be inside it?
Any Ideas?
try this
<iframe name="myIframe" id="myIframe" width="400px" height="400px" runat="server"></iframe>
Expose this iframe in the master page's codebehind:
public HtmlControl iframe
{
get
{
return this.myIframe;
}
}
Add the MasterType directive for the content page to strongly typed Master Page.
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits=_Default" Title="Untitled Page" %>
<%# MasterType VirtualPath="~/MasterPage.master" %>
In code behind
protected void Page_Load(object sender, EventArgs e)
{
this.Master.iframe.Attributes.Add("src", "some.aspx");
}
Another option is to use placeholders.
Html:
<body>
<div id="root">
<asp:PlaceHolder ID="iframeDiv" runat="server"/>
</div>
</body>
C#:
iframeDiv.Controls.Add(new LiteralControl("<iframe src=\"" + whatever.com + "\"></iframe><br />"));
How about:
<asp:HtmlIframe ID="yourIframe" runat="server" />
Is supported since .Net Framework 4.5
If you have Problems using this control, you might take a look here.
You can think of an iframe as an embedded browser window that you can put on an HTML page to show another URL inside it. This URL can be totally distinct from your web site/app.
You can put an iframe in any HTML page, so you could put one inside a contentplaceholder in a webform that has a Masterpage and it will appear with whatever URL you load into it (via Javascript, or C# if you turn your iframe into a server-side control (runat='server') on the final HTML page that your webform produces when requested.
And you can load a URL into your iframe that is a .aspx page.
But - iframes have nothing to do with the ASP.net mechanism. They are HTML elements that can be made to run server-side, but they are essentially 'dumb' and unmanaged/unconnected to the ASP.Net mechanisms - don't confuse a Contentplaceholder with an iframe.
Incidentally, the use of iframes is still contentious - do you really need to use one? Can you afford the negative trade-offs associated with them e.g. lack of navigation history ...?
I recently converted a website project to a web application project in Visual Studio 2008. I finally got it to compile, and the first page (the login screen) displayed as normal, but then when it redirected to the Default.aspx page, I received an error:
Parser Error Message: 'SOME.NAMESPACE.MyApplicationName.WebApplication._Default' is not allowed here because it does not extend class 'System.Web.UI.Page'.
All of my pages inherit from a class called "BasePage" which extends System.Web.UI.Page. Obviously the problem isn't with that class because the login.aspx page displays without error, and it also inherits from that basepage.
All the pages on the site, including the login page, are children of a masterpage.
After some testing, I have determined what it is that causes the error (although I don't know WHY it does it).
On all the pages where I have the following tag, the error does NOT occur.
<%# MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
On all the pages that do not contain that line, the error DOES occur. This is throughout the entire application. I have the tag only on pages where there has been a need to reference controls on the MasterPage.
So, I thought I would just add that line to all my pages and be done with it. But when I add that line, I get a compile error:
'object' does not contain a definition for 'Master'
This error is coming from the designer.cs file associated with the ASPX page that I have added the "MasterType" declaration to.
I've forced a rebuild of the designer file, but that doesn't change anything. I compared the content of the Master reference in the designer files between the login.aspx (working) and the default.aspx (not working) but they are exactly the same.
Since I'd really like to get it to work without having to add the "MasterType" declaration to everypage, and since that "fix" isn't working anyway, does anyone know why not having the "MasterType" declaration on an aspx file causes the parser error? Is there a fix for this?
Example Code:
Here is the code for login.aspx and login.aspx.cs which is working without error:
Login.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainMaster.master" AutoEventWireup="true" Inherits="SOME.NAMESPACE.MyApplicationName.WebApplication.Login" Codebehind="Login.aspx.cs" %>
<%# MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
<table>
<tr>
<td>
<asp:UpdatePanel ID="upLogin" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" DefaultButton="Login1$LoginButton">
<asp:Login ID="Login1" runat="server" LoginButtonStyle-CssClass="button"
TextBoxStyle-CssClass="textBoxRequired"
TitleTextStyle-CssClass="loginTitle" >
</asp:Login>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upPasswordRecovery" runat="server">
<ContentTemplate>
<asp:PasswordRecovery ID="PasswordRecovery1" runat="server"
SubmitButtonStyle-CssClass="button" TitleTextStyle-CssClass="loginTitle"
SuccessText="Your new password has been sent to you."
UserNameInstructionText="Enter your User name to reset your password." />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="SideBarPlaceHolder" Runat="Server">
<h2>Login</h2>
<asp:Button ID="btnCreateAccount" runat="server" Text="Create Account" OnClick="btnCreateAccount_Click" CausesValidation="false" />
</asp:Content>
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SOME.NAMESPACE.MyApplicationName.WebApplication;
using SOME.NAMESPACE.MyApplicationName.Bll;
namespace SOME.NAMESPACE.MyApplicationName.WebApplication
{
public partial class Login : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
Login1.Focus();
}
protected void btnCreateAccount_Click(object sender, EventArgs e)
{
Page.Response.Redirect("~/CreateUser/default.aspx");
}
}
}
Here is the code for default.aspx and default.aspx.cs which is throwing the parser error when viewed in a web browser:
Default.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainMaster.master" AutoEventWireup="True" Inherits="SOME.NAMESPACE.MyApplicationName.WebApplication._Default" Codebehind="Default.aspx.cs" %>
<%# MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
<div class="post">
<h2 class="title">Announcements</h2>
<p class="meta">Posted by Amanda Myer on December 15, 2009 at 10:55 AM</p>
<div class="entry">
<p>The MyApplicationName CMDB will be down for maintenance from 5:30 PM until 6:30 PM on Wednesday, December 15, 2009.</p>
</div>
<p class="meta">Posted by Amanda Myer on December 01, 2009 at 1:23 PM</p>
<div class="entry">
<p>The MyApplicationName CMDB is officially live and ready for use!</p>
</div>
</div>
</asp:Content>
<asp:Content ID="SideBarContent" ContentPlaceHolderID="SideBarPlaceHolder" Runat="Server">
<img src="images/MyApplicationName.jpg" alt="MyApplicationName Gremlin" width="250"/>
</asp:Content>
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SOME.NAMESPACE.MyApplicationName.Bll;
using SOME.NAMESPACE.MyApplicationName.WebApplication;
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Thanks!
I figured it out. The problem was that there were still some pages in the project that hadn't been converted to use "namespaces" as needed in a web application project. I guess I thought that it wouldn't compile if there were still any of those pages around, but if the page didn't reference anything from outside itself it didn't appear to squawk. So when it was saying that it didn't inherit from "System.Web.UI.Page" that was because it couldn't actually find the class "BasePage" at run time because the page itself was not in the WebApplication namespace.
I went through all my pages one by one and made sure that they were properly added to the WebApplication namespace and now it not only compiles without issue, it also displays normally. yay!
what a trial converting from website to web application project can be!
I had this issue, as I had copied a (fairly generic) webpage from one of my ASP.Net applications into a new application.
I changed the relevant namespace commands, to reflect the new location of the file... but... I had forgotten to change the Inherits parameter in the aspx page itself.
<%# Page MasterPageFile="" StylesheetTheme="" Language="C#"
AutoEventWireup="true" CodeBehind="MikesReports.aspx.cs"
Inherits="MikesCompany.MikesProject.MikesReports" %>
Once I had changed the Inherits parameter, the error went away.
Look for this:
ANY page in your project that has a missing, or different Namespace...
If you have ANY page in your project with <NO Namespace> , OR a
DIFFERENT Namespace than Default.aspx, you will get this
"Cannot load Default.aspx", or this: "Default.aspx does not belong here".
ALSO: If you have a Redirect to a page in your Solution/Project and the
page which is to be Redirected To has a bad namespace -- you may not get
a compiler error, until you try and run. If the Redirect is removed
or commented-out, the error goes away...
BTW -- What the hell do these error messages mean? Is this MS.Access, with
the "misdirection" -- ??
DGK
I had copied and renamed the page (aspx/cs). The page name was "mainpage" so the class name at the top of the cs file as follows:
After renaming the class to match this error was resolved.
For me I had all of the namespaces on the pages and none of the solutions above fixed it. My problem was in:
<%# Page Language="C#"
AutoEventWireup="true"
CodeBehind="xxx.aspx.cs"
Inherits="xxx.xxx.xxx"
MasterPageFile="~masterurl/default.master" %>
Then in my aspx.cs file the namespace did not match the Inherits tag. So it needed
namespace xxx.xxx.xxx
In the .cs to match the Inherits.
I had a similar error but not from a Conversion...
System.Web.HttpException: 'Namespace.Website.MasterUserPages' is not allowed here because it does not extend class 'System.Web.UI.MasterPage'
I was also extending the MasterPage class.
The error was due to a simple compilation error in my Master Page itself:
System.Web.HttpCompileException: c:\directory\path\Website\MasterUserPages.Master(30): error CS1061: 'ASP.masteruserpages_master' does not contain a definition for 'btnHelp_Click' and no extension method 'btnHelp_Click' accepting a first argument of type 'ASP.masteruserpages_master' could be found (are you missing a using directive or an assembly reference?)
I was not able to see the error until I moved the MasterPage to the root website folder. Once that was taken care of I was able to put my MasterPage back in the folder I wanted.
My issue was simple: the Master page and Master.Designer.cs class had the correct Namespace, but the Master.cs class had the wrong namespace.
You can always refractor the namespace and it will update all the pages at the same time. Highlight the namespace, right click and select refractor from the drop down menu.
I delete that web page that i want to link with master page from web application,add new web page in project then set the master page(Initially I had copied web page from web site into Web application fro coping that aspx page (I was converting website to web application as project))
Remember.. inherits is case sensitive for C# (not so for vb.net)
Found that out the hard way.
For me the fix was different. The corresponding aspx page has been unloaded from the project, I added that back and the error is gone.