我有一个程序,我写的是通过网站向我的一些朋友发送消息。它工作正常,但由于某种原因,我只能通过一个按钮点击事件让它无法正常工作。如果我没有第二个按钮点击SEND数据方法(这将POST数据),它将始终只是将消息发送给同一个人,尽管新的URL被加载到webBrowser URL *,但如果我有第二次点击事件一切正常。我错过了什么?
*使用调试器我在每次迭代时看到一个新的URL加载,但我确实在HTTP调试器上看到程序每次都发送到同一个URL
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
}
答案 0 :(得分:1)
尝试仅在加载网址时填充您的字段(当DocumentCompleted
事件被触发时):
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
}