WebBrowser does not change - c#

I'm making an application in C# which, depending of a variable, shows one web page or another.
When I push a button, the program load the userName and the webBrowser should show a different web page. Here is my source code:
private void button1_Click(object sender, EventArgs e) {
string url = "http://www.url.com/" + userName;
webBrowser1.Navigate(url);
webBrowser1.Refresh();
}
The problem is that, when I push the button a second time with a different variable, the web browser reloads the same web page.

I think it's because of the webBrowser1.Refresh(); you don't need it and i think you are pushing the button 2 consecutive times with different values and it gives you the impression that it's loading another page but it isn't. try to remove that line and add an event to the Navigated method of your WebBrowser object in order to obtain a feedback when the browser is done loading the page.

I have solved my problem. I had set the property AllowNavigation to false, so, when I tried to change the web page, it didn't allow me to did it. Anyway, I needed to remove the Refreshcall to make it work.

Related

How to notify the page when GoBack() happened?

Here is what my app does:
The first page can navigate to the second page, and the second page displays a list of data. The user can choose one of them then the app will bring the data back to the first page.
Sounds easy, but I'm confused with the Windows Mobile Navigation Model.
The first page navigates to the second page, using this code:
this.Frame.Navigate(typeof(SecondPage));
and the second page uses the code below to go back:
this.Frame.GoBack();
How could the first page know if the second page disappeared? I want to update the UI on the first page after the second page disappeared.
Now, I used a static class to keep the data that user picks, but I have no idea when should be the right time to update the first page.
Is there any way to get an event or notification?
This is quite simple, since UWP does this for you. I noticed you're not using MVVM, so you can simply override the OnNavigatedTo event in your page. This event is triggered when navigation to your page is completed (and thus the second screen dissapeared). Simply check for NavigationMode.Back to confirm you're returning and not navigating forward.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.Back)
{
// coming from 2nd page, so refresh your data
}
}

Immediately redirect to another page in WPF

I have a page that several other pages navigate to. However in some circumstances the user should not see this page, so I want to send them to another page instead.
Rather than update the rest of the calling code, I just want to change this page to handle it.
public MyPage()
{
Loaded += MyPage_Loaded;
InitializeComponent();
// Other stuff
}
void MyPage_Loaded(object sender, RoutedEventArgs e)
{
if(condition)
NavigationService.Navigate(someUri);
}
Since the NavigationService isn't available in the constructor I have to hook up to the Loaded event and do the redirect there. The problem is that the page has already been loaded and displayed to the user. There is also a slight delay before redirecting the user.
Is there a better way to do this where the redirect is seamless?

Clicking multiple times on the same redirect button in ASP.NET causes weird results

When I click multiple times on a button that performs a server-side redirect using ASP.NET, thing can get weird. Sometimes I get ViewState errors, other times the page is only partially loaded.
The code for the OnClick event of the button is simple:
HttpContext.Current.Response.Redirect(targetUrl);
If I have something like:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Something();
}
}
in the page I'm redirecting to, the Something() function won't be called if the button is pressed more than once in rapid succession.
Is this normal? What could be the cause of these weird issues when pressing buttons multiple times quickly?
Ok as far as i know i could explain the weird result as below:
the first time you click the button, server will do the redirect and since its the first time you hit the page from another page it will not be a postback, but the second time you click the button in the server you will be already redirected and it will see the request as postback because on the server your already on that page, since its redirected you in the first time, at the end you will get the response of the last click which will be a postback in the server.
to avoid this issue, you should make a loading panel appear on the button, or disable the button before you go to the server using javascript.

WebBrowser class wont submit data to webform untill main thread is dead

I'm having trouble my application which is supposed to write text in some textboxes on a website and click OK button. But then the problem comes, the site loads a new page and you have to confirm the previous commands by clicking a new OK button. What I need help with is for my C# code to execute the commands as they are called and no do them once the running thread is dead.
Example of my code:
private void startScript_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("login_box").InnerText = "NAME";
webBrowser1.Document.GetElementById("password_box").InnerText = "PASS";
webBrowser1.Document.GetElementById("login_button").InvokeMember("click");
//This is where i want the first texts and click to be submitted so it then
//can click on the confirm button the the next page
webBrowser1.Document.GetElementById("confirm_login").InvokeMember("click");
//Submit the confirm click
} //This is where all the information given in the code is actually given/executed to the browser
So to clear up the question. What I need to do is; give the information to the browser directly when the webBrowser1.Document...; is called. A command to submit would also work (webBrowser1.SubmitData) or something like that.
The WebBrowser navigation, which is taking place when you click the button, is an asynchronous operation, it doesn't complete instantly.
I can't guess what your code is doing immediately after InvokeMember("click"), but if you're trying to access WebBrowser.Document right away, or doing something like Thread.Sleep, that won't work. It worked when followed with MessageBox.Show, because MessageBox.Show runs its own modal message loop.
You need to handle WebBrowser.DocumentCompleted first and then access the document. There are a few ways of doing that, here is one of them.

Client Rendering Problem after 'page.' calls

I have tried, 'PreviousPage', 'PreviousPage.IsCrossPagePostBack' 'Page.previousPage', page.title
It causes the client to stop rendering the page after this line.
simple example
protected void Page_Load(object sender, EventArgs e)
{
response.write("I can see this");
string test = PreviousPage.IsCrossPagePostBack.toString(); //Any page call Causes client rendering to freeze
response.write("But i cant see this");
System.Windows.Forms.MessageBox.Show("However i can see this,proving that the server is still running the code");
}
Anybody Please, any ideas?
ANSWER
Well it ended up it was something stupid. code smell over.
The button i was using to fire the PostBack had a handler that fired to redirect, i just deleted the handler, keeping the PostBackUrl setting and magic.
Have you checked PreviousPage for null?
From msdn:
The PreviousPage property is a null
reference (Nothing in Visual Basic)
when the user requests that page
directly from the server.
Also - MessageBox in a web form, not a great idea... perhaps use the inbuilt trace.axd

Categories

Resources