Is there an equivalent to ASP.NET "IsPostBack" in WPF? - c#

I use frame to load pages in my WPF project, is there a way to detect a page loading is the first or not? Something like "IsPostBack" in ASP.NET, I'm trying to find an equivalent to it in WPF.
I found IsPostBack is a property in System.Web.UI, should I include this namespace in my page?
I still have to use a static variable "bool SystemLoad = true", at the first load it is True and then I set it to False, so when the page is reloaded, it doesn't do as at the first load.
Thank you!

IsPostBack is not relevant to a WPF application, and since your WPF application window does not inherit 'Page', there is no way you can use IsPostBack variable from System.Web.UI.
The best you can do is to implement your custom logic as below.
private bool isLoaded;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (isLoaded)
return;
isLoaded = true;
}

Related

UWP StartBringIntoView not working in Page_Loaded

I have 3 BladeItems in another page. And I want to navigate from MainPage to that page and bring the requested BladeItem into view. But it is not working.
I first thought it was because that the page has not been loaded. So I put it into the Page_Loaded. However, it is still now working. Why is that?
private void Page_Loaded(object sender, RoutedEventArgs e)
{
TitleBarHelper.SetDarkTitleBar();
Window.Current.SetTitleBar(AppTitleBar);
UpdateTitleBarLayout(Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().TitleBar);
FullMediaControl.Update();
SetMusic(MediaHelper.CurrentMusic);
FullPlaylistControl.ScrollToMusic(MediaHelper.CurrentMusic);
if (MusicInfoRequestedWhenUnloaded)
{
MusicPropertyBladeItem.StartBringIntoView();
MusicInfoRequestedWhenUnloaded = false;
}
else if (LyricsRequestedWhenUnloaded)
{
LyricsBladeItem.StartBringIntoView();
LyricsRequestedWhenUnloaded = false;
}
}
Source Page Code is here. This page can be navigated using the "Show Lyrics" or "Music Info" item in the MenuFlyout at the right bottom more button.
And actually the FullPlaylistControl.ScrollToMusic in the code above is also not working. It just scrolls to a row in a ListView. I guess they might be the same reason.
This is the documentation for StartBringIntoView.
According to the instructions in the documentation, this method is only possible when the control is rendered on the visual tree, so you need to modify it when you call the method.
You want MusicPropertyBladeItem.StartBringIntoView() to work, you need to call it in the MusicPropertyBladeItem_Loaded event. For the same reason, you need to call ScrollToMusic when the FullPlaylistControl is loaded.
Page_Loaded only means that the page is loaded, but it doesn't mean that the controls have been rendered.
Best ragards.

How to change asp:ListView DataPager page from codebehind (c#)

Quite simple question, but I am having loads of issues with it.
protected void restorePagerNumber()
{
if (Session["PageNumber"] != null)
{
System.Diagnostics.Debug.Write(Session["PageNumber"]);
DataPager pager = searchListView.FindControl("searchDataPager") as DataPager;
pager.SetPageProperties((int)Session["PageNumber"] * pager.PageSize, pager.MaximumRows, false);
}
}
Thats what I currently have, I tried to use it before databind, after data bind, none of them seem to work. Can I actually change pager value after creating new object?
Doesnt sound logical, but I cannot access datapager without that. Is there another way to access dataPager that is in a listView and maybe another way to set its page number.
Cheers
I found a scenario that is similar to yours (https://web.archive.org/web/20210125144848/http://www.4guysfromrolla.com/articles/021308-1.aspx) and I verified that the sample application works calling SetPageProperties() at run-time.
Be sure to change the last "databind" argument in your SetPageProperties call to from False to True:
pager.SetPageProperties((int)Session["PageNumber"] * pager.PageSize, pager.MaximumRows, true);
then make sure you're calling restorePagerNumber at PageLoad
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
restorePagerNumber();
}
}
Hope that helps.

Event wont work with user control

