Adgang til en UserControl fra en anden UserControl
Jeg har fundet denne tutorial på nettet og déns eksempel virker finthttp://fredrik.nsquared2.com/viewpost.aspx?PostID=377
- dog kan jeg ikke få mit eget eksempel til at virke. Jeg har et multiview i én UserControl og og linkene dertil i en anden.
Men det er ligesom at den ikke får adgang UserControlen med multiview'et da jeg får den samme fejlmeddelse hver gang:
Navnet 'MultiView1' findes ikke i den aktuelle sammenhæng *
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<%@ Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id=Head1 runat="server">
<title>Reference</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:WebUserControl id="WebUserControl1" runat="server" />
<uc2:WebUserControl2 ID="WebUserControl2_1" runat="server" />
</form>
</body>
</html>
WebUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<%@ Reference VirtualPath="~/WebUserControl2.ascx" %>
<asp:ImageButton ID="lnkTab1"
ImageUrl="grafik/mail.jpg"
onmouseover="this.src='grafik/mail_over.jpg';"
onmouseout="this.src='grafik/mail.jpg';" AlternateText="Kontakt"
OnClick="mvKontakt_Click"
runat="server" />
<asp:ImageButton ID="lnkTab2"
ImageUrl="grafik/sitemap.jpg"
onmouseover="this.src='grafik/sitemap_over.jpg';"
onmouseout="this.src='grafik/sitemap.jpg';" AlternateText="Sitemap"
OnClick="mvSiteMap_Click"
runat="server" />
<asp:ImageButton ID="lnkTab3"
ImageUrl="grafik/cms.jpg"
onmouseover="this.src='grafik/cms_over.jpg';"
onmouseout="this.src='grafik/cms.jpg';" AlternateText="Login"
OnClick="mvLogIn_Click"
runat="server" />
WebUserControl.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
WebUserControl2 w = Page.FindControl("WebUserControl2_1") as WebUserControl2;
MultiView1.ActiveViewIndex = 0; <---- *
}
}
protected void mvKontakt_Click(object sender, EventArgs e)
{
WebUserControl2 w = Page.FindControl("WebUserControl2_1") as WebUserControl2;
MultiView1.ActiveViewIndex = 1;
}
protected void mvSiteMap_Click(object sender, EventArgs e)
{
WebUserControl2 w = Page.FindControl("WebUserControl2_1") as WebUserControl2;
MultiView1.ActiveViewIndex = 2;
}
protected void mvLogIn_Click(object sender, EventArgs e)
{
WebUserControl2 w = Page.FindControl("WebUserControl2_1") as WebUserControl2;
MultiView1.ActiveViewIndex = 3;
}
}
WebUserControl2.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl2" %>
<asp:MultiView ID=MultiView1 runat="server">
<asp:View ID=View0 runat="server">
Hovedindhold</asp:View>
<asp:View ID=View1 runat="server">
Kontakt</asp:View>
<asp:View ID=View2 runat="server">
Sitemap</asp:View>
<asp:View ID=View3 runat="server">
Login</asp:View>
</asp:MultiView>