locked
Creating a master page in ASP.NET error on markup aspx page RRS feed

  • Question

  • User-2004582644 posted

    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 string Base64ForUrlEncode(string str)
    {
    byte[] encbuff = Encoding.UTF8.GetBytes(str);
    return HttpServerUtility.UrlTokenEncode(encbuff);
    }

    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 string Base64ForUrlEncode(string str)
        {
            byte[] encbuff = Encoding.UTF8.GetBytes(str);
            return HttpServerUtility.UrlTokenEncode(encbuff);
        }
    
        public 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>
    <ItemTemplate>
    <%# master.Base64ForUrlEncode("TestUrl") %>
    </ItemTemplate>
    </asp:TemplateField>
    <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>

    Code-Behind sDefault.asp.cs

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

    Friday, December 7, 2018 11:12 AM

Answers

  • User283571144 posted

    Hi Chevy Marl Sunderland,

    As far as I know, if you want to use the master page's code-beind method in the gridview databind eval, I suggest you could convert the method to be static and use import namespace in the aspx page.

    More details, you could refer to below codes:

    Master page code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace EmptyWebFormTest
    {
        public partial class Mpdue : System.Web.UI.MasterPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                InitializeCulture();
                Response.Write("<br /><br />My masterpage is : " + this.Page.Title);
            }
    
            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);
            }
        }
    }

    Child page aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Mpdue.Master" AutoEventWireup="true" CodeBehind="TestForWebFormMS.aspx.cs" Inherits="EmptyWebFormTest.TestForWebFormMS" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <%--Start GridView--%>
            <%@ Import Namespace="EmptyWebFormTest" %>
     
                <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="No data" EnableViewState="true"
                runat="server" DataKeyNames="sID" CssClass="mGrid" Width="500" HorizontalAlign="Center">
                <AlternatingRowStyle CssClass="altrows" />
                <Columns>
                    <asp:TemplateField>
                       <ItemTemplate>
                           <asp:HyperLink runat="server"
                            NavigateUrl='<%#(String.IsNullOrEmpty(Eval("Nr").ToString()) ? "" : 
                          Eval("Nr").ToString().Equals("0") ? "" : 
                          string.Format("sExport1.aspx?d={0}&s={1}", Mpdue.Base64ForUrlEncode(HttpUtility.UrlEncode(Eval("d").ToString())),
       HttpUtility.UrlEncode(Mpdue.Base64ForUrlEncode("s")))) %>' />
                       </ItemTemplate>
    
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
     
    </asp:Content>
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2018 7:47 AM

All replies

  • User2103319870 posted

    Chevy Marl Sunderland

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

    MasterPage MasterPage = (MasterPage)Page.Master;
            MasterPage.Base64ForUrlEncode(null);

    Change the code like below

     MasterPage MasterPage = (MasterPage)this.Master;
                MasterPage.Base64ForUrlEncode(null);

    Also you are using the class name MasterPage, as your page name. Try changing it to a different name

    Friday, December 7, 2018 2:39 PM
  • User-1716253493 posted

    Try This

    <%# MasterPage.Base64ForUrlEncode("TestUrl") %>

    Seem like you want use it every where, why not create a class?

    Friday, December 7, 2018 4:55 PM
  • User-2004582644 posted

    Thank you for help.

    I have changed the code, but I have this error :

    CS1061: 'System.Web.UI.MasterPage' does not contain a definition of 'Base64ForUrlEncode'

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="testing_Default" EnableEventValidation="false" 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">
    
        <%--Start GridView--%>
        <asp:Panel ID="pngvProducts" runat="server" Visible="false">
            <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="Nessun dato disponibile" EnableViewState="true"
                runat="server" DataKeyNames="sID" CssClass="mGrid" Width="500" HorizontalAlign="Center">
                <AlternatingRowStyle CssClass="altrows" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <%# MasterPage.Base64ForUrlEncode("TestUrl") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </asp:Panel>
    </asp:Content>

    Default.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.Odbc;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class testing_Default : System.Web.UI.Page
    {
        public MasterPage master;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MasterPage MasterPage = (MasterPage)this.Master;
                MasterPage.Base64ForUrlEncode(null);
            }
        }
    }

    MasterPage.master.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.Odbc;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class testing_MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeCulture();
            }
        }
    
        public string Base64ForUrlEncode(string str)
        {
            byte[] encbuff = Encoding.UTF8.GetBytes(str);
            return HttpServerUtility.UrlTokenEncode(encbuff);
        }
    
        public string Base64ForUrlDecode(string str)
        {
            byte[] decbuff = HttpServerUtility.UrlTokenDecode(str);
            return Encoding.UTF8.GetString(decbuff);
        }
    
        protected void InitializeCulture()
        {
            Page.Culture = "en-US";
            Page.UICulture = "en-US";
        }
    }

    MasterPage.master

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="testing_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>

    Wednesday, December 12, 2018 8:38 AM
  • User-2004582644 posted

    Thank you for reply.

    I have tried your suggestion without success.

    I have semplified my code but I have the same error.

    Mpdue.master.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class testing_Mpdue : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeCulture();
            Response.Write("<br /><br />My masterpage is : " + this.Page.Title);
        }
    
        protected void InitializeCulture()
        {
            Page.Culture = "en-US";
            Page.UICulture = "en-US";
        }
    
        public string Base64ForUrlEncode(string str)
        {
            byte[] encbuff = Encoding.UTF8.GetBytes(str);
            return HttpServerUtility.UrlTokenEncode(encbuff);
        }
    
        public string Base64ForUrlDecode(string str)
        {
            byte[] decbuff = HttpServerUtility.UrlTokenDecode(str);
            return Encoding.UTF8.GetString(decbuff);
        }
    }

    Mpdue.master

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="Mpdue.master.cs" Inherits="testing_Mpdue" %>
    
    <!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>Mpdue</title>
        <asp:ContentPlaceHolder id="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
            
            </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    </html>

    Default.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.Odbc;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class testing_Default : System.Web.UI.Page
    {    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MasterPage Mpdue = (MasterPage)this.Master;           
            }
        }
    }

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="testing_Default" EnableEventValidation="false" MasterPageFile="Mpdue.master" %>
    
    <%@ MasterType VirtualPath="Mpdue.master" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
        <%--Start GridView--%>
        <asp:Panel ID="pngvProducts" runat="server" Visible="false">
            <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="No data" EnableViewState="true"
                runat="server" DataKeyNames="sID" CssClass="mGrid" Width="500" HorizontalAlign="Center">
                <AlternatingRowStyle CssClass="altrows" />
                <Columns>
                    <asp:TemplateField>
                       <ItemTemplate>
                           <asp:HyperLink runat="server"
                            NavigateUrl='<%#(String.IsNullOrEmpty(Eval("Nr").ToString()) ? "" : 
                          Eval("Nr").ToString().Equals("0") ? "" : 
                          string.Format("sExport1.aspx?d={0}&s={1}", Mpdue.Base64ForUrlEncode(HttpUtility.UrlEncode(Eval("d").ToString())),
       HttpUtility.UrlEncode(Mpdue.Base64ForUrlEncode("s")))) %>' />
                       </ItemTemplate>
    
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </asp:Panel>
    </asp:Content>

    Wednesday, December 12, 2018 4:36 PM
  • User283571144 posted

    Hi Chevy Marl Sunderland,

    As far as I know, if you want to use the master page's code-beind method in the gridview databind eval, I suggest you could convert the method to be static and use import namespace in the aspx page.

    More details, you could refer to below codes:

    Master page code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace EmptyWebFormTest
    {
        public partial class Mpdue : System.Web.UI.MasterPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                InitializeCulture();
                Response.Write("<br /><br />My masterpage is : " + this.Page.Title);
            }
    
            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);
            }
        }
    }

    Child page aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Mpdue.Master" AutoEventWireup="true" CodeBehind="TestForWebFormMS.aspx.cs" Inherits="EmptyWebFormTest.TestForWebFormMS" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <%--Start GridView--%>
            <%@ Import Namespace="EmptyWebFormTest" %>
     
                <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="No data" EnableViewState="true"
                runat="server" DataKeyNames="sID" CssClass="mGrid" Width="500" HorizontalAlign="Center">
                <AlternatingRowStyle CssClass="altrows" />
                <Columns>
                    <asp:TemplateField>
                       <ItemTemplate>
                           <asp:HyperLink runat="server"
                            NavigateUrl='<%#(String.IsNullOrEmpty(Eval("Nr").ToString()) ? "" : 
                          Eval("Nr").ToString().Equals("0") ? "" : 
                          string.Format("sExport1.aspx?d={0}&s={1}", Mpdue.Base64ForUrlEncode(HttpUtility.UrlEncode(Eval("d").ToString())),
       HttpUtility.UrlEncode(Mpdue.Base64ForUrlEncode("s")))) %>' />
                       </ItemTemplate>
    
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
     
    </asp:Content>
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2018 7:47 AM