Click to See Complete Forum and Search --> : Web Controls Urget Help!!!!!!!


j-dearden
09-10-2007, 08:07 AM
Guday all

do any of you know how i can call a method from a web user control (.ascx) that is native to the containing page (.aspx, code behind)?

thnks, this realy has got me stumped

Cstick
09-10-2007, 09:32 PM
This works. Maybe you should consider using interface classes if different pages will have differnet methods you want called.

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="WebApplication3.Default2" %>
<%@ register tagprefix="Sample" tagname="InvokeParentMethod" src="~/InvokeParentMethodControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<sample:InvokeParentMethod runat="server"></sample:InvokeParentMethod>
</form>
</body>
</html>

Default2.aspx.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;

namespace WebApplication3
{
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

public void DoSomething()
{
Response.Write("Blah");
}
}
}

InvokeParentMethodControl.ascx.cs

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="InvokeParentMethodControl.ascx.cs" Inherits="WebApplication3.InvokeParentMethodControl" %>
<asp:button id="InvokeButton" runat="server" text="Invoke" />

InvokeParentMethodControl.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;

namespace WebApplication3
{
public partial class InvokeParentMethodControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.InvokeButton.Click += InvokeButton_Click;
}

protected void InvokeButton_Click(object sender, EventArgs e)
{
Default2 page = (Default2)base.Page;
page.DoSomething();
}
}
}