-
Oct 22nd, 2023, 07:34 AM
#1
Thread Starter
Lively Member
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.
-
Oct 22nd, 2023, 10:29 PM
#2
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.
-
Oct 23rd, 2023, 08:59 AM
#3
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?
-
Oct 24th, 2023, 05:24 AM
#4
Thread Starter
Lively Member
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.
-
Oct 24th, 2023, 07:29 AM
#5
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 24th, 2023, 07:47 AM
#6
Re: How to write C# code in to HTML tag in asp.net?
Originally Posted by Max45
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.
-
Oct 24th, 2023, 10:05 AM
#7
Re: How to write C# code in to HTML tag in asp.net?
Originally Posted by jmcilhinney
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|