InvokeMember("Click") as it come in div - c#

in webbrowser control a booking website. when i search A To B it give list of items with price. (price is a radio button) when i click radio button. a new div created after some process a submit button named "submitButton0" comes on webpage. on same web page. website location not changed. i want to invoke click of submitButton0.
problem is that. webBrowser1_DocumentCompleted Event has been done. but that time submitButton0 not in page. pragmatically readio button has been click. after some process a submitButton0 comes on page. as submitButton0 comes i want to invoke its click quickly.
what is the solution of it?

but that time submitButton0 not in page
Which probably means that the buttons are added by Javascript. There's no way to synchronize with script execution, you need to poll. Enable a timer in the DocumentCompleted event handler and in the Tick event check if the DOM was changed enough to allow calling InvokeClick() reliably.
Do check if the site owner makes a web service available. The fact that buttons get added later is a fairly strong hint that the site's TOU does not permit automation. And a service makes your app much more resilient to web page changes. The DOM polling trick is pretty much guaranteed to break sooner or later.

Related

ASP.Net Only 1 Form Restriction: Should I be using a webpage for everything?

I am forced to use Web Forms in my project, and sadly, Web Forms only allow - If I may - "Strict" Websites to be created.
Whenever you need a Button you need to put it in a form, and then you need another button which has nothing to do with the previous button, and you can't have 2 forms,
And the idea of putting a DIV that fires a server side (C#) method is kind of difficult, okay it may be easy but all I have found are "tricks", not an "official" clean way.
So I have this idea of making a webpage for each action in my websites.
For Example:
Let's say I wanna click on the ARROW that raises the rating of this question, I would put something like this.
HTML
Rate Up
And Some CSS Codes to make it look like a beautiful button...
Okay now this will take me to a page called Rating.aspx with 2 parameters, the first parameter is the ID of the question that I would like to raise its rating, and the second parameter is either UP (+rating) or DOWN(-rating).
On the Page Load method of Rating.aspx, I would update the database, then redirect to the question page.
This will work perfectly, BUT, is it a good approach? is it professional? (put in mind that there will be many actions to preform like that...)
With ASP.NET you better use server controls. Better way of implementing that is using or , that actually renders your anchor tag. But you can attach OnClick event handler to this control (link button) so after clicking there would be automatic POST to server. The same page cycle for the current page will take place (this is called PostBack) and your attached event handler will fire, where you can actually make changes to the database. So you don't even need to create any other pages for tasks like this. Every server control has specific set of events like OnClick for buttons or OnSelectedIndexChanged for dropdown lists. You can even create your own controls or derive from existing ones and create your own events.
Take a look on following links for more information:
Button Click
Event Handling in ASP.NET
ASP.NET Page Life Cycle

Button click event won't fire on first click, but always fires on subsequent clicks

I have a large-scale asp.net project that I'm working on (new to ASP.NET), and I have a problem with some dynamically added buttons not firing their click events on the first click. What's weird is that this doesn't always happen; sometimes they work just fine on the first click, and other times if I run the project and follow exactly the same steps the button won't work on the first click.
Here's some background...
The page that's problematic is used to deploy different versions of some content to clients. The page has an UpdatePanel that contains a bunch of tables, and only one of them is visible at any given time. Each table represents a different "screen" of a wizard-like workflow. So when the page is loaded, the first table is visible and all others are not, and the first page has a radio button list with deployment types and a Continue button. When the Continue button is clicked, some data is loaded for the next "screen" and the first table is made invisible and the next table is made visible.
So now that you have some background, on to the problem...
One of these tables has an empty Accordion on it, and when the previous "screen"'s Continue button is clicked, the Accordion is populated dynamically with data from the DB; there's one pane for each major version and each pane has a table in it with one row for each minor version. Each of these rows has the Version Number, the Deployment Status, and an Action button that lets the user Deploy the version, or if its already deployed they can Redeploy. Anyways, these action buttons are the ones that I have a problem with where they sometimes don't work on the first click. Like I said, the buttons are created in the click event from the Continue button on the previous "screen", and I set the ID of each button, and link the event up to the click event at the time the button is created. I can debug over this and see the event handlers being added with no problem, but then when I click the button the first time (and this is actually not consistent; sometimes they work on the first time!) they don't do anything, but on the second click they work normally. I have a method called something like populateAccordion() that is responsible for actually loading the data from the DB and creating these records with buttons in them. I call this method on page_load if it's a postback, but only if the cached selections from previous "screens" are not null (i.e. I cache a subjectId and centerId on previous "screens" and only load the Accordion data if those are not null).
This is a little too complicated to post code for, but does anyone have any ideas what could be going wrong, or how I could figure this out? I've done quite a bit of debugging, and my populateAccordion() is always called properly on the previous screen's Continue button click, and on every page_load after that, and the Click events are always wired up right when the buttons are created in that method, so I can't figure out why the click events wouldn't fire on the first try.
The problem is similar to the one mentioned in this post...
https://stackoverflow.com/a/2784140/1246574
But I am definitely setting the IDs for all of my buttons, so the solution mentioned in that post isn't the fix for my problem.
I've also read quite a few other posts that say to move your code from Page_Load to Page_PreRender, but that doesn't change anything in this case; it acts exactly the same.
I also tried setting the buttons' UseSubmitBehavior = false, but no luck.
Thanks!
This is probably a latency issue. It sounds like you're able to click the controls before the page has fully loaded all the JS. Is there an onload() function in there? Perhaps you could try hiding the controls until the page has finished loading?

Stop ASP.NET PostBacks Showing As Separate Pages?

I have an ASP.NET form that the user can make lots of changes to, each time they make a change the page PostsBack and the details are updated.
If the user hits the browser back button they go back through all the previous versions of the page.
Is it possible to stop each PostBack being treated by the browser as a new page?
So the would make any changes they like and if they hit the back button it brings them to the previous form and not the same form but a different version?
I know I could use AJAX to update values but I'm not an advanced coder so trying to keep things simple as I haven't used AJAX before.
Ajax is your only solution.
There is no way to remove a page from the browser history. Javascript is explicitly denied the capability.
Now, you could, potentially, stop them from using the back button at all. Although this might result in unhappy users and I'm not 100% certain it works in all browsers.
function body_onload(){
window.history.forward(1);
}
You could use a trick to do it.
On postback you can set a session bit to true saying they submitted that form. On your postback check to see if that value is set. If it is they are trying to do it again and you can just abort it. It wouldn't prevent the postback per se but you could control the logic and prevent it from DOING anything.
I personally would explore ajax as Jquery provides some nice ways to do it and it'd be a learning experience but I suppose this would work as you are asking. On a per session basis. If you only want 1 submission ever use a database to store the activity.
You could use UpdatePanel: http://msdn.microsoft.com/en-us/library/bb386454.aspx

What are the MUSTS for having an asp.Net application to support BACK button of the browser?

Is there any pattern or kind of "least requirements list" to follow for ensuring an asp.NET application to support BACK button of the browser for each aspx page?
thanks
In general, the back button on the browser will take you to the previous HTML GET or POST that occurred. It navigates by page-wide transactions, so anything done dynamically cannot be navigated that way. Also, the back button doesn't rewind code execution, so if you are determining something based off of a Session variable or something similar, that won't be rewound either. Obviously, it won't rewind database transactions either.
In general, if you want to support the back button, you'll need to make sure to divide everything you need to navigate between with said button is divided by an HTML transaction of some sort.
Again, you're going to run into issues if your page display is dependent on server-side control that changes from one post to the next. This is one reason you see some forms feed a 'Page has expired' error when you try to navigate back to them.
Not really... It depends on your application flow.
There are things that make supporting the back button more awkward.
for example using pure ajax to change the majority of the content on the page,
will look like a 'new' page but wont be compatible with the back button (though you can fudge it)
another example is posting back to the same page more than once, as this can make it appear like the back button is not working, and at the same time re-doing your request (and therefore database transactions)
Fundamentally it depends on your application requirements.

WinForms Web Browser control forcing refocus?

I'm trying to automate a web process where I need to click a button repeatedly. When my code "clicks" that button (an HtmlElement obtained from the WebBrowser control I have on my form) then it brings focus back to my application, more specifically the WebBrowser control. I wish to better automate this process so that the user can do other things while the process is going on, but that can't happen if the window is unminimizing itself because it's attaining focus.
The code associated with the clicking is:
HtmlElement button = Recruiter.Document.GetElementById("recruit_link");
button.InvokeMember("click");
I've also tried button.RaiseEvent("onclick") and am getting the exact same results, with focus problems and all.
I've also tried hiding the form, but when the InvokeMember/RaiseEvent method is called, whatever I was working on loses focus but since the form is not visible then the focus seems to go nowhere.
The only non-default thing about the webbrowser is it's URI being set to my page and ScriptErrorsSuppressed being set to True.
Why do You need to click this button? It's sending some form?
If yes, you can use WebClient and simulate sending form without graphic interface.
Basicly almost anything significant requires connection to website using Get Post or multipart/post so WebClient will be perfect. You can simply get the site wich is this button on and parse it to get what clicking it do. And then simulate your own action.
In IE7 or higher you can implement IProtectFocus on the webbrowser site and deny the focus change. You would be much better off if you use the raw ActiveX or its wrappers that support this kind of customization, e.g. csexwb. If you have to use the winform webbrowser control, you need to create your own webbrowser site.

Categories