locked
Call a procedure thats in another form RRS feed

  • Question

  • User-305496339 posted

    Hi friends,

    I was wondering if I have a sub procedure that's in one form how do I call it from a different form. I don't know if the development environment requires some settings that I havent set but I have tried several how to's that  none of them work. And in my case this procedure references controls like the report viewer which has
    added complexity to this problem. Any help would be greatly appreciated.

    Wednesday, November 13, 2019 10:57 PM

Answers

  • User475983607 posted

    The standard approach is moving shared code into a library or class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 13, 2019 11:54 PM
  • User475983607 posted

    Yes, but what about if a procedure references a control like me.reportviewer. Do I pass a reference to the form that is using the procedure?

    The code needs to be refactored so it can be moved to a library or class.  Perhaps share your source code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 14, 2019 12:20 AM
  • User665608656 posted

    Hi rkrex,

    According to your description, if you want to do this in webform, you cannot directly call a control in another form from one form, you can only transfer data to it.

    If you are using WinForm, please provide detailed code design and your specific requirements, which will help us solve your issue easily.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 14, 2019 7:53 AM
  • User475983607 posted

    Are these answers to my questions or is something wrong? I was just wanting to call a sub procedure that is on one form from other forms. I have done this before but its been awhile back I just thought someone knew off of the top of their head. I will research how to do this. Thanks !!!!

    I think the problem is you do not understand how ASP.NET Web Forms works.  You can try newing the page class and invoke whatever method you like.   

        public partial class _Default : Page
        {
            public string MyMethod()
            {
                return "Hello World";
            }
        public partial class OtherPage: System.Web.UI.Page
        {
            public _Default page = new _Default();
           
            protected void Page_Load(object sender, EventArgs e)
            {
               string reslut = page.MyMethod();
            }

    There is no guarantee this will work as expected because a web form is usually instantiate through a request which causes the page to run through the page life cycle.  Commonly, the Page_Load initializes page members including data bound controls.  The end result is an HTML response that the browser loads.

    As stated several times, the standard design approach, is placing shared code in a location that both pages can access.  This is usually a class or a library.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2019 4:01 PM
  • User475983607 posted

    rkrex

    Someone suggested I use a web method. Its really not that big of a deal. I have done this before and obviously I have created class libraries.  I just thought I would ask, and I thought it may matter if I chose to keep this method as a  property of the web form because it references controls in that form.  Thanks !!!

    ASP.NET Web Form controls are protected and initialized through the page life cycle.  While it is possible to populate page members through methods and properties, it's much easier to just craft a library or class is shared between multiple pages.  This is often referred to a a layered design approach where there are defined data access and business logic libraries.

    Web methods are static and not an option for accessing instance members of the page class.  You must pass the values to a web method.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2019 5:31 PM

All replies

  • User475983607 posted

    The standard approach is moving shared code into a library or class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 13, 2019 11:54 PM
  • User-305496339 posted

    Yes, but what about if a procedure references a control like me.reportviewer. Do I pass a reference to the form that is using the procedure?

    Thursday, November 14, 2019 12:00 AM
  • User475983607 posted

    Yes, but what about if a procedure references a control like me.reportviewer. Do I pass a reference to the form that is using the procedure?

    The code needs to be refactored so it can be moved to a library or class.  Perhaps share your source code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 14, 2019 12:20 AM
  • User665608656 posted

    Hi rkrex,

    According to your description, if you want to do this in webform, you cannot directly call a control in another form from one form, you can only transfer data to it.

    If you are using WinForm, please provide detailed code design and your specific requirements, which will help us solve your issue easily.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 14, 2019 7:53 AM
  • User-305496339 posted

    Thanks for your help, but for some strange reason intellisense does not list the other form when I type it in the editor. I don't know if I have some setting off in my IDE.

    Thursday, November 14, 2019 3:47 PM
  • User409696431 posted

    ???  What are you typing, where, that you expect intellisence to know the names of your other webforms?  Why are you looking for your the name of another webform?

    Sunday, November 17, 2019 1:21 PM
  • User-305496339 posted

    Are these answers to my questions or is something wrong? I was just wanting to call a sub procedure that is on one form from other forms. I have done this before but its been awhile back I just thought someone knew off of the top of their head. I will research how to do this. Thanks !!!!

    Monday, November 18, 2019 3:37 PM
  • User475983607 posted

    Are these answers to my questions or is something wrong? I was just wanting to call a sub procedure that is on one form from other forms. I have done this before but its been awhile back I just thought someone knew off of the top of their head. I will research how to do this. Thanks !!!!

    I think the problem is you do not understand how ASP.NET Web Forms works.  You can try newing the page class and invoke whatever method you like.   

        public partial class _Default : Page
        {
            public string MyMethod()
            {
                return "Hello World";
            }
        public partial class OtherPage: System.Web.UI.Page
        {
            public _Default page = new _Default();
           
            protected void Page_Load(object sender, EventArgs e)
            {
               string reslut = page.MyMethod();
            }

    There is no guarantee this will work as expected because a web form is usually instantiate through a request which causes the page to run through the page life cycle.  Commonly, the Page_Load initializes page members including data bound controls.  The end result is an HTML response that the browser loads.

    As stated several times, the standard design approach, is placing shared code in a location that both pages can access.  This is usually a class or a library.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2019 4:01 PM
  • User-305496339 posted

    Someone suggested I use a web method. Its really not that big of a deal. I have done this before and obviously I have created class libraries.  I just thought I would ask, and I thought it may matter if I chose to keep this method as a  property of the web form because it references controls in that form.  Thanks !!!

    Monday, November 18, 2019 5:02 PM
  • User475983607 posted

    rkrex

    Someone suggested I use a web method. Its really not that big of a deal. I have done this before and obviously I have created class libraries.  I just thought I would ask, and I thought it may matter if I chose to keep this method as a  property of the web form because it references controls in that form.  Thanks !!!

    ASP.NET Web Form controls are protected and initialized through the page life cycle.  While it is possible to populate page members through methods and properties, it's much easier to just craft a library or class is shared between multiple pages.  This is often referred to a a layered design approach where there are defined data access and business logic libraries.

    Web methods are static and not an option for accessing instance members of the page class.  You must pass the values to a web method.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2019 5:31 PM