Results 1 to 7 of 7

Thread: How to write C# code in to HTML tag in asp.net?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    123

    How to write C# code in to HTML tag in asp.net?

    Hi everyone
    I want to write c# code into html tag

    Here HTML tag:
    HTML Code:
    <button type="button">Click Me!</button>
    Here C# code:
    Code:
    SqlConnection Conn = new SqlConnection();
    Conn.ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Users\\Dell\\source\\repos\\WebApplication9\\App_Data\\Database1.mdf; Integrated Security = True";
    Conn.Open();
    Response.Write("Database is opened");
    How to put C# code into Html Button in asp.net?
    Last edited by dday9; Oct 23rd, 2023 at 08:57 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,663

    Re: How to write C# code in to HTML tag in asp.net?

    Unless you're using Blazor, you can't. C# code is executed on the server while the HTML markup is presented on the client. Please explain what you're actually trying to achieve, rather than just how you're trying to achieve. Also, please describe exactly what type of project this is. ASP.NET covers a lot of ground these days. How you achieve a particular aim will differ based on the specific technology.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,960

    Re: How to write C# code in to HTML tag in asp.net?

    Typically I would suggest setting up an endpoint to execute your C# code and use JavaScript to submit an AJAX request to the endpoint.

    However, you aren't providing us with enough information. Really you don't want to just open a database connection, do you?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    123

    Re: How to write C# code in to HTML tag in asp.net?

    What I want to do it is to create Html page with all html elements that I need them like
    buttons
    links
    etc
    Than I want to use C# codes behind these elements.

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,684

    Re: How to write C# code in to HTML tag in asp.net?

    You can either create the page and use server side controls that will call the server, execute the C# code and return results to the page or you can use JavaScript to submit an AJAX as suggested, or you can scrap the whole idea and do everything possible but with html and javascript and only use AJAX request to get data from the server and do everything else needed in the page with javascript.
    Also javascript is not a correct per say suggestion. There are many frameworks that will do the javascript job more easily and consistent like angular and React.
    If you are just starting I strongly suggest start using something similar , don't make the mistake I did and relied on microsoft for the website, the MS asp controls are huge big ugly bloaters for the website.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,663

    Re: How to write C# code in to HTML tag in asp.net?

    Quote Originally Posted by Max45 View Post
    What I want to do it is to create Html page with all html elements that I need them like
    buttons
    links
    etc
    Than I want to use C# codes behind these elements.
    Well thank you for just repeating what you already said. Do you want this C# code in your HTML page for your health or do you expect it to actually do something for you? Could you find your way clear to actually explaining what that is? You know, the actual problem rather than your attempted solution, like I said. I don't know why I need to spell this out but apparently I do.
    Last edited by jmcilhinney; Oct 24th, 2023 at 07:57 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,661

    Re: How to write C# code in to HTML tag in asp.net?

    Quote Originally Posted by jmcilhinney View Post
    Well thank you for just repeating what you already said. Do you want this C# code in your HTML page or do you expect it to actually do something for you? Could you find your way clear to actually explaining what that is? You know3, the actual problem rather than your attempted solution, like I said. I don't know why I need to spell this out but apparently I do.
    On that note, in my signature block below is a link about hovercrafts and eels ... it;s a guide on how to get effective help here...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width