How masterpage get the current page from masterpage by itself - c#

I am writing a website with Visual Studio 2008, C# 3.5 and ASP.NET MVC 2. I put the navigation bar in the masterpage.But there is problem that I will not know which button is needed to be highlight(current page) in the navigation bar.
I want get the current page that need to be highlight by masterpage self (not through the content page).And I don't think it is a good way to get the current page by url string.Because I have no idea about the final url is.
So how can I do this?

I guess you could set a ViewData["currentPage"] value in the Action methods, this would allow you to process that ViewData in the Masterpage. That is, however, off the top of my head and I'm sure there's a more elegant way to do this.

When you click the link in the navigation bar (in the master page) that should be triggering a controller action that will decide which view content page will be shown. In that action method you can (as Lazarus suggests) add some data identifying the current page to ViewData that will be picked up by the master page to modify the navigation bar.

Related

blazor not changing the page when clicking on a link

I am migrating my personal blog to Blazor, and I have a problem. I was developing the "next post" functionality and blazor (or the browser) is not redirecting the user when a link is clicked. the post are "the same page" under the next route #page "/post/{Url}
For example the user is in domain.com/post/foo and in that site there is a link to domain.com/post/bar.
It is a simple link, like the next:
<a href="post/bar" title="#Post.Title">
#Post.Title
</a>
When the user clicks on the link the url for the browser changes, and the location of the page moves to the top, but the page content is not changing.
I noticed that this behaviour is happening when I click from a post page into another, but is not happening from the "index" of the site (/) or when I click the tags on the post, which links to domain.com/tags/{tag}.
one thing to notice is that if I select the url in the browser and click enter or refresh the page it actually goes to the page i clicked on, in this case domain.com/post/bar
My question is, is this actually expected behavior or is it a bug? if it is expected behaviour, is there any way of making the url to actually go to the clicked url and not stay in the same page.
I am using net5.0 if that matters.
Thanks.
As an add on to #enet's answer, you're probably fetching the post referenced by the Url property in OnInitialized/OnInitializedAsync. See the last long paragraph in #enet's answer.
One way to address this is:
place the code to load data in a separate method say LoadDataAsync
in SetParameters/SetParametersAsync detect if the data reference - in your case Url - has changed in , and if so call LoadDataAsync.
The key here is understand component lifecycle and getting the code in the right place.
Put breakpoints in on OnInitialized and SetParameters and watch when they get hit when you load and do navigation.
when I click from a post page into another
post in domain.com/post/foo and domain.com/post/bar is definitely not a page but a literal path segment.
I've created a routable component named Foo:
Foo.razor
#page "/post/foo"
#code {
}
Foo is a routable component; that is, it is a page, and can be accessed by typing the url in the address bar of your browser, using an anchor element or the NavigationManager. You may embed it in a parent component as well.
post is, as I've said above, a literal path segment
But if you insist that post is actually a component page, then I guess it may look something like this:
Post.razor
#page "/post"
#page "/post/{Param}"
<a href="post/bar" title="Title">
Title
</a>
<div>#Param</div>
#code {
[Parameter]
public string Param {get;set;}
}
Now, when you type in the address bar a url like this:
https://localhost:<port-number>/post
You'll be navigated to the post routable component (page)
If you click on the anchor element, the url in the address bar will change to https://localhost:<port-number>/post/bar
bar is not a component; it is a parameter. The address bar changes, but you seem to be in the same post component, the data or content, however, do changes...
Note that when you click on the anchor element, you are being navigated to
a new Url ("post/bar"), but rendering engine does not create a new instance of the Post component in order to render the page, but instead it treats it as the same page with changed parameters. Thus it looks like no navigation has taken place, and nothing has changed, except the url in the address bar. I'm not claiming that your issue is the same, but it may stem from similar code usage.
Note that since Blazor does not create a new component instance, but uses the existing one, the OnInitialized(Async) pairs can only run once... So you'll have to use the SetParametersAsync method (" on initializedAsync is getting the "nextPost" information from the backend, but its just a plain http cal")
All the above was intended to ask you to do the simplest thing and provide the route templates........
Is bar a component?
Is foo a component?
Answer withdrawn as it breaks some component parameter recommendations by ASPNetCore Team - see https://github.com/dotnet/aspnetcore/issues/24599

Need to render form in footer only on home page. Asp.net MVC

I have a _layout.cshtml page that renders the header and footer for every page. In my _footer.cshtml I have a newsletter form. As of right now, the newsletter form is rendering on every page. I need it to only render on the home page footer.
My thought for a solution is just using an if statement that checks if we're on the home page:
#if(Request.RawUrl == "/")
{
Newsletter form
}
My problem with this is that whenever I try to get the url, it's the same on every page.
For example:
the url might be https://something.com/checkout, so Request.RawUrl gets /checkout. But when I navigate to https://something.com, Request.RawUrl still gets /checkout. How can I get the actual path of the page?
I've tried using just about every request.url option I can find. None of them get the actual url path of the page after being redirected to a new page. Just whatever the original one was.
The best way would be to use a ViewModel instead of querying the Request object.

asp.net redirect to another location than set in navigateURL

in my asp.net website I have menu control and there are those items:
<asp:menuitem ... navigateURL="~/Admin/admin.aspx">
<asp:menuitem ... navigateURL="~/Admin/search.aspx">
I want force the asp.net think (or display in web browser adress bar), that I am on admin.aspx, but actually be redirected to search.aspx. Is it somehow possible ?
Thanks.
Doing that with the markup you showed, no, it's not possible. You'll need to either use an <iframe> setting the src property via javascript or you do it from code-behind using Server.Transfer as opposed to Response.Redirect.
URL rewriting sounds like it's what you are after:
http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Failing that you might be able to build some custom logic into your page and use Server.Transfer to change page, without changing the URL.
You can setup customized routing if you would like, but that will still display "search.aspx" in the address bar. If you want to fake the admin.aspx as search.aspx, then add your search controls in to the admin.aspx page and hide the admin controls. If you are trying to "spoof" admin.aspx, like you would an e-mail address or something, then I would direct to not StackOverflow.

Getting Child page title from user control on Master page

I have a user control in the Master page. In the code behind of that ascx, I want to get the title of the page. The title is being set in the head section with tag in the child pages.
Try this
var obj = this.Parent.Page;
var title= obj.Title;
Have you tried Page.Title?
However, I think the head tag needs to be ran server side to use this
I would think Page.Title would be enough. If not, you have to ride up through the object model a bit until you get to the page. Both solutions have been detailed above. The only variation may be if the ascx is set on the master page rather than the page. Worst case here is getting the title in the master page and feeding to the ascx as the page is rendered.
Now, an understanding of why this gets a bit confusing. Most people think the page sits on the master page. But, technically, the master page is set up as a control on the page. This is largely to avoid a complete rearchitecture of ASP.NET as master pages were introduced. This means the page is requested and starts to render. Then the master page linked tags are hit and that "control" is rendered, etc. In some cases, Microsoft has provided easy short cuts, in others, you have to navigate and the navigation is upside down from many people's expectation.

Popups with complex functionality using jQuery

I am using jQuery to simulate a popup, where the user will select a series of filters, which I hope to use to rebind a ListView in the original window.
The "popup" is opened via an ajax request and the content is actually a diferent aspx file (the rendered output is injected into a div that acts as the popup).
I have another ListView in this popup, and it has pagination.
My problem is that since the popup is in reality html content inside a div in the same page, when I try to paginate, the whole page postbacks and is replaced with the aspx that has the filters.
How can I fix this?
I tried using an update panel to contain the ListView but it didn't work.
$("div.yourthingie").hide();
Will hide the part you want to show :) Instead of generating the popup on the fly, leave a small part already made, and hide it in the begining, when you need to show, unhide and add the information you need to.
Hope it helps
Either get rid of the HTML "crust" and just produce the <div> with its contents, or use an IFRAME.
First, let's think through what is happening. When you submit the original page, you are taking a "normal" Request/Response trip to get the code. On the page is a JQuery AJAX bit that fires off what is essentially a modal dialog. The desired effect is the user plays with the new page until they have figured out their filters and submits back. The problem is this "modal page" loses information when someone paginates.
The solution to this is fairly simple, in theory. You have to store the "filters" in the popped up page so they can be resent, along with pagination information. OR you have to cache the result set while the user paginates.
What I would do to solve this is create a static page that has the "filters" in place and work out the AJAX kinks separate from having the page post back to a parent page. Once you have all of the AJAX bits working properly, I would then link it into the popup routine and make sure the pagination is still non-problematic. THe final problem is creating a JavaScript routine that sends back to the parent page and allows the parent page to send its JQuery bits back to the server.
I am not sure about the HTML DIV part of the equation and I think you can solve the problem without this solution. In fact, I believe you can make the "modal popup" page without invoking AJAX, if it is possible to either a) submit the filters to apply via the querystring or b) fake a form submit to the second page. The query string is an easier option, but it exposes some info. Faking a form submit is not that difficult, overall, but could be problematic with a popup.
I am just firing off some ideas, but I hope it spurs something for you.

Categories