I have a problem that I can't resolve on the server side of my project.
I'll explain:
I have a page named Global,this is ASP.NET page.
This page uses a UserControl named CateGories.
Now I have a button on this UC page,that when I press I want to invoke a function on the Global page that makes a connection with my DB.
I decided to use delegates(events)
This is the code.
Global page:
//here i add my function to the event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowCurrentTime.Text = DateTime.Now.ToString();
}
CateGories ClassCat = new CateGories();
ClassCat.MainDel += PopulateLinks;
}
//this is the function that the event will run
public void PopulateLinks(string CategoryName)
{....}
Code of the UC page (CateGories):
//delegation of the event
public delegate void Click(string ButtonName);
public event Click MainDel = null;
//function that invokes when I click a button
protected void News_Click(object sender, EventArgs e)
{
if (MainDel != null)
{
MainDel(News.Text);
}
}
Now everythig should work fine, but there is a problem, when the compiler gets to the
if(MainDel!=null)
...
It doesn't get in the function, there go MainDel is null.
I can't see the problem here, why after I insert function to MainDel, its gets null eventualy...
I'll be happy if someone can help
thanks.
Max.
I think I've encountered this problem before, when working with web applications the way I would a windows application.
The problem lies in that when the page gets reloaded, a new instance of your page class is created so any values from the last server interaction are lost.
MainDel is indeed null for what I can see.
You're creating one instance of CateGories on your Web Form and another one on your User Control. And once you check it for beeing null on the UC the reference is to the not initialized object.
One possible way to do that is creating and adding the User Control programatically to the page before PageLoad() and keeping a reference to it so you can access it's properties.
Another solution could be using Page.FindControl to find the UC and make the subscription to the event.
The method names above may be incorrect, it's been a long time without working with web forms.

ASP.NET: Controls are null when I try to change a property

Created a user control.
Within user control, I have a simple control, like a Literal, in the markup.
In the user control's Page_Load, I attempt to change a property, like Text, on control created in the markup.
Control is null.
What in the world am I missing?
(Begin rant: I am doing all these fancy things with login systems and dynamically adding/modifying controls on the fly with AJAX, etc, but I can't change an f'n static control's property! I can't even Google such a simple problem without finding something else! In the past, I had to dynamically generate the whole bloody page to avoid this idiotically simple problem. End rant.)
Page.LoadControl() has two overloads, one accepts a Virtual Path, the other accepts a Type.
Sometimes, when using the Type version overload, you get this kind of behaviour.
When possible, try to use the Virtual Path version of LoadControl.
Edit: as an FYI, this is because the actual Type of a User Control is not what you would expect it to be. ASP.NET kind of 'wraps' the Type of the User Control.
I figured out a way after analyzing the order a page is executed (http://msdn.microsoft.com/en-us/library/ms178472.aspx).
protected void Page_Init(object sender, EventArgs e)
{
literalGameName.Text = myGame.Name;
}
Init happens after controls have been made, but not after the page is done loading control-stuff.
I know this is an old one, but wanted to chime in on how I fixed a similar issue I was having. I have a usercontrol with an asp:Panel on it that I was trying to set the visibility off during the Page_Load, but the control was coming up null. I then noticed that my Page_Load definition looked like this:
protected void Page_Load(object sender, EventArgs e)
{
_myControl.visible = true;
}
When in fact, I needed the definition to look like this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_myControl.visible = true;
}
Doing the override fixed the issue.

In Silverlight, what's the difference between UserControl_Loaded and Page_Loaded?

I'm trying to write a silverlight application that takes in InitParams and then uses those InitParams to make a change to the Source of the MediaElement on the page. I'm trying to figure out the proper place to put my code.
I watched Tim Heuer's excellent video on InitParams, but in the video (which was for Silverlight 2), it shows the following on the Page.xaml.cs:
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
I don't see Page_Loaded when I open MainPage.xaml.cs, and I'm wondering if that was automatically created in the Silverlight 2 SDK and left out of the Silverlight 3 SDK. Or perhaps Tim added that in his video manually.
I find that I can go into the opening UserControl tag of MainPage.xaml and add Loaded="<New_Event_Handler>" which creates the following in MainPage.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
By default, there's also the following in MainPage.xaml.cs, which is run during the Application_Startup event in App.xaml.cs:
public MainPage()
{
InitializeComponent();
}
I need to figure out where is the best place to insert my code to change the Source on my MediaElement in my xaml. Should I put it in MainPage? Should I add the Loaded event handler and put it into UserControl_Loaded? If it's supposed to be Page_Loaded, where do I find that in Silverlight 3?
Any help would be much appreciated.
"UserControl_Loaded" and "Page_Loaded" are just method names and the names don't matter (you could name the method "Foo" if you wanted). What makes these methods do anything is the fact that they are attached to the Loaded event on the UserControl (which is what you did when you edited the MainPage.xaml file).

Categories