I have a master page which is shared between about 20 views. In just one view, I need to make one small adjustment to the master page (I need to hide a textbox).
How can I include css or javascript in my view to acheive this?
Or is there some clever trick like including a conditional <% if (View.Name = "blah") { ... } %> that I can stick in my master page?
Thanks for any hints.
As you said you can write a JavaScript code to achieve your goal and include it only in one view. In one of my projects I do the same: I have jQuery AJAX request only at one view, so I just used script tags to include it
Related
I am very new to ASP.NET and I am trying to create a website for learning purposes. I was developing the website in PHP where I used to include another PHP file in the main using <?php include('static/nav.php'); ?>. In the same way, I am trying to include another aspx page to the main aspx page for top_nav, side_bar, and footer. But I am not able to do so. Is there any way to include it ? which I am not able to find it.
I have seen somewhere this code
#Include virtual="/menu.aspx" but its not working.
For the main menu bar etc.?
This is normally done without any code or markup on your part.
The concept involves what is called a master form, and then a child form.
So, try creating a new project, and choose ASP.NET frame work).
On the last page, choose web forms - you can un-check https setting
The result is then this:
Notice the menu bar. That is a bootstrap menu bar.
So, now how do we add a web form, say Hello Word?
When you add a web form, you can choose the "master" form that holds that menu bar etc.
So you go like this:
You now get this:
So, our markup is this:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master"
CodeBehind="helloWord.aspx.vb" Inherits="WebWithMenu.helloWord" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Hello Woorld</h2>
</asp:Content>
And thus this is the result:
So you don't' really have to write code - your main "master" page will be part of each new web page you create (as long as you choose new form with master page).
And we can add HelloWord to the menu:
Just open site master, add this:
<li><a runat="server" href="~/Contact">Contact</a></li>
--->this <li><a runat="server" href="~/HelloWord">Hello World</a></li>
Now, we get this:
And if you click on the menu bar, then your HelloWord page will show. So, you can quite much see that we did not have to really write much of anything, and we have that main menu bar that exists for all pages.
this WHOLE process took less time then to write this post!
Now, are there other ways to include content or say another web page?
Well, there is of course iframe - but I think like most, we try and avoid those, but that does let you drop in another page.
There is also what we call "user control"
A User Control is a reusable page or control with an extension of .ascx and created similar to an .aspx page but the difference is that a User Control does not render on its own, it requires an .aspx page to be rendered.
User Controls are very useful to avoid repetition of code for similar requirements. Suppose I need a calendar control in my application with some custom requirements in multiple pages, then instead of creating the control repetitively you can create it once and use it on multiple pages.
Here is a tutorial:
https://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-user-control-in-Asp-Net/
And you can say start to use say jQuery and REALLY nice is jQuery.ui. jQuery.UI is nice since it will let you load contents of a WHOLE page into the current page, and say pop that up as a dialog.
So there are several choices here. But for a top most menu bar etc., I would adopt the master + child design approach.
I'm currently converting a ASP.NET webforms solution to MVC.
In webforms we besides the site.master we have master pages where we keep the common code and head imports for each section. So a normal page inheritance goes like so:
Site.master -> section.Master -> page.aspx
In MVC I'm unclear as to where to place the code common to a section. Is splitting the section content into partial views the only solution? So it becomes?
Layout.cshtml -> page.cshtml -> any-number-of-partial-views
To give an example, if I need a single CSS file to be shared across all 10 section pages, I can put it in a partial view and render it on each page but doesn't feel incredibly efficient. Or maybe I just need to get my head around to this new way of working.
You can use sections in MVC as well.
In your _Layout.cshtml you might have something like this:
#RenderSection("testSection",required:false)
And in your child view you can specify what HTML should be rendered in this section or you can leave it all together because required has been set to false:
#section testSection{
<h1>Test</h1>
}
Partial view is your big friend if you would like to reuse the code several places.
You can put partial view inside Views > ControllerNameAsFolderName > view pages (put here) to use only in same controller views or even inside Shared folder to use globally.
section is another option, this is like UserControl (in web forms). create section reference #RenderSection("sectionName", required:false) on Layout page and use it on view pages like
#section sectionName{
<div>content goes here</div>
}
Assume you want to put something (like meta infos) inside <head> from view pages, in this case you can create a section in _Layout.cshtml page and then use that section on view pages.
This way you can place your code at certain location in DOM structure from any page. section has a great feature that allow you to mark it as required:false/true.
I'm making a website that has a menu on all the pages that needs to be changed frequently. Right now I'm copying the code of the menu in all the pages, and so, if the menu needs to be changed, I have to open each page and change the menu there.
Is there any way by which I could define a function in a file somewhere that just writes the menu code using Response.Write() and then keep calling that function on all the pages that need the menu? That way whenever I'd need to make changes to the menu, I'll only make it in the function definition.
Have a look at a user control or master pages. Be more elaborate, so we can help you better.
It sounds like you should consider using a user control for the menu.
Master page is the perfect candidate.
Add the menu in master page.
You can also create a user control for this.
But if you want a function for doing a common task through out the site then create a class
and add the function is that class. Create an object of that class and call the function
anywhere in the site.
If you define a Menu in your Master Page it should refelect in all child pages.
All you need to do is create a Master Page and define your Menu in that.
After that you should create your child page using option >> WebForm using MasterPage
Apart from that for any other functions which are in Master page you can call them in your child page like this:
You can call method inside a MasterPage like this:
var master = Master as MyMasterPage;
if (master != null)
{
master.Method();
}
In my website, I have created a masterpage and attached all of my pages to it.
My masterpage structure contains a header and a footer. On the left it has a treeview control, which i have attached to all my pages, and on the right there is a contentplaceholder to show the content of respective pages.
My problem is that when I click any link in the treeview it refreshes the whole masterpage and open the respective page. I wish to avoid this refresh. Means it should show the contents of page on right side contentplaceholder without refreshing the whole page.
I have seen people suggesting to use iframes. But for using iframes I shall have to restructure my website. Is there any other solution than iframes and with minimal changes to the work that I have done?
You will probably want to look at using AJAX to stop this from happening. You will want to read up on using an UpdatePanel. Below are some good articles that goes over this:
http://geekswithblogs.net/ranganh/archive/2007/05/11/112405.aspx
http://msdn.microsoft.com/en-us/library/bb399001.aspx
You also have the option of using jQuery to handle your AJAX calls. While I typically prefer the use of jQuery when using AJAX, I am not sure I would use it in your situation. If you would like to look at what it offers take a look at these links:
http://api.jquery.com/jQuery.ajax/
http://sixrevisions.com/javascript/the-power-of-jquery-with-ajax/
You could put the content you wish to change inside an asp:UpdatePanel that way that will be the only thing that is repainted (it uses AJAX under the hood):
http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx
The master page class derives from the UserControl class and the master page is like a child control. So we can say that master page is not a true page, when a page loads we can notice the navigation URL in the address bar is the content page's but not the master page! so we cannot refresh a content page without refreshing master page.
there is one way to avoid flickering the page by adding the code in < Head > section in the masterpage.
<meta http-equiv="Page-Enter" content="blendTrans(Duration=0)"/>
<meta http-equiv="Page-Exit" content="blendTrans(Duration=0)"/>
I have a master page(MyMasterPage.master) with two content place holders. I wanna show another aspx page(MyHeader.aspx) in the 1st content place holder of a content page(MyContentPage.aspx) that uses my master page.
You should be using user controls for reusable components of a pages. Create a Header.ascx file instead of a .aspx. You can then drag that into your ContentPlaceHolder from the solution explorer when in design mode.
http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
I would use UserControls. MyHeader.ascx.
I echo the prior commenter's suggestion to use a usercontrol if possible.
If you really want to keep your existing aspx page; you might want to take a look at using an IFrame to accomplish this.