如何访问母版页中的公共方法setURL()? [英] how to access the public method setURL() in the master page?
问题描述
我在Site.Master页中有一个公共方法,并且我希望从内容页面"Home.aspx"中使用该方法.
这些是我的密码
Site.master.cs
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
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
Home.aspx
<%@ 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
Home.aspx.cs
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();
}
}
}
我试图通过创建主类的对象并使用此方法来访问该方法
Master.setURL();
但它没有用.谁能分析代码并告诉我如何使用此方法?我想我缺少指令.谁能告诉我它是什么?
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??
推荐答案
由于该方法是非静态的,因此您将必须使一个对象来访问它:)
像这样
Since the method is non-static so you''ll have to make an object to access it :)
Like this
SiteMaster s=new SiteMaster();
s.setURL();
MyMasterPage master = this.Master as MyMasterpage;
master.myMasterMethod();
顺便说一句,您的问题的答案很容易在Google上找到.了解如何使用该资源.
BTW, the answer to your question is EASILY locatable on google. Learn how to use that resource.
这篇关于如何访问母版页中的公共方法setURL()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!