C# webbrowser control (executing all javascripts associated with form) - c#

I am trying to auto submit form using webbrowser control. I am using the following code to submit"
currentElement.InvokeMember("submit");
Now this methods works fine. But sometimes a form may have some javascript function that is called on button click at the time of submission. So let's say if a form has some button image called "Submit" and when a user presses it, a javascript function somefunction() is called and then form is submitted.
Problem is when I use the above method InvokeMember then it only submits the form and doesn't execute associated scripts (in this case somefunction()) and I have to manually write code
webBrowser1.Document.InvokeScript("somefunction");
But this requires that I know before hand if there is some function. Is there any way I submit form and it will automatically run all associated javascript?
And I don't know button name or ID either which is clicked by user to submit form. Because in some cases it may not even have ID or name for e.g.
<span class="btn" onclick="somefunction()">
<img style="cursor:pointer" title="Submit" alt="Submit" src="http://stackoverflow.com/imagesbutton.png?2012">
<div id="s" style=""></div>
</span>

It's been a while since I have messed around with the WebBrowser control, but I used to make it jump through hoops for me. I've come across this issue in the past.
http://www.codeproject.com/Tips/60924/Using-WebBrowser-Document-InvokeScript-to-mess-aro
When you get a Form object, take the string out of the OnSubmit and run this to execute it before submitting the form:
object[] codeString = {"myObject.setVariable(0);"};
webBrowser1.Document.InvokeScript("eval",codeString);
Works like a charm.

If you know the name or id of the image tag you can use
webBrowser1.Document.GetElementById("button").InvokeMember("click");
or
webBrowser1.Document.GetElementByName("button").InvokeMember("click");
to call the associate function in javascript

Related

how to avoid postback when i click the button in MVC

