Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Is it possible to call a method defined in AnotherPage.aspx.cs?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gnewsgroup - 25 Oct 2007 19:59 GMT
I know that we can re-use/share code by placing the code in App_Code.

But, is it possible for MyPage.aspx.cs to call a method defined in
AnotherPage.aspx.cs?

Suppose, in AnotherPage.aspx.cs, we define a method which binds a
DropDownList to a database table.  Let's call the method
BindDropDownList().

In MyPage.aspx, we are gonna use the same DropDownList.  Is it
possible, in MyPage.aspx.cs, to simply instantiate AnotherPage and
call BindDropDownList()?  Something like

   // This is MyPage.aspx.cs
   AnotherPage anotherPage = new AnotherPage();
   anotherPage.BindDropDownList();

Does this sound whimsical?

I guess the answer is Nope, and we better write a generick-ish
BindDropDownList and stick it in a class under App_Code, right?
Mark Rae [MVP] - 25 Oct 2007 20:23 GMT
> In MyPage.aspx, we are gonna use the same DropDownList.

Sounds like a perfect candidate for a UserControl...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gnewsgroup - 25 Oct 2007 20:39 GMT
> > In MyPage.aspx, we are gonna use the same DropDownList.
>
[quoted text clipped - 3 lines]
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

Thanks, that must be the solution. I find it hard to pass variables
amongst user controls, though.
Mark Rae [MVP] - 25 Oct 2007 20:49 GMT
>> > In MyPage.aspx, we are gonna use the same DropDownList.
>>
>> Sounds like a perfect candidate for a UserControl...
>
> Thanks, that must be the solution. I find it hard to pass variables
> amongst user controls, though.

Just use public properties...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gnewsgroup - 26 Oct 2007 14:59 GMT
> >> > In MyPage.aspx, we are gonna use the same DropDownList.
>
[quoted text clipped - 8 lines]
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

I've tried out your idea, and it was successful.  That's great, but,
what about event-raising and event-consuming amongst user controls?

Say, I want user control A to respond to a click event of user control
B.  Public properties won't do the trick, right?
Mark Rae [MVP] - 26 Oct 2007 16:23 GMT
> I've tried out your idea, and it was successful.  That's great, but,
> what about event-raising and event-consuming amongst user controls?
>
> Say, I want user control A to respond to a click event of user control
> B.  Public properties won't do the trick, right?

Ah yes - for that, you need delegates:
http://www.netomatix.com/development/usercontrols3.aspx

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gnewsgroup - 26 Oct 2007 17:41 GMT
> > I've tried out your idea, and it was successful.  That's great, but,
> > what about event-raising and event-consuming amongst user controls?
[quoted text clipped - 7 lines]
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

Fantastic.  I did write a user control which has a GridView.  This
GridView gets populated with customer names retrieved from the
database.  The customer names in the GridView are made to be
LinkButtons (in template fields).  When such a LinkButton'ed name is
clicked, I want to send the name as a string to the parent control.

Let's call this user control SearchCustomers.ascx.

I followed the example you gave in the link above, and successfully
made the whole thing happen, which kinda excited me.  Thank you.

But, I do not want to show SearchCustomers user control in the parent
page until a button is clicked in the parent page.

I have written a Visible property in SearchCustomers.ascx.cs, like so:

   private bool visible;

   protected void Page_Load(object sender, EventArgs e)
   {
       if (this.visible)
       {
           Panel1.Visible = true;  // Pane1 holds the GridView.
       }
       else
       {
           Panel1.Visible = false;
       }
   }

   public bool Visible
   {
       get
       {
           return this.visible;
       }
       set
       {
           this.visible = value;
       }
   }

In ParentPage.aspx's Page_Load, if I say

            SearchCustomers1.Visible = true;

This control does show up nicely, but if I attempt to set the Visible
property in the button click event handler like so:

protected void btnSearchCustomers_Click(object s, EventArgs e)
{
          SearchCustomers1.Visible = true;
}

The SearchCustomers1 user control does not show up.  I kinda suspect
that it is because it has passed the Page_Load event, so the control
cannot be rendered.  In any case, how do I show/hide a user control
dynamically?  Somehow, I guess this user control has to be recreated /
reloaded, but what do I need to do?

Thank you again if you could share your wisdom.
Mark Rae [MVP] - 26 Oct 2007 19:31 GMT
> The SearchCustomers1 user control does not show up.  I kinda suspect
> that it is because it has passed the Page_Load event, so the control
> cannot be rendered.  In any case, how do I show/hide a user control
> dynamically?  Somehow, I guess this user control has to be recreated /
> reloaded, but what do I need to do?

I suspsect you're right.

It's a little difficult to tell without the rest of the app around the above
code, but I'd start by setting a breakpoint at the beginning line of each
method, and watching the Visible property. Remember that when you set a
control's Visible property to false server-side, it doesn't hide the
control - it actually doesn't render it at all, so it never gets streamed
down to the browser...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gnewsgroup - 26 Oct 2007 20:32 GMT
> > The SearchCustomers1 user control does not show up.  I kinda suspect
> > that it is because it has passed the Page_Load event, so the control
[quoted text clipped - 14 lines]
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

Thank you.  I only recently realized that setting Visible to false
server-side will cause the server not to render it.

Well, the trick is to put SearchCustomser1 on a Panel, and then show/
hide the panel instead.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.