How do I merge aspx pages MVC? - c#

I want to merge a sidemenu.aspx on another (Home.aspx, Phone.aspx, Car.aspx... etc) page. How do I do that?

You're a little vague, but I believe you're looking for Master pages, and ContentPlaceHolder controls. The default website when creating a ASP.NET MVC application is set up with some default ones.

In MVC, You can embed a page into another with a function in the "mvc futures" libraries
In your general page you would have:
<% Html.RenderAction("Index", "SideMenu"); %>
Which will call the action "Index" of the controller "SideMenu" that would return the view "sidemenu.aspx"
This could be added to the master page Agent_9191 is talking about!

While you probably should have a Master Page that can contain your sidebar (this is how I do things), you could also have sidebar as a View User Control, which can be rendered using the Html.RenderPartial helper method.

I had to merge webforms pages with an asp.net mvc page a few months ago. The most seamless and easiest way I found to do it was using an iFrame:
<iframe src="/path/to/file.aspx" frameborder="0" width="970" height="970">
</iframe>
It looks like its just part of the site, and the aspx postbacks/viewstate worked perfect with no other changes to asp.net required!

Related

ASP.NET C# - Showing right contentplaceholder in default.aspx

I am trying to help friend with his website which is written in asp.net. I am newbie in asp.net but I know php and mysql. Right know I am trying to figure out, how to declare which page (something.aspx) is shown in ContentPlaceHolder.
For example:
I have one master page (web.master) on which I have:
< asp:ContentPlaceHolder id="cpMainContent" runat="server">
Then I have many content pages (f.e. article.aspx, section.apsx) on which I have:
< asp:Content ID="Something" ContentPlaceHolderID="cpMainContent" runat="server">Some content
So my question is, how the website knows which .aspx file to open? If I open my friends website, I found out, that in cpMainContent is content from file section.aspx. If I make new page, like section2.aspx, how should I let the website know, that it should use the new created page?
Thank you very much for responses.
When creating a new page, you give it a name and before you press "ADD" you can check off to "Select Master Page". It will bring up a dialog box of all master pages in the project and you can select one. Easy as that!
Master pages are not accessed directly by the user. Redirection can be performed by adding the directory pathto the anchor tag to other pages( content pages associated with master pages/ not associated with master page ).

Making a asp.net web site template without using master page?

I am very new to asp.net and my job.
I was assigned a project to make a simple online order web application using asp.net c#.
The specification has been strictly defined (copied below)
Regarding the common content of the site, I need to make head, top and left(a search function)
"The design in /design/: Head.aspx Top.aspx Left.aspx"
-- Does that mean I am not allowed to use (nested) master page?
--- If so, how can I make a template without using master page?
Another option is to use the Server.Execute() method to render the contents of three separate pages into your base page if you have to use pages instead of usercontrols and still must have three separate pages rendered into one.
In the old days we did this with iis html includes.
If the head/top/left content is just basic HTML, then you could just put the HTML in a separate file and use an include link/reference in the original .aspx page.
If the content is all like a search function, then I agree with Henk's comment, create an .ascx User Control (which is really no different than an .aspx page with an .ascx extension) and then just reference that control on your .aspx page like this:
//put this at the top of your .aspx page
<%# Register src="usercontrols/YourControl.ascx" tagname="nameOfControl" tagprefix="ucControlName" %>
//then reference your control where you want it in your .aspx page
<ucControlName:nameOfControl ID="nameOfControl" runat="server" Visible="True" />
Here's an MSDN article on creating user controls: MSDN User Control
Maybe shoot the person who wrote the spec an email and ask why they have an issue with using a master page - this is kind of exactly why you would use one?!
JM

Update ContentPlaceHolder with Ajax?

Say i have a MasterPage and two subpages, that are included through the ContentPlaceHolder.
Would it possible in AJAX to update the ContentPlaceHolder to change from 1 subpage to another?
And if so, are there any problems that i may encounter by using this type of interface?
You can't use ContentPlaceHolder control, since it will not be rendered in your page.
Please use div runat="server" and use div id to load the ajax content
All the scripts in the page will work fine loaded via ajax. There are few scenarios it will break/conflict with the parent page script. In that case, you can use iframe and set a src attribute

mvc - allow asp tags in chtml pages

Is there anyway to allow asp tags in chtml MVC pages? Do I have to declare something in the view itself? I know its possible to have an aspx page in the solution, but I would like to combine controls in the chtml page.
Example:
#using (Html.BeginForm())
{
<asp:calander>...
etc
Actually Razor does not support WebForm controls, but you need to see Scott Hanselman's example if you really want to mix n match
http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx

Autopostback in c# code while keeping several textboxes and dropdownlists values

I have several textboxesand dropdownlists on my form. When a user submits the form, I want to keep several of these values on the form but I also want to make them invisible for several seconds after the form submits, so the user knows the form has been submitted. I am pretty sure you need to do an autopostbackin order to change the visibility of textboxesand dropdownlists but I do not want to do an autopostback because I will lose the fields I want to keep. Any alternatives?
Can you please expand your question, give more light to what you're dealing with, right now it's very ambiguous.
As for making textboxes / dropdownlists "invisible" you can accomplish this through Javascript:
<input type=text id=text1>
<input type=button value=hide onclick=hide()>
<input type=button value=show onclick=show()>
<script type=text/javascript>
function hide(){
document.getElementById('text1').style.visibility='hidden';
}
function show(){
document.getElementById('text1').style.visibility='visible';
}
</script>
Web Pages are stateless. Microsoft brought in ViewState in ASP.NET 1.0 to accommodate for this. What I mean by stateless is there is no consistent connection between the client (users's computer) and the server (what hosts the web page the client is looking at). THe ViewState is a medium that was created to help bridge this buy retaining values on form controls during postbacks.
If you are using ASP.NET MVC, you will lose the data in your boxes. ASP.NET MVC does not use the ViewState (thankfully). This is why it's recommended that with ASP.NET MVC you use AJAX to communicate to your server. If you would like to understand more about this, check this out:
asp.net c# MVC: How do I live without ViewState?
If you are using regular website/web application projects, your data will remain intact if you enable the viewstate (This is enabled by default).
To disable / enable viewstate see the following for ASPX headers:
<%# Page Language="C#" EnableViewState="false" %>
<%# Page Language="C#" EnableViewState="true" %>
Viewstate:
http://msdn.microsoft.com/en-us/library/ms972976.aspx

Categories