[英]Dynamic Content Function (callable from front page .aspx, not code behind)
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. 我已经设置了某种CMS,以便工作人员可以在我们的新网站上编辑网站内容,而不必向我们提交请求。 I need to write a function that I can call from within my .aspx (front end) file. 我需要编写一个可以从.aspx(前端)文件中调用的函数。 So I have a div and I want to make my function call from within it such as: 所以我有一个div,我想从其中进行函数调用,例如:
<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. 我首先想到的是使其成为Site.Master背后代码的功能,但是我在调用它时遇到了问题。 I'm not the most versatile C# developer, so some guidance would be nice. 我不是最全能的C#开发人员,所以一些指导会很不错。 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: 使用以下模式在ASPX本身中包含C#代码:
<%@ 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: 母版页的aspx代码:
<%@ 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 您可以使用@MasterType指令来避免强制转换
<%@ 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: 如果没有@MasterType指令,它将是:
<%@ Page Language="C#" MasterPageFile="~/site.Master" %>
<div class='content-section'>
<% =(Page.Master as MasterPageType).MethodInMasterThatReturnsStringFromDB()%>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.