I've got a sort-of CMS set up so staff members can edit website content on our new website instead of submitting requests to us. I need to write a function that I can call from within my .aspx (front end) file. So I have a div and I want to make my function call from within it such as:
<div class='content-section'>
<% //call function here %>
</div>
My first thought was to make it a function in the code behind of Site.Master, but I was having issues calling it. I'm not the most versatile C# developer, so some guidance would be nice. The Function essentially just returns a string (fetched from a database) containing page content
Thanks
Use the following pattern to have C# code in ASPX itself:
<%@ Page Language="C#" %>
<script runat=server>
protected String MethodThatReturnsStringFromDB()
{
// do the DB logic
return "stringfromdb";
}
</script>
<html>
<body>
<form id="form1" runat="server">
<div class='content-section'>
<% =MethodThatReturnsStringFromDB()%>
</div>
</form>
</body>
</html>
and if you want to use this method from several pages, then you can try putting it in the master page as follows and use it in child pages:
master page aspx code:
<%@ Page Language="C#" %>
<script runat=server>
public String MethodInMasterPageThatReturnsStringFromDB()
{
// do the DB logic
return "stringfromdb";
}
</script>
<html>
<!-- site.master markup -->
</html>
here is the child page code:
call the master page's method.
You can use the @MasterType directive to avoid the cast
<%@ Page Language="C#" MasterPageFile="~/site.Master" %>
<%@ MasterType VirtualPath="~/site.master" %>
<div class='content-section'>
<% =Page.Master.MethodInMasterThatReturnsStringFromDB()%>
</div>
without the @MasterType directive, it would be:
<%@ Page Language="C#" MasterPageFile="~/site.Master" %>
<div class='content-section'>
<% =(Page.Master as MasterPageType).MethodInMasterThatReturnsStringFromDB()%>
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.