I have two button in my cshtml page. When i click the first button data fetch from the database and bind it my controls. The records are many so the second button goes down, then i click the second button same thing happened fetch from the database and bind the grid, so the page is loaded then the page goes up. I want to stay same place when i click the second button. How to prevent it.
make your button like this :
<input type="button" id"button2" onclick="CallAjax();return false;"/>
//then in javascript
function CallAjax()
{
//your ajax call here to retrieve or update data
}
This is MVC 101...
Make your button a standard HTML5 button, which calls a JavaScript function on click...
<input type="button" id="goButton" value="Go ยป" onclick="goFunction();">
(I don't think you have to "return false" with a button, only with a submit)
Then, use Razor helpers in your view to do the Ajax call to the controller...
function goFunction() {
var url = '#Url.Action("MyActionMethodName", "MyControllerName")';
var settings = { 'some data': 'some values' };
$.ajax(url, settings);
}
See the JQuery Ajax page for more information about how to work the "settings" object.
BTW - sounds like you are "paging a grid" but you just forgot to say so... use a tool for that, such as Kendo. Paging is solved, don't solve it again.

Tying it all together - ASP.NET with Bootstrap 3.0

I have an ASP.NET project (non-MVC) and I'm also using Bootstrap 3.0. This is my first time using this combination and need some guidance.
I have a gridview with a buttonfield column. Right now everything is showing up just fine with my gird and Bootstrap table formatting and its binding to my datatable - no problems there.
Next, I want to make the click of the button in the Buttonfield column to initiate a modal window and display a modal based on a unique ID from the row button that opened it.
I don't really know how to tie this all together with ASP.NET and Bootstrap. HTML literals? Dynamic ASP.NET panels? It doesn't matter to me whether there is a postback or not, I'd really just like some guidance or even pseudo-code on how these can be tied together.
Since the OP specifically requested bootstrap help...
You should go through the bootstrap documentation for modals http://getbootstrap.com/javascript/#modals
It makes no difference if you are using MVC or not and you should not need to do any kind of post back to display the modal.
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" />
Will trigger element with id myModal to be shown.
Using bootstrap's own demo code in this jsfiddle demonstrates opening and dismissing the modal.
For the second part of the question, this updated jsfiddle shows how you can also use the button click event to set a value in the modal. You could do other actions in that event handler like get or send data to the backend or change other elements in the modal.
For your case, you would want to handle all button clicks in a single event handler but you can store the id in a custom attribute on the button element. I like to use custom attributes instead of parsing from name, id, or class attributes. This is the bootstrap convention.
$(function() {
$('button.btn').on('click', function() {
var value = $(this).attr('data-value')
$('div.modal').find('#target').text(value);
});
});
Here I have broken out how to get the custom attribute value from the button instance which was clicked.
Post what you have so far and what you still can't get working.
This also shouldn't be tagged with C# or asp.net as that is irrelevant.
You might need a simple js function to take care of that(as mike mentioned this has nothing to do with using or not using bootstrap since it is just some css stuff):
var RunDialog;
$(document).ready(function () {
RunDialog = $("#Id").dialog();
});
You can use ASP.Net Ajax ModalPopup for ASP.Net Web Form. You can google a lot of examples regarding GridView with ModelPopup.
ModalPopupExtender inside a GridView ItemTemplate
I want to make the click of the button in the Buttonfield column to
initiate a modal window and display a modal based on a unique ID from
the row button that opened it.
ModalPopup should work with Bootstrap.

Popup windows to select items on ASP.NET

I am new to the ASP.NET world and I need to do a popup to select some data.
The idea is that the user can select one or more options with a CheckBox. When he presses a button a popup appears with a list of options loaded from the database.
I don't know how to create a popup with those options and receive the selected options when the popup close. But I know how to do the option list from the database with a repeater.
there is no such popup control in ASP.NET.
However there are numerous 3rd party plugins, which provide popup controls.
ajax tool kit model popup extender
jquery built popups
you can create your own. popup is nothing but a hidden container, which appears upon some action, and whose location and background is as per your choice.
create a popup like this:
<div class="parent">
<div class="popup">
</div>
</div>
<input type="button" value="popup" id="btnpopup" />
and css
.parent
{
position:relative;
background-color:#CCCCCC;
width:200px;
height:200px;
}
.popup
{
width:50%;
height:50%;
position:relative;
top:20%;
left:20%;
background-color:#DDDDDD;
display:none;
}
and jQuery code
$('#btnpopup').click(function(){
$('.popup').toggle(200);
});
see this fiddle
You can use the OnClientClick of the button to open the popup. Depending on it being a normal browser popup or a jQuery dialog you have two general options:
Standard Popup
Standard popups open as if they are a separate page. When you click OK you may have to store the selection into the user's session if the data is needed on the page that triggered the popup, or store it directly in the database. If the former is the case, when you return to the page and submit it, the data from the popup will be available in the session to process.
jQuery Dialog or particularly any js-triggered html dialog. You can show it with the corresponding js again in the OnClientClick function, and perform the selection. On the OK button click of the dialog almost nothing is needed (except hiding it). Since the dialog input controls are part of the page, they will be posted on submit and be process-able on the server.
That's basically all you need to do, but some more reading on the topic won't hurt. Good luck.

.NET button in div with style="display:none" not firing

I have a pretty simple web-form set up in .Net where I am leveraging jQuery for some of the functionality. I am using the DOMWindow portion for part of the presentation layer.
There is a login form in a div that is set to display:none. When a user clicks a button on the page, it displays the login form. However the .Net button for the login form will not fire it's event when display is set to none. If i take this out, it fires fine. I have also tried using the visibility attribute, but no luck.
the div code is:
<div id="Login" style="display:none;">
The launching code is:
click here to login.<br />
the jQuery code is:
function LaunchLoginWindow() {
$(document).append("#Login");
$.openDOMWindow({
loader: 1,
loaderImagePath: 'animationProcessing.gif',
loaderHeight: 7,
loaderWidth: 8,
windowSourceID: '#Login'
});
}
Any help or explanation that anyone can offer is appreciated.
I noticed i had some code in there defining a client-side function on the Login div. I removed this so as to eliminate it as a possible issue.
I can see in your code that you are appending the div #Login but not setting its style property back to normal like block so. Set it back to block and i am sure it will work
try adding somthing like:
$(document).append("#Login").show();
OK, after playing around with this using firebug, I found the issue: When the jQuery plug-in DOMWindow creates its display layer, it appends to the HTML node of the DOM, which places the control outside the asp.net form tag. Therefore the button and actions associated with it via the DOMWindow are not recognized by .Net. So i edited the DOMWindow source file to append to the DOM form node rather then the html node.
The drawback is that the source has now been customized and will have to be QA'd thoroughly, especially if any further changes are made. But I hope to manage this effectively via commenting in the file.
Hope this helps anyone else who hits this issue.
pbr

ASP.net Text Box Enter button pressed hell

Basically I have an asp.net website with login and search pages.
Currently, I have no idea why, when ever a user hits enter in either of the login text boxes (user name/password) or in the Seach text box, the website is redirected to the default page.
I have no idea why this is happening, I've tried setting defaultButton on both the panel containing the search and the login panel but that doesnt seem to work.
I've also tried catching the key press event with javascript which isnt working either.
I have no idea what event is being fired and why, or why it seems to override everything I try to do.
Anyone seen anything like this before?
---------------Edit-------------------
<div class="searchBar" onkeypress="javascript:return
WebForm_FireDefaultButton(event,'ctl00_MainContent_logView1_LogRepeater_ctl02_lbSearch')">
<input name="ctl00$MainContent$logView1$LogRepeater$ctl02$ctl02" type="text" />
<a id="ctl00_MainContent_logView1_LogRepeater_ctl02_lbSearch" href="javascript:__doPostBack('ctl00$MainContent$logView1$LogRepeater$ctl02$lbSearch','')">
Search</a>
</div>
Ok so here is the code generated by ASP.net that is getting ignored.
---------------Update----------------
I have tried adding a hidden button and setting that button as the defaultbutton of the form and that has stoped it from redirecting to the default page each time but this does mean that pressing enter in a text box just reloads the page, which is better but still isnt Ideal.
This may not be an ASP.NET quirk as much as it is a web-browser quirk.
When you press enter in a single-line text field, the browser will submit the form to the action specified in the form tag.
ASP.NET "helpfully" creates a form tag just inside the body tag and puts all body elements inside it...
As far as I know, you can safely remove this form tag; the HTML specification allows fields outside of form tags for use in JavaScript/AJAX operations.
I have seen this behaviour before, it occurred when the first button on the form was the LoginControl. Everytime enter was pressed it would fire the click event for the login and essentially log the user out. Are you using the LoginControl?
In the end I had to add a hidden button before the login control. The hidden button didn't do anything but it prevented the LoginControl from executing.
I had this same problem once. We had a login form that worked fine when a user hit enter. Then at one point we added a search box/button in the menu bar at the top of the page. Suddenly when users would hit enter they would attempt to perform a search. I resolved it by setting the .DefaultButton of the Form for the page to my login button.

Categories