Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Page named 'Maths' inheriting from ASP.NET Page. And have a wrapper class containing all the codes for the page. Now when I am clicking a button first control goes to the page load of the 'Maths', then only it goes to the wrapper class. I want only to go to the wrapper class. Can you please help me that. I have already set code behind of the page as wrapper class.

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" Inherits="Wrapper.Maths" CodeBehind="~/WrapSample.cs" %>
<%@ Register TagName="MathsController" TagPrefix="MathsControllerUC" Src="~/MathsController.ascx"  %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body onload="">
    <form id="form1" runat="server" >
        <div>
    <MathsControllerUC:MathsController id="text"  runat="server">

    </MathsControllerUC:MathsController>
            </div>
    </form>
   
</body>
</html>

Maths controller is
ASP.NET
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="~/WrapSample.cs" Inherits="Wrapper.MathsController" %>
<div>
    <asp:TextBox ID="txtFirst" runat="server">0</asp:TextBox>
</div>
<div>
    <asp:TextBox ID="txtSecond" runat="server">0</asp:TextBox>
</div>
<div>
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click"  Width="42px" />
    
    <asp:Button ID="btnSubtract" runat="server" Text="Sub" OnClick="btnSubtract_Click" Width="42px" />
    
    <asp:Button ID="btnMultiple" runat="server" Text="Mul" OnClick="btnMult_Click"  Width ="42px" />
    
    <asp:Button ID="btnDivide" runat="server" Text="Div"  OnClick="btnDivide_Click" Width="42px" />
    
</div>


this is my page and my class is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Wrapper
{
       public partial class MathsController : System.Web.UI.UserControl
    {
        protected override void Page_Load( EventArgs e)
        {

        }
        protected override void OnInit()
        {

        }
    
        public class WrapSample
        {
            public WrapSample()
            {

            }
            public int Add(int First, int Second)
            {
                WrapSample.inside obj = new WrapSample.inside();
                int sum = obj.add(First, Second);
                return sum;
            }
            public int Sub(int First, int Second)
            {
                WrapSample.inside obj = new WrapSample.inside();
                int sum = obj.Sub(First, Second);
                return sum;
            }
            public int Mul(int First, int Second)
            {
                WrapSample.inside obj = new WrapSample.inside();
                int sum = obj.Mul(First, Second);
                return sum;
            }
            public void Div(int First, int Second, out int res, out int rem)
            {
                WrapSample.inside obj = new WrapSample.inside();
                obj.Div(First, Second, out res, out rem);
            }

            private class inside
            {

                public int add(int x, int y)
                {

                    return x + y;
                }
                public int Sub(int x, int y)
                {

                    return x - y;
                }
                public int Mul(int x, int y)
                {

                    return x * y;
                }
                public void Div(int x, int y,out int Res,out int rem)
                {
                    Res = x / y;
                    rem = x % y;
                }
            }
        }
    
        protected void btnSubtract_Click(object sender, EventArgs e)
        {
            try
            {
                WrapSample obj = new WrapSample();
                Response.Write(obj.Sub(int.Parse(txtFirst.Text), int.Parse(txtSecond.Text)).ToString());
            }
            catch (Exception ex)

            {
                Response.Write(ex.Message);
            }
        }
        
        protected void btnMult_Click(object sender, EventArgs e)
        {
            try
            {
                WrapSample obj = new WrapSample();
                Response.Write(obj.Mul(int.Parse(txtFirst.Text), int.Parse(txtSecond.Text)).ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }


        protected void btnDivide_Click(object sender, EventArgs e)
        {
            try
            {
                int res, rem;
                WrapSample obj = new WrapSample();
                obj.Div(int.Parse(txtFirst.Text), int.Parse(txtSecond.Text),out res,out rem);
                Response.Write("Quotient is "+res);
                Response.Write("\nReminder is " + rem);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                
                
                WrapSample obj = new WrapSample();
                int a=int.Parse(txtFirst.Text);
                int b=int.Parse(txtSecond.Text);
                Response.Write((obj.Add(a,b)).ToString());
                
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}

when button click it first goes to the page load function that is in the another page.
I want only go to the class itself. How can I override page load in the code behind class
Posted
Updated 1-Apr-15 22:28pm
v5
Comments
Andy Lanng 2-Apr-15 4:03am    
Not entirely sure what you mean. Could you post the code-behind so we can see what state it is currently in, please.

Thanks
Timberbird 2-Apr-15 4:38am    
If I understand you correctly, you want to call wrapper class method when button is pressed - and WITHOUT generating Page_Load event? Then I'd change your question as following: "How to call server method from client without running common page lifecycle" (yes, Page_Load is not the only method being executed, see here). In this case you have a number of possibilities - you can implement web method and call it from client with AJAX, or move the call to a service, or event create a specific handler for this purpose.
Baiju christadima 2-Apr-15 4:49am    
Thanks. Timberbird
Andy Lanng 2-Apr-15 4:55am    
I Agree with Timberbird (thanks for the code. It's much clearer now ^_^)

1 solution

The problem is that when the button is clicked, the page info is "Posted" back. This triggers the page to reload which will go through the page life-cycle. If you Post a page in this way then it cannot be avoided.

You probably want to look into creating a WebService? This can be called from the client side using Ajax and dealt with in javascript. No page reload required.
Your first C# Web Service[^]
How to call a Web Service from client-side JavaScript using ASP.NET AJAX[^]


Another alternative is to use an UpdatePanel. This will call the page_load but won't cause anything outside of the panel to change.
Ajax update panel- best practice 1[^]

Finally, If there is something happening in the Page_Load that you don't want to do a second time then try using the IsPostBack boolean. This value is false what the page is first loaded and true if the Page_Load has occurred as a result of a page event
Understanding The Complete Story of Postback in ASP.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900