We help IT Professionals succeed at work.

pressing download button in c#

AlHal2
AlHal2 asked
on
145 Views
Last Modified: 2017-03-30
Please see http://www.bmfbovespa.com.br/en_us/products/indices/broad-indices/brazil-broad-based-index-ibra-composition-index-portfolio.htm
At the bottom of the page there is a download button.
How do I press this download button from a C# program?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
You can't "press  a button" from server side code.  C# runs before the page is loaded to the client.  Interactions, like button pressing, occur on the client side.  If you want to press a button, you must use a client side language, like jQuery / JavaScript.

Author

Commented:
I've used a tiny bit of JavaScript in my own webpages.  This page is not my own.
How would I go about it.  Do you have any sample code?
CERTIFIED EXPERT
Top Expert 2010

Commented:
This will click the link:

$("a[id$='Conteudo_Export_Test']").click();

Open in new window


You will want to wrap that inside a document ready function, so that it doesn't run until the DOM is loaded.

Here is a demo:  https://jsfiddle.net/zephyr_hex/rg90u61v/

HTML

<a href="" id="ctl00_contentPlaceHolderConteudo_Export_Test">Download</a>
<span></span>

Open in new window


jQuery

$(document).ready(function() {
   //test:  capture the click event
	$("a[id$='Conteudo_Export_Test']").on('click', function() {
  $('span').append('The link was clicked');
  });

  //click the Download link
	$("a[id$='Conteudo_Export_Test']").click();
});

Open in new window

Author

Commented:
Thanks.  How can I schedule this to run at a particular time of day?

Author

Commented:
I've put this into a sample webpage.  How do I determine where the downloaded file goes?

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
   
   
        $(document).ready(function () {
            //test:  capture the click event
            $("a[id$='Conteudo_Export_Test']").on('click', function () {
                $('span').append('The link was clicked');
            });

            //click the Download link
            $("a[id$='Conteudo_Export_Test']").click();
        });
    
    
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    
    

        
        <a href="" id="ctl00_contentPlaceHolderConteudo_Export_Test">Download</a>
    
</asp:Content>

Open in new window

CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thanks.