我尝试在ASP.NET中创建一个母版页
我在页面 sDefault.aspx 的标记上遇到此错误,我需要在其中调用母版页:
CS0103: The name 'Base64ForUrlEncode' does not exist in the current context.
为什么我有此错误? 在 MasterPage.master.cs 中,插入:
public static string Base64ForUrlEncode(string str)
{
byte[] encbuff = Encoding.UTF8.GetBytes(str);
return HttpServerUtility.UrlTokenEncode(encbuff);
}
我尝试了这个solution,但没有成功。
该如何解决? 我的代码如下。
这是 MasterPage.master.cs
的代码public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
InitializeCulture();
}
}
protected void InitializeCulture()
{
Page.Culture = "en-US";
Page.UICulture = "en-US";
}
public static string Base64ForUrlEncode(string str)
{
byte[] encbuff = Encoding.UTF8.GetBytes(str);
return HttpServerUtility.UrlTokenEncode(encbuff);
}
public static string Base64ForUrlDecode(string str)
{
byte[] decbuff = HttpServerUtility.UrlTokenDecode(str);
return Encoding.UTF8.GetString(decbuff);
}
}
这是 MasterPage.master
的代码<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<style type="text/css">
.labelStyle {
color: red;
background-color: yellow;
border: Solid 2px Red;
margin-left: auto;
margin-right: auto;
width: 30em;
display: block;
text-align: center;
}
.image {
margin-left: auto;
margin-right: auto;
}
.toUpper {
text-transform: uppercase;
}
.header {
background-color: #686565;
font: bold;
color: #686565;
text-align: center;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.2/jquery-ui.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
这是页面 sDefault.aspx 的标记,我需要在其中调用母版页:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sDefault.aspx.cs"
Inherits="sDefault" MasterPageFile="MasterPage.master" %>
<%@ MasterType virtualPath="MasterPage.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:GridView ID="gvProducts"
AutoGenerateColumns="False"
EmptyDataText="No data"
EnableViewState="true"
runat="server"
DataKeyNames="sID"
HorizontalAlign="Center">
<AlternatingRowStyle CssClass="altrows" />
<Columns>
<asp:TemplateField
HeaderText="Nr"
ItemStyle-CssClass="ddl_Class_new"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl='<%#(String.IsNullOrEmpty(Eval("Nr").ToString()) ? "" :
Eval("Nr").ToString().Equals("0") ? "" :
String.Format("sExport1.aspx?d={0}&s={1}",
Base64ForUrlEncode(HttpUtility.UrlEncode(Eval("d").ToString())),
HttpUtility.UrlEncode(Base64ForUrlEncode("s")))) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Content>
编辑#1
Default.asp.cs背后的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MasterPage MasterPage = (MasterPage)Page.Master;
MasterPage.Base64ForUrlEncode(null);
}
}
编辑#2
Default.asp.cs背后的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MasterPage mp = (MasterPage)Page.Master;
string test = mp.Base64ForUrlEncode("TestUrl");
}
}
标记aspx
<asp:TemplateField>
<ItemTemplate>
<%# mp.Base64ForUrlEncode("TestUrl") %>
</ItemTemplate>
</asp:TemplateField>
MasterPage.master.cs
public string Base64ForUrlEncode(string str)
{
byte[] encbuff = Encoding.UTF8.GetBytes(str);
return HttpServerUtility.UrlTokenEncode(encbuff);
}
答案 0 :(得分:0)
Base64ForUrlEncode
在母版页类(public partial class MasterPage
)中,而aspx页sDefault
拥有它自己的页。因此,除非您在aspx上指定名称空间,否则无法找到该方法。
<%= YourNameSpace.MasterPage.Base64ForUrlEncode("test") %>
更新
public MasterPage master;
protected void Page_Load(object sender, EventArgs e)
{
master = (MasterPage)Page.Master;
string s = master.Base64ForUrlEncode("test");
}
然后将Master Public中的Base64ForUrlEncode
方法设为非静态
public string Base64ForUrlEncode(string str)
{
}
现在您可以直接在aspx上引用它
<ItemTemplate>
<%# master.Base64ForUrlEncode("TestUrl") %>
</ItemTemplate>
答案 1 :(得分:0)
我想参考您的修改1:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MasterPage MasterPage = (MasterPage)Page.Master;
MasterPage.Base64ForUrlEncode(null);
}
}
将其更改为:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MasterPage mp = (MasterPage)Page.Master;
string test = mp.Base64ForUrlEncode("your test string");
}
}
如果您只有1个字段和/或在后面的代码中引用一个函数来构建网址,则可以将网格更改为中继器。
在恕我直言中,这是太多逻辑了:
<asp:HyperLink runat="server"
NavigateUrl='<%#(String.IsNullOrEmpty(Eval("Nr").ToString()) ? "" :
Eval("Nr").ToString().Equals("0") ? "" :
String.Format("sExport1.aspx?d={0}&s={1}",
Base64ForUrlEncode(HttpUtility.UrlEncode(Eval("d").ToString())),
HttpUtility.UrlEncode(Base64ForUrlEncode("s")))) %>' />
(或“绑定项目数据”)