如何覆盖页面加载事件 [英] How to override Page load event
问题描述
我有一个名为'Maths'的页面继承自ASP.NET页面。并有一个包含该类的所有代码的包装类。现在当我点击一个按钮时,第一个控件转到'Maths'的页面加载,然后只进入包装类。我只想去包装类。你能帮我的吗?我已经将页面背后的代码设置为包装类。
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.
<%@ 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>
M. aths控制器是
Maths controller is
<%@ 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
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
推荐答案
问题是当单击按钮时,页面信息被发布回来。这会触发重新加载的页面,该页面将经历页面生命周期。如果您以这种方式发布页面,则无法避免。
您可能想要研究创建WebService?这可以使用Ajax从客户端调用,并在javascript中处理。无需重新加载页面。
您的第一个C#Web服务 [ ^ ]
如何从中调用Web服务使用ASP.NET AJAX的客户端JavaScript [ ^ ]
另一种选择是使用一个UpdatePanel。这将调用page_load,但不会导致面板外的任何内容发生变化。
Ajax更新面板 - 最佳实践1 [ ^ ]
最后,如果Page_Load中发生了一些您不想再做的事情,那么请尝试使用IsPostBack布尔值。如果Page_Load由于页面事件而发生,则此值为false首页加载的页面为true
了解ASP.NET中回发的完整故事 [ ^ ]
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[^]
这篇关于如何覆盖页面加载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!