Call a C# function from a button clicked on an HTML page - c#

My application (C#) consists of an embedded web browser which will load a page in HTML from a server. The HTML page will be created from scratch for this particular application. My intention is to have the following procedure:
A user opens the application
A web browser embedded within the application will load an HTML page
User will click a button on the HTML page
A C# function (on client side) is triggered from this action
function does some work
send a list of data back to the web page
Web page will retrieve this data and display it
user's browser will be refreshed to display the new page
Now my questions are the following:
How can a C# function on the client side be triggered when a button is clicked on a server page
What is the best way to transfer data (will probably be object)
how can a function on the HTML page wait for data to be received before continuing to execute the function?

Using the WebBrowser control, you can use the ObjectForScripting property. This will allow JavaScript to call C# code using window.external.C# MethodName
There's some quirks with the serialization you might need to work through, and you might need to use webbrowser.Document.InvokeScript to trigger your data back to JavaScript (I've not personally used the result of a window.external call so cannot speak for it).

Related

On which page would I add an ajax query for ASP.NET web app with layout and partial views?

I need to make my web app more responsive, such that data I have requested gets posted as it comes in. The problem is I have a web page, a layout page, and a partial page that contains a table. The table is the control being updated. (As a new test result comes in, it gets added on to the table for the user to view.)
If I wanted to use an ajax call for asynchronous data retrievals, which of the files do I update? The Layout, the main view page, or the partial view with the table?
Ajax is Asynchronous JavaScript Request from Client to Server. Meaning that you want to send a request from your JavaScript to your server.
The first part, i.e. making the request goes into your .js file. For example you may want to trigger the Ajax call on a button click... this button may be part of your MainPage, PartialView, Layout, etc... your client side logic goes into your .js file.
On the Server side, you need a controller Action to receive the request and send a response back.
See this sample here

Custom action send document to different page

I have a SharePoint 2013 provider hosted app. In the app project there is a ribbon custom action that navigates to a page in the web project. When the ribbon custom action is pressed, the selected document (the entire document not just properties) should be send to the .aspx page. On the .aspx page I want to save it in the database.
How can i send the entire document to the .aspx page?
<CommandUIHandler Command="Invoke_RibbonCustomAction1ButtonRequest"
CommandAction="~remoteAppUrl/Pages/CustomActionTarget.aspx?HostUrl={HostUrl}&Source={Source}&ListURLDir={ListUrlDir}&SelectedListID={SelectedListId}&SelectedItemID={SelectedItemId}"/>
</CommandUIHandlers>
This is the code I have but as you can see this will send the properties to the .aspx page and i want to send the entire document.
Through Source={Source} you can get the complete url till document and when you hit it you are able to download it. so i guess you have to write code on another page from where you want to push it to DB, download it on local and from where you can push this file to sharepoint DB.
i am not able to fully answering your question but hope it may give you some directions.

Reading json data from a WPF WebBrowser Control

How would I go about reading the json data returned by a web server in response to the ajax calls/polls from the html document inside my wpf web browser control? The connection between the client and server is secured.
It's possible to call C# code from Javascript and vice versa i.e. call Javascript in the page from C#. Depending on your scenario you could use either of these to return the JSON string.
See this article for more information
If you don't have control over the html page i.e. you cannot change it you might have to parse the whole page, you can get the page like this

filling web form using a windows application in C#

I have a site which get information of user in one of its pages.
every user has a card which contain his information. I want to write a windows application in Visual C# which read the card and fill web form using those data.
for this reason I have to run a browser in my windows application and run some javascript code to fill that elements in that browser.
does any one how can I run a browser and give to that specific javascript (in url after on page has been loaded) to fill the form?
There is a WebBrowser control that you can use in your windows app. As for populating the information on a web page, I would just pass the userID in QueryString to the URL of the page you create (in your WebBrowser control), and in the page add code to retrieve the user information and display it.
Here is some info on the WebBrowser control:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx
My guess is you would be better off handing over the data you want to see in the web forms via http post or get. Then let the serverside write the values into the propper forms.

Auto fill web page using ASP.Net C# web page

I need to write a ASP.Net C# web page that can open a web page, fill in the fields and click the submit button on the web page automatically. My web page should launch the IE browser and navigate to a specified URL and fill the form and submit it. Not sure where I should start from. Any help is greatly appreciated.
Thank you.
I'm not sure that, browser will allow to run all your js code.
But you can try with jquery, or with some another javascript lib.
You can add to your page iframe tag, than add dynamically another web-site to the iframe content. You also must know id's or classNames of html controls.
if you're going to use jquery
$(document).ready(function () {
$('#textbox1').val()="someText1";
....
$('#textboxN').val()="someTextN";
//call click event
$('#btnID').click();
});
to automatically start your page, you can use Process.Start() method in server side.
if you're trying too run in client side, you can use Response.Redirect Method
Why not make a POST/GET call directly?
You could use HTTPWebRequest
http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx

Categories