Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a public method in Site.Master page and i wanna acees that method from a content page called "Home.aspx"

these are my codes

Site.master.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    DBAccess obj1 = new DBAccess();

    protected void Page_Load(object sender, EventArgs e)
    {
        String name;
        if (Session["email"] != null)
        {
            
            name = obj1.setusername();

            HyperLink1.Text = "Welcome " + (String)Session["name"];
            HyperLink1.NavigateUrl = "MyReservations.aspx";
            HyperLink1.ToolTip = "View your Reservations";

            HyperLink2.Text = "Log Out";
            HyperLink2.NavigateUrl = "logout.aspx";


          
           
        }

       
    }

    public string setURL( String link)
    {
       
        return link;
    }
}


Home.aspx
ASP.NET
<%@ Page Title="VidStream" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Home.aspx.cs" Inherits="_Default" %>
<%@ MasterType VirtualPath="~/Site.master"%>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css">
        .style1
        {
            width: 100%;
            border: 1px solid #E4E4E4;
        }
    </style>


<asp:Content ID="nav" runat="server" ContentPlaceHolderID="navigationbar">
<ul id="navbar">
<li><a href="Home.aspx" class="active">Home</a></li>
<li><a href="Movies.aspx">Movies</a></li>
<li><a href="News.aspx">News</a></li>
<li><a href="Contact.aspx">Contact</a></li>
</ul>



<asp:Content ID="Heading" runat="server" ContentPlaceHolderID="Heading">
<h1>Coming Soon</h1>



 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <form id="tableContainer"  runat="server">
        <asp:Table ID="CommingMovies" runat="server" class="style1" CellSpacing="20">

            
 
    
    
    </form>


Home.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.SessionState;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Web.Security;
using System.Security.Policy;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateDynamicTable();

    }

    private void CreateDynamicTable()
    {
        MoviesGenDataContext con = new MoviesGenDataContext();

        var nowmov = from m in con.Item_Masters
                     where m.Item_In_Stock == true
                     select m;

        //var movi = from m in con.Moviedbs select m;
        ArrayList list = new ArrayList();
        foreach (var x in nowmov)
        {
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TableCell cell3 = new TableCell();
   

            Label desc = new Label();
            LiteralControl h1start = new LiteralControl("<h1>");
            LiteralControl h1end = new LiteralControl("</h1>"); 
            desc.Text = x.Item_Desc;
            
           

            

            HyperLink movlk = new HyperLink();
            LiteralControl nln = new LiteralControl("<br />");
            LiteralControl nln2 = new LiteralControl(" ");
            HyperLink play = new HyperLink();
            HyperLink book = new HyperLink();

            //ImageButton playbtton = new ImageButton();
            play.ImageUrl = "Images/playtrailer.gif";
            play.NavigateUrl = "Global.asax?url="+x.Trailer_URL;
            
i wanna access the method here

           // play.NavigateUrl = Site. + x.Trailer_URL;
            
           

            //ImageButton bookbtton = new ImageButton();
            book.ImageUrl = "Images/bookDVD.gif";

            movlk.ImageUrl = "Posters/" + x.Name.Trim() + ".jpg";

            cell1.Controls.Add(movlk);
           

            cell1.Width = Unit.Pixel(214);
            cell2.Width = Unit.Pixel(200);
            cell2.Controls.Add(h1start);
            cell2.Controls.Add(desc);
            cell2.Controls.Add(h1end);
            cell2.Controls.Add(nln);

            //play.Text = "Play This Trailer";
            //play.NavigateUrl = "MPlayer.aspx?Vn=Posters/"+ x.Name + ".flv";

            //book.Text = "Book This Movie";
            //book.NavigateUrl = "Book.aspx?Vn=" + x.Name.Trim();

            cell3.Controls.Add(play);
            cell3.Controls.Add(nln);
            cell3.Controls.Add(book);

            row.Cells.Add(cell1);
            row.Cells.Add(cell2);
            row.Cells.Add(cell3);
          

        
            CommingMovies.Rows.Add(row);
       


            //GridView1.AutoGenerateColumns = true;

            //GridView1.DataSource = list;
            //GridView1.DataBind();
        }

    }
}


i tried to access the method by creating an object of the master class and using this
Master.setURL();
but it didnt work. can any one analyze the code and tell me how to access this method?? i think i'm missing a directive. can anyone tell me what it is??
Posted
Updated 21-Aug-11 23:42pm
v2

Since the method is non-static so you'll have to make an object to access it :)

Like this
C#
SiteMaster s=new  SiteMaster();
s.setURL();
 
Share this answer
 
Comments
aroshlakshan 22-Aug-11 5:16am    
i tried but it didn't work. can you please analyze the code a little bit more and see whats the problem??
C#
MyMasterPage master = this.Master as MyMasterpage;
master.myMasterMethod();


BTW, the answer to your question is EASILY locatable on google. Learn how to use that resource.
 
Share this answer
 
v2
Comments
aroshlakshan 22-Aug-11 6:33am    
yeah, i figured that much!! thanks!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900