Same webpage always loads with webBrowser - c#

I have a program that I wrote for sending messages to some of my friends through a website. It works fine, but for some reason I can't get it to work correctly with just one button click event. If I don't have a second button click for the SEND data method (whichs POSTs the data), it will always just keep sending the message to the same person, eventhough the new URL is loaded into the webBrowser URL*, but if I have that second click event all works fine. What am I missing?
*Using the debugger I see a new URL load with every iteration, but I do see on the HTTP debugger that the program is sending to the same URL each time
private void button1_Click(object sender, EventArgs e)
{
ListBox();
}
private void ListBox()
{
//gets name from ListBox
GetData();
}
private void GetData()
{
webBrowser1.Navigate(inputURLID);
//SendData (); Always sends to the same person if I call from here, so I made a second button click and it works fine
}
private void button2_Click(object sender, EventArgs e)// works fine like this
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}
....
private void SendData()// always sends to the same person if I just do it like this
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}

Try to populate your fields only when the url has been loaded (when DocumentCompleted event is fired):
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}

Related

web browser submit button not invoking second time c#

Am trying to submit a webform pragmatically on button1 click. In first attemp ie after launching my webbrowser from visual studio it automatically submit the form but again clicking on button1, webpage loads but submit button is not invoked. While debugging its shows that the line of code executed but no action takes place.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("xxxx");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Url.OriginalString.ToString() == "xxxx")
{
if(webBrowser1.ReadyState==WebBrowserReadyState.Complete)
{
HtmlElementCollection doc = webBrowser1.Document.All;
foreach (HtmlElement ele in doc)
{
if (ele.GetAttribute("name").ToString()== "username")
{
ele.SetAttribute("value","xxxx");
}
if (ele.GetAttribute("name").ToString() == "password")
{
ele.SetAttribute("value", "xxxx");
}
if (ele.GetAttribute("classname") == "btn")
{
ele.InvokeMember("click");
}
}
}
}
textBox1.Text = webBrowser1.DocumentText;
}
You only want to setup the DocumentCompleted event for the web browser once. In your code you keep adding to the event chain each time the button is pressed which is not the behaviour you want.
You want something like (pseudo code as haven't got VS with me):
// When the form/parent loads bind the event ONCE here.
public void FormLoads()
{
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
// Just navigate here and the event will still be raised
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("xxxx");
}
Its Due to your webBrowser1_DocumentCompleted Event is not working when your click on Button.
Just Create one method
Private Void Submit(); and here put your code which you want to perform on click event and call this method from two location one is at button1_click and another webBrowser1_DocumentCompleted so that your code is run at both events.

Process.Start to open multiple web pages

I am currently using a simple button to open a webpage.
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
}
What I want to do is get it to open 3 pages at once with the one click and I am having a hard time getting it to work. I have tried multiple Process.start lines
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
System.Diagnostics.Process.Start("http://www.gmail.com");
System.Diagnostics.Process.Start("http://www.stackoverflow.com");
}
and even adding multiple pages into the handler.
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca","http://www.gmail.com","http://www.s tackoverflow.com")
}
It will only open the last page in the list in both cases. Any ideas?
If IE is open, your code works fine and opens each link in a new tab, if not, I was able to make it work by making the app wait for 1 sec before calling the second page to open:
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
System.Threading.Thread.Sleep(1000);
System.Diagnostics.Process.Start("http://www.gmail.com");
System.Threading.Thread.Sleep(1000);
System.Diagnostics.Process.Start("http://www.stackoverflow.com");
}

Running Page Load multiple times

I have a user control on an aspx page that contains filter fields and return a WhereClause. The user conrol will raise an event called FilterApplied when Search is clicked.
On my main form, I add the control with:
<uc1:Filter runat="server" ID="Filter" />
<br />
In my code behind I have:
protected void Page_Load(object sender, EventArgs e)
{
//Register event when filter is changed.
this.Filter.FilterApplied += new EventHandler(this.FilterApplied);
if (Page.IsPostBack)
{ //Do some things that take long
}
}
protected void FilterApplied(object sender, EventArgs e)
{
//Reload the page to refresh the graphs.
Page_Load(sender, e);
}
Problem is:
When I click Search on my user control, the Form_Load runs twice. Once because it is reloaded and then another time because I call it from FilterApplied. If I don't call it from FilterApplied, then the whereclause is still empty.
How can I ensure the Page_Load only run once when Search is clicked?
Your problem lays in multiple registering for FilterApplied event. Each time you call the Page_Load method, you subscribe to this event again. Here is a really simple example of what you are doing, written in WinForms with one button on the form, just to point out your problem:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int numClicks = 0;
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += button1_Click;
this.Text = numClicks.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
numClicks++;
//try uncommenting and commenting next line of code, and observe the difference:
//button1.Click -= button1_Click;
Form1_Load(sender, e);
}
}

c# web browser url update

I have created a web browser in c#
this was what i get when i opened my web browser and typed google. Then i searched google for something
the result was like this
But the url wasn't updated in address bar. How to update the address bar when user click on a link on any website in my web browser
In the first image the url was google.com
In the second image url was https://www.google.co.in/#hl=en&output=search&sclient=psy-ab like that some thing but it wasn't updated
You must update the textbox on top with the URL of the WebBrowserControl, using the webBrowser1_Navigating event.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
textbox1.text = webBrowser1.Url.ToString();
}
Check http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser_events.
I think you can use Navigating event to detect when user starts search or navigates to another page.
The Form_Load must contain this:
private void Form1_Load(object sender, EventArgs e)
{
web = new WebBrowser();
web.Navigated += web_Navigated;
}
and this function:
private void web_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
textBox1.Text = web.Url.ToString();
}

Why does my list reset when I press a button?

I have a page where a instructor inserts a student's id into a text box and then submits it. It should then come up with a list of all the students that have been added. But every time the submit button is pressed, the list gets cleared and only the last student added is displayed.
public Service1Client ws = new Service1Client();
public string adress;
public List<ServiceReference1.User> lu;
protected void Page_Load(object sender, EventArgs e)
{
if (lu==null)
lu= new List<ServiceReference1.User>();
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.User u= new ServiceReference1.User();
u = ws.ShowUser(int.Parse(TextBox2.Text));
if (!IsContained(u))
{
lu.Add(u);
TextBox1.Text += u.FirstName;
}
}
Is there a way to solve this? Or must I send the information to the page via a query string?
Each HTTP request gets a new instance of your page.
You should store the list in ViewState to persist across requests.

Categories