I would like to know how to store/write codes on a single page/web form and use that code (include that code) on other webform pages in my web application.
In classic ASP, I could use the code:
<!--#include file="../../Connections/connSample.asp" -->
to include the codes written on the page connSample.asp on other asp pages.
I am looking for similar ways to include the codes written on a single .Net c# webform on other web forms without having to rewrite the codes again and again.
For example, I would like to store the database location information on a single web form then include the database location information written on that web form on other web form in my web application.
aspx:
I want to store the database location/path information "C:\DBFolder\SampleDB.accdb" on one of the web form page (For example, databasepath.aspx in my application,
then I would like to include this information on other web form's data source code without having to type "C:\DBFolder\SampleDB.accdb" again and again.
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile=" ... How can I include database path here by reading the information written on databasepath.aspx? ... " ...>
aspx.cs (Code behind):
protected void BtnSubmit_Click(object sender, EventArgs e)
{
string connectString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source= ... How can I include database path here ... ";
}
You don't have any more to include source code files. An ASP.NET application is compiled to DLLs and so the code you define in a file is just available depending on which access modifiers are used etc...
Do you really have to use Access files? In this case you could use EF Core and an Access provider (never tried).
If you keep the current approach you should be able to use something such as DataFile="<%$apSettings.DataFile%>" which will fetch the value from the web.config file (I don't find the exact syntax right now).
You should go through a few getting started tutorials. ASP.NET Web Forms is an object oriented/event driven framework that uses server controls to render dynamic HTML.
ZKM128
For example, I would like to store the database location information on a single web form then include the database location information written on that web form on other web form in my web application.
Connection strings are stored in the web.config file and accessible application wide. The common pattern is crafting a class that does the data access work.
I recommend skipping Web Forms and moving to MVC or Razor Pages which are closer to classic ASP in regards to rendering HTML.
Thank you for your help,
Yes, I remember that I've entered the connection string information on the web.config file when I was using the SQL server as the back end database.
But for some websites/web applications that I've created, I still have to use MS Access database because there are too many old classic web pages that I've created many years ago which cannot be ignored, destroyed and those pages are still using Access database
as back end database. For newer web application contents, I am always using the .NET web form but still need to enter data to the same old MS Access database in order to make the whole website running.
I will study the MVC / Razor pages, if they are closer to classic ASP then probably I will love them,
Member
16 Points
44 Posts
Server Side Include in .Net webform
Sep 19, 2019 09:02 AM|ZKM128|LINK
Hello,
I would like to know how to store/write codes on a single page/web form and use that code (include that code) on other webform pages in my web application.
In classic ASP, I could use the code:
<!--#include file="../../Connections/connSample.asp" -->
to include the codes written on the page connSample.asp on other asp pages.
I am looking for similar ways to include the codes written on a single .Net c# webform on other web forms without having to rewrite the codes again and again.
For example, I would like to store the database location information on a single web form then include the database location information written on that web form on other web form in my web application.
aspx:
I want to store the database location/path information "C:\DBFolder\SampleDB.accdb" on one of the web form page (For example, databasepath.aspx in my application,
then I would like to include this information on other web form's data source code without having to type "C:\DBFolder\SampleDB.accdb" again and again.
aspx.cs (Code behind):
All-Star
48340 Points
18014 Posts
Re: Server Side Include in .Net webform
Sep 19, 2019 09:24 AM|PatriceSc|LINK
Hi,
You don't have any more to include source code files. An ASP.NET application is compiled to DLLs and so the code you define in a file is just available depending on which access modifiers are used etc...
As you are new I would suggest to have a look at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data which is the latest and likely simpler approach.
Do you really have to use Access files? In this case you could use EF Core and an Access provider (never tried).
If you keep the current approach you should be able to use something such as DataFile="<%$apSettings.DataFile%>" which will fetch the value from the web.config file (I don't find the exact syntax right now).
All-Star
52261 Points
23316 Posts
Re: Server Side Include in .Net webform
Sep 19, 2019 09:28 AM|mgebhard|LINK
You should go through a few getting started tutorials. ASP.NET Web Forms is an object oriented/event driven framework that uses server controls to render dynamic HTML.
Connection strings are stored in the web.config file and accessible application wide. The common pattern is crafting a class that does the data access work.
I recommend skipping Web Forms and moving to MVC or Razor Pages which are closer to classic ASP in regards to rendering HTML.
Member
16 Points
44 Posts
Re: Server Side Include in .Net webform
Sep 20, 2019 12:49 AM|ZKM128|LINK
Thank you for your help, I will read the model binding pages,
Member
16 Points
44 Posts
Re: Server Side Include in .Net webform
Sep 20, 2019 12:59 AM|ZKM128|LINK
Thank you for your help,
Yes, I remember that I've entered the connection string information on the web.config file when I was using the SQL server as the back end database.
But for some websites/web applications that I've created, I still have to use MS Access database because there are too many old classic web pages that I've created many years ago which cannot be ignored, destroyed and those pages are still using Access database as back end database. For newer web application contents, I am always using the .NET web form but still need to enter data to the same old MS Access database in order to make the whole website running.
I will study the MVC / Razor pages, if they are closer to classic ASP then probably I will love them,
All-Star
48340 Points
18014 Posts
Re: Server Side Include in .Net webform
Sep 20, 2019 08:10 AM|PatriceSc|LINK
I found a syntax summary at https://support.microsoft.com/en-us/help/976112/introduction-to-asp-net-inline-expressions-in-the-net-framework and scroll down to <%$..%>
And so with this syntax you can bind properties to your web.config file directly from your ASPX markup...
All-Star
194032 Points
28035 Posts
Moderator
Re: Server Side Include in .Net webform
Sep 20, 2019 10:34 AM|Mikesdotnetting|LINK
https://www.mikesdotnetting.com/article/144/classic-asp-include-files-in-asp-net
9 years old, but no less valid for that.
Member
16 Points
44 Posts
Re: Server Side Include in .Net webform
Sep 21, 2019 02:39 PM|ZKM128|LINK
Thank you so much!!
Exactly the answer that I was looking for.