Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
User control call page method?
Message
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 8.0
Miscellaneous
Thread ID:
01374382
Message ID:
01374432
Views:
18
I'm missing something glaringly obvious (not to me but I'm sure it is simple).


You are doing..

Dim page As MyWebPage


My page is Default.aspx which is class _Default

If I try

Dim page as _Default

I get an error "Type '_Default' is not defined"


??



>>I have several user controls (.ASCX) that exist on a page.
>>
>>In each, when an event happens (sqldatasource.inserted), I need to refresh the data in a control on the main (parent) page.
>>
>
>>Can someone explain this to me in some rational terms please?
>>
>
>(hopefully the VB code works - I'm normally working in C#)
>
>.NET is strongly typed. That means you can't just grab a generic "Page" object from your ASCX class and call some method that you added in some subclass (your ASPX) of the page - it doesn't exist at the Page class level. Therefore you need to cast the Page object into your specific subclass of the page in order for it to "know"/compile that the method you're calling actually exists.
>
>There are a few ways of doing this - which one you picks kind of depends on how the controls are going to be used, how much work you want to do, how tightly coupled you want to make things, etc.
>
>Let's start with the one that's the least flexible, since it's the simplest. Assume your user control "knows" about the page it's going to be used on.
>
>Let's suppose we have a public method in the ASPX code-behind that updates a control on the page.
>
>public void UpdateText(string newText)
>{
> this.lblInfo.Text = newText;
>}
>
>We want to call this method from an ASCX page when something is changed. So from within the event we can do this:
>
>
>MyWebPage page = Page as MyWebPage;
>
>if (page != null)
>   page.UpdateText("New Text");
>
>
>
>Dim page As MyWebPage 
>
>If Page <> Nothing Then
>   page.UpdateText("New Text");
>End If
>
>
>That works OK but it limits the control to only working on that specific page. One way around that is to create an interface for the page which requires a page to implement an "UpdateText" method, ex:
>
>
>   public interface IMyWebPage
>    {
>        void UpdateText(string newText);
>    }
>
>    public partial class MyWebPage : System.Web.UI.Page, IMyWebPage
>    {
>        protected void Page_Load(object sender, EventArgs e)
>        {
>
>        }
>
>        public void UpdateText(string newText)
>        {
>            // Code specific to this page does something with the newText passed in.
>        }
>    }
>
>
>
>
>Public Interface IMyWebPage
>   Sub UpdateText(ByVal newText As String)
>End Interface
>
>Public Partial Class MyWebPage
>   Inherits System.Web.UI.Page
>   Implements IMyWebPage
>
>  Public Sub UpdateText(ByVal newText As String)
>     '' code spec.
>   End Sub
>
>
>We'd then change the code in the ASCX file to:
>
>
>
>IMyWebPage page = Page as IMyWebPage;
>
>if (page != null)
>   page.UpdateText("New Text");
>
>
>
>Dim page As IMyWebPage 
>
>If Page <> Nothing Then
>   page.UpdateText("New Text");
>End If
>
>
>
>
>Yet another way would be to create an event on the user control which fires, then bind to it from the ASPX page. If you need an example for that, let me know.
____________________________________

Don't Tread on Me

Overthrow the federal government NOW!
____________________________________
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform