Suppose we have an example1.aspx.cs page containing a method named method1() that performs certain actions. Within method1(), I need to invoke another method directly named method2() residing in a different page, such as example2.aspx.cs or example2.ascx.cs (user control). How can I achieve this in an ASP.NET Web Forms project?

I tried...

example2.this.page.method2();

but it's not working.

1

There are 1 best solutions below

0
Albert D. Kallal On

Well, it kind of begs the question that if you have "some" code in a given web page that BOTH pages now need to share?

Then you should move that given and "shared" code out to a common library class and then both forms can freely call that shared code.

Also, given that just about any code you have in .net is a class, then of course you need an instance of that class before you can call or use such code. So, in effect how this works?

Your question then becomes:

How can I share and call some code in a class?

Well, you either mark the code as static, and thus eliminating the need to first create an instance of that class. The other approach would be to create an instance of that page in code, and then call that method. However, since the page unlikely to be already loaded, then creating a whole instance of that page class is a bit overkill, and worse many (if not all) of the page class events such as page load etc. will run, and you don't want that in most cases.

So, best advice is to move the code out of the page, and now both pages can call that common library routine.

However, if for some strange reason you want to keep the code in that page class, then mark the code as static.

So, say in MyPage1 I want to call and use some code in MyPage2.

So, in MyPage2, we can have say this method to convert Celsius temperature to Fahrenheit.

Hence, in web page MyPage2, we have this code:

    public static decimal CtoF(decimal c)
    {
        // convert passed Celsius temperature value
        // to Fahrenheit

        decimal MyResult = (c * 1.8m) + 32;
        return MyResult;

    }

So, now in page 1, I can have this markup:

        <h3>Enter Celsius value</h3>
        <asp:TextBox ID="txtC" runat="server" TextMode="Number"></asp:TextBox>

        <br />
        <br />
        <asp:Button ID="cmdConvert" runat="server" Text="Convert to F"
            OnClick="cmdConvert_Click"
            />
        <br />

        <h3>Fahrenheit Value</h3>

        <asp:TextBox ID="txtF" runat="server"></asp:TextBox>

And code behind in page 1 is this:

    protected void cmdConvert_Click(object sender, EventArgs e)
    {
        decimal C = Convert.ToDecimal(txtC.Text);
        decimal F = MyPage2.CtoF(C);      // call code routine in MyPage2

        txtF.Text = F.ToString("0.00");
    }

And the result is thus this:

enter image description here

However, as noted, since in theory both pages need to share such code, then it would be far better to move the code and methods you need to share out of the second page and place such code in a shared class as global that all pages and all code can freely use.