1

I tried creating a master page in ASP.NET

I have this error on the Markup of page sDefault.aspx where I need call the master page :

CS0103: The name 'Base64ForUrlEncode' does not exist in the current context.

Why I have this error ? In the MasterPage.master.cs I have insert the :

public static string Base64ForUrlEncode(string str)
{
    byte[] encbuff = Encoding.UTF8.GetBytes(str);
    return HttpServerUtility.UrlTokenEncode(encbuff);
}

I have tried this solution without success .

How to do resolve this? My code below.

This is the code of 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);
    }    
}

This is the code of 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>

And this the Markup of page sDefault.aspx where I need call the master page :

 <%@ 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>

Edit #1

Code-Behind Default.asp.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        MasterPage MasterPage = (MasterPage)Page.Master;
        MasterPage.Base64ForUrlEncode(null);
    }
}

Edit #2

Code-Behind Default.asp.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        MasterPage mp = (MasterPage)Page.Master;
        string test = mp.Base64ForUrlEncode("TestUrl");
    }
}

Markup 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);
}
Chevy Mark Sunderland
  • 401
  • 2
  • 10
  • 21
  • 1
    Possible duplicate of [Call Method in Master Page](https://stackoverflow.com/questions/6332889/call-method-in-master-page) – JP Hellemons Dec 06 '18 at 10:20

2 Answers2

0

Base64ForUrlEncode is in the Master page class (public partial class MasterPage), while the aspx page sDefault has it's own. Therefore the method cannot be found unless you specify the namespace on the aspx.

<%= YourNameSpace.MasterPage.Base64ForUrlEncode("test")  %>

Update

public MasterPage master;

protected void Page_Load(object sender, EventArgs e)
{
    master = (MasterPage)Page.Master;
    string s = master.Base64ForUrlEncode("test");
}

Then make the Base64ForUrlEncode method in the Master Public an non-static

public string Base64ForUrlEncode(string str)
{
}

Now you can reference it directly on the aspx

<ItemTemplate>
    <%# master.Base64ForUrlEncode("TestUrl") %>                   
</ItemTemplate>
VDWWD
  • 35,079
  • 22
  • 62
  • 79
0

I'd like to reference to your edit 1:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        MasterPage MasterPage = (MasterPage)Page.Master;
        MasterPage.Base64ForUrlEncode(null);
    }
}

change it to:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        MasterPage mp = (MasterPage)Page.Master;
        string test = mp.Base64ForUrlEncode("your test string");
    }
}

You could change the grid to a repeater if you have just 1 field and/or reference a function in the code behind to build the url.

This is too much logic in the view imho:

<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")))) %>' />

(or something "on item data bound")

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128