Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
C#
if (searchSubmit == false)
            {
                webBrowser1.DocumentCompleted +=
                    new WebBrowserDocumentCompletedEventHandler(OnDocumentCompleted);
            }
            else
            {
                webBrowser1.Stop();
            }


        }
            
     private void OnDocumentCompleted(object receiver, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlElementCollection avCollection = webBrowser1.Document.GetElementById("filterAvailability").GetElementsByTagName("option");

            List<HtmlElement> avList = new List<HtmlElement>();
            foreach(HtmlElement avItem in avCollection)
            {
                avList.Add(avItem);
            }

            HtmlElement avElement =
                (HtmlElement)avList.Where(avOption => avOption.GetAttribute("value").Equals("1")).SingleOrDefault();

            if (avElement.GetAttribute("value").Equals("1"))
            {
                avElement.SetAttribute("Selected", "1");
                avElement.InvokeMember("click");
            }

            bookCollection = webBrowser1.Document.GetElementsByTagName("button");
            foreach (HtmlElement curElement in bookCollection)
            {
                if (curElement.GetAttribute("id").Equals("searchSubmit"))
                {
                    curElement.InvokeMember("click");
                }
            }
            searchSubmit = true;

         if (searchSubmit == true)
         {
             webBrowser1.Stop();
         }
        }

The problem is the even is looping all the time even if I set a bool variable. I noticed this because the page keeps refreshing since there is the searchSumbit Click invoke member. how do i make the code run only once?.

Thanks


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 4-Nov-13 0:05am
v2

1 solution

Probably, you don't want to add a new handler each time the user presses the button, as you appear to be doing now.

You do realize that "+=" for an event adds a handler to the chain? And that if you have the same handler in the chain 8 times, it will get called eight times every time the event is fired?
Add it once, or remove it when you don't need it anymore.
 
Share this answer
 
Comments
slayasty 4-Nov-13 6:23am    
mm i dont seem to understand what you mean. what do you mean add it once or remove it? the bool searchsubmit doesnt have anything to do with the attribute searchsubmit. i just named the bool that way coz it seemed logical to me. I think its better i name the bool something else to reduce the confusion.

thanks
OriginalGriff 4-Nov-13 6:39am    
What I mean is that your code appears to either add another handler to the chain or stop the browser in mid navigate.

If you do this twice with "searchSubmit" false, the DocumentCompleted event will cause the handler to be called twice each time it occurs.
slayasty 4-Nov-13 7:14am    
i see. even if i remove the if condition it will still loop tho :/
OriginalGriff 4-Nov-13 7:25am    
Where is it looping, exactly? What does the debugger say?
slayasty 4-Nov-13 7:46am    
at the end of the event, the eventArgs is empty. is this normal?

the whole event receiver is looping. regards to what is the debugger saying, what exactly should i look for?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900