Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can a button in a simple html-document somehow call a (clientside) C# method?

Can a button in a simple html-document somehow call a (clientside) C# method?

Scheduled Pinned Locked Moved C#
csharphtmlsysadminquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I have a simple html-document with a button and when someone clicks the button, I would like to execute a C# method. Is this possible? There is no Internet or server involved in this, the html-file and the output (.dll? .exe? something else?) of my C# code will all reside in the same folder on my computer.

    OriginalGriffO L D L 4 Replies Last reply
    0
    • A arnold_w

      I have a simple html-document with a button and when someone clicks the button, I would like to execute a C# method. Is this possible? There is no Internet or server involved in this, the html-file and the output (.dll? .exe? something else?) of my C# code will all reside in the same folder on my computer.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Try:

      Bear in mind that this will do a postback to the Server with all that entails. To be a bit more sophisticated about it, you would have to use AJAX: AJAX In ASP.NET[^]

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Try:

        Bear in mind that this will do a postback to the Server with all that entails. To be a bit more sophisticated about it, you would have to use AJAX: AJAX In ASP.NET[^]

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        A Offline
        A Offline
        arnold_w
        wrote on last edited by
        #3

        But there is no server, just a client.

        OriginalGriffO 1 Reply Last reply
        0
        • A arnold_w

          But there is no server, just a client.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          You can't run C# code on a client - it needs the right .NET framework and that may not be installed. And besides, if you didn't have a server you would have nothing to load the HTML from in the first place! I think you need to have a good long think about exactly what you are trying to do, and perhaps research some to find out how these things work. I get the feeling you are just guessing, and that's not a good idea.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • A arnold_w

            I have a simple html-document with a button and when someone clicks the button, I would like to execute a C# method. Is this possible? There is no Internet or server involved in this, the html-file and the output (.dll? .exe? something else?) of my C# code will all reside in the same folder on my computer.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You probably need to run your HTML in a web browser control, in a Windows Forms or WPF app, and then hook up to events in the HTML. "Tagging" hidden content with "scripting code" is an added option. [internet explorer - Hooking IE Events in C# - Stack Overflow](https://stackoverflow.com/questions/46062526/hooking-ie-events-in-c-sharp)

            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

            A 1 Reply Last reply
            0
            • L Lost User

              You probably need to run your HTML in a web browser control, in a Windows Forms or WPF app, and then hook up to events in the HTML. "Tagging" hidden content with "scripting code" is an added option. [internet explorer - Hooking IE Events in C# - Stack Overflow](https://stackoverflow.com/questions/46062526/hooking-ie-events-in-c-sharp)

              It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

              A Offline
              A Offline
              arnold_w
              wrote on last edited by
              #6

              Can a button in html call any function at all? If so, in what programming language? Could I in that case make that programming language call a .dll consisting of my compiled C# code?

              L 1 Reply Last reply
              0
              • A arnold_w

                Can a button in html call any function at all? If so, in what programming language? Could I in that case make that programming language call a .dll consisting of my compiled C# code?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                A button is "dumb"; it doesn't "call anything and everything"; it needs to be "hooked up"; regardless of the platform. You're interfacing with a "document object model". An API.

                It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                1 Reply Last reply
                0
                • A arnold_w

                  I have a simple html-document with a button and when someone clicks the button, I would like to execute a C# method. Is this possible? There is no Internet or server involved in this, the html-file and the output (.dll? .exe? something else?) of my C# code will all reside in the same folder on my computer.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  An HTML button cannot directly call a C# method. The button is in one execution environment and the C# code is in another, even if your HTML and C# code are running client-side. It sounds like you're trying to use an HTML page as an interface for a desktop app. That's a bad idea. It can work, but there's a steep price. You'd have to host the ASP.NET runtime in your desktop app. This adds a LOT of overhead and complexity to your app, so it's usually not worth it. If you're going to go through this pain, it's actually easier to just write up the app as an actual website and host in on the internet.

                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                  Dave Kreskowiak

                  A 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    An HTML button cannot directly call a C# method. The button is in one execution environment and the C# code is in another, even if your HTML and C# code are running client-side. It sounds like you're trying to use an HTML page as an interface for a desktop app. That's a bad idea. It can work, but there's a steep price. You'd have to host the ASP.NET runtime in your desktop app. This adds a LOT of overhead and complexity to your app, so it's usually not worth it. If you're going to go through this pain, it's actually easier to just write up the app as an actual website and host in on the internet.

                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                    Dave Kreskowiak

                    A Offline
                    A Offline
                    arnold_w
                    wrote on last edited by
                    #9

                    You are correct that I want a web-browser with an html-file to be the GUI of a (very basic) application that displays text and pictures with basic filter functionality. If I divide it into an html-file and a server written in C#, is it then possible to do both the client-browsing and server-hosting on the same laptop computer, without access to the Internet?

                    D J 2 Replies Last reply
                    0
                    • A arnold_w

                      You are correct that I want a web-browser with an html-file to be the GUI of a (very basic) application that displays text and pictures with basic filter functionality. If I divide it into an html-file and a server written in C#, is it then possible to do both the client-browsing and server-hosting on the same laptop computer, without access to the Internet?

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Yes it's possible, though, I wouldn't recommend it. Google for "C# host ASP.NET in Windows Forms application".

                      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                      Dave Kreskowiak

                      1 Reply Last reply
                      0
                      • A arnold_w

                        I have a simple html-document with a button and when someone clicks the button, I would like to execute a C# method. Is this possible? There is no Internet or server involved in this, the html-file and the output (.dll? .exe? something else?) of my C# code will all reside in the same folder on my computer.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        You should read some CodeProject articles starting with Sample HTTP Server Skeleton in C#[^]. EDIT: alternative article: Embedded .NET HTTP Server[^] I read it again only yesterday; today the link (as many others to CP) seems defective, hope it will get fixed soon. Using those techniques you can combine any C# functionality you want with an HTTP server of your own, allowing you to get one or more "web pages" from it by using an appropriate URL. Technically speaking your C# code is running on the server, which can reside on the same PC your browser/client is running on. I've done this a couple of times, it works just fine. BTW it is advanced stuff and will need some studying... :) PS: and then Microsoft is working on its Blazor | Build client web apps with C# | .NET[^] technology where .NET code will actually run inside any browser anywhere, but AFAIK that isn't really available yet.

                        Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...

                        1 Reply Last reply
                        0
                        • A arnold_w

                          You are correct that I want a web-browser with an html-file to be the GUI of a (very basic) application that displays text and pictures with basic filter functionality. If I divide it into an html-file and a server written in C#, is it then possible to do both the client-browsing and server-hosting on the same laptop computer, without access to the Internet?

                          J Offline
                          J Offline
                          jsc42
                          wrote on last edited by
                          #12

                          arnold_w wrote:

                          without access to the Internet

                          If you consider the Internet to be things outside of your PC then the answer is Yes. However, even if you keep everything on your PC, you will need the PC to host a web server but that can be local to the PC only. So the PC can be stand-alone and not connected to the Internet. This is actually common practice - when you write a web application, you run it on a locally hosted web server and only when all testing has been completed you would publish it to an intranet or extranet web server.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups