Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to call a method of one user control in another user control

how to call a method of one user control in another user control

Scheduled Pinned Locked Moved ASP.NET
winformstutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lakshmichawala
    wrote on last edited by
    #1

    iam having two user controls u1 and u2 .i have one public method in u1 .now i want to call this method in u2.how can i handle this one .

    V 1 Reply Last reply
    0
    • L lakshmichawala

      iam having two user controls u1 and u2 .i have one public method in u1 .now i want to call this method in u2.how can i handle this one .

      V Offline
      V Offline
      Vimalsoft Pty Ltd
      wrote on last edited by
      #2

      Remember i order to do that in U2 you must register the U1 and after that you can access your u1 method in U2 like this

      u1.Mymethod();

      Hope this Helps

      Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

      L 1 Reply Last reply
      0
      • V Vimalsoft Pty Ltd

        Remember i order to do that in U2 you must register the U1 and after that you can access your u1 method in U2 like this

        u1.Mymethod();

        Hope this Helps

        Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

        L Offline
        L Offline
        lakshmichawala
        wrote on last edited by
        #3

        this is not working ...is any other way to do that one

        V 1 Reply Last reply
        0
        • L lakshmichawala

          this is not working ...is any other way to do that one

          V Offline
          V Offline
          Vimalsoft Pty Ltd
          wrote on last edited by
          #4

          What is it that is not working ? Tell me what you did because that is how its done.

          Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

          L 1 Reply Last reply
          0
          • V Vimalsoft Pty Ltd

            What is it that is not working ? Tell me what you did because that is how its done.

            Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

            L Offline
            L Offline
            lakshmichawala
            wrote on last edited by
            #5

            hi Maseko, actually in my application there are neary 25 webpages which are using two user controls tat are banner and navgation user control..but in each page the ids of these two controls are different and they have used according to the pagenames..now there is a situation that the method of one user control u1 is required to used in the u2 when a button in the u2 is clicked..

            V D 2 Replies Last reply
            0
            • L lakshmichawala

              hi Maseko, actually in my application there are neary 25 webpages which are using two user controls tat are banner and navgation user control..but in each page the ids of these two controls are different and they have used according to the pagenames..now there is a situation that the method of one user control u1 is required to used in the u2 when a button in the u2 is clicked..

              V Offline
              V Offline
              Vimalsoft Pty Ltd
              wrote on last edited by
              #6

              Good Morning lakshmichawala I will show you an Example of how you do it. i will create a Small Example. First i will create a usercontrol and name i U1 like and it will have a method that will display a string like this cs will look like this

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Web;
              using System.Web.UI;
              using System.Web.UI.WebControls;

              public partial class U1 : System.Web.UI.UserControl
              {
              protected void Page_Load(object sender, EventArgs e)
              {

              }
              
              public String Message()
              {
                  return "Message from U1 User control";
              }
              

              }

              and the html will be like this

              <%@ Control Language="C#" AutoEventWireup="true" CodeFile="U1.ascx.cs" Inherits="U1" %>
              <asp:Label ID="lblU1" runat="server" Text="Usercontrol 1"></asp:Label>

              and second thing is to create a Second user control and name it U2

              <%@ Control Language="C#" AutoEventWireup="true" CodeFile="U2.ascx.cs" Inherits="U2" %>
              <%@ Register src="U1.ascx" tagname="U1" tagprefix="uc1" %>
              <asp:Label ID="lblU2" runat="server" Text="User Control 2"></asp:Label>

              <p>
               </p>
              <p>
               </p>
              <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

              as you can see i have registered the U1 and in my U2 i can use the Function that i have made public like this in the button click

              protected void Button1_Click(object sender, EventArgs e)
              {
              U1 obj = new U1();

                  String Message;
              
                  Message = obj.Message();
              
                  Response.Write(Message);
              
              } 
              

              and a hardcoded message will come from the U1. Hope this helps Thanks

              Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

              1 Reply Last reply
              0
              • L lakshmichawala

                hi Maseko, actually in my application there are neary 25 webpages which are using two user controls tat are banner and navgation user control..but in each page the ids of these two controls are different and they have used according to the pagenames..now there is a situation that the method of one user control u1 is required to used in the u2 when a button in the u2 is clicked..

                D Offline
                D Offline
                DoctorMick
                wrote on last edited by
                #7

                If you're trying to interact between two already loaded instances of different user controls I would possibly use an event. For example, when u2 button is clicked raise an event to the calling page, the calling page can then call the method in u2 as it already knows about it. Alternatively, you can create a property on u2 of type u1 and the consuming page could set the property on u2 to the instance of u1 on the page, u2 will then have access to u1 and its methods.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups