using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
namespace
SimpleCalculator
{
public
class
Calculator
:
WebPart
{
TextBox
tbA;
TextBox
tbB;
TextBox
tbResult;
Button
btnAdd;
Button
btnSub;
protected
override
void
CreateChildControls()
{
base.CreateChildControls();
EnsurePanelFix();
UpdatePanel
refreshCalculation =
new
UpdatePanel
();
ScriptManager
scriptHandler =
new
ScriptManager
();
tbA =
new
TextBox
();
tbB =
new
TextBox
();
tbResult =
new
TextBox
();
btnAdd =
new
Button
();
btnSub =
new
Button
();
tbResult.ReadOnly =
true
;
btnAdd.Text =
"+"
;
btnSub.Text =
"-"
;
scriptHandler.ID =
"scriptHandler"
;
refreshCalculation.ID =
"refreshName"
;
refreshCalculation.UpdateMode = UpdatePanelUpdateMode.Always;
refreshCalculation.ChildrenAsTriggers =
true
;
btnAdd.Click +=
new
EventHandler
(btnAdd_Click);
btnSub.Click +=
new
EventHandler
(btnSub_Click);
refreshCalculation.ContentTemplateContainer.Controls.Add(tbA);
refreshCalculation.ContentTemplateContainer.Controls.Add(newLiteralControl(
"<br>"
));
refreshCalculation.ContentTemplateContainer.Controls.Add(tbB);
refreshCalculation.ContentTemplateContainer.Controls.Add(newLiteralControl(
"<br>"
));
refreshCalculation.ContentTemplateContainer.Controls.Add(btnAdd);
refreshCalculation.ContentTemplateContainer.Controls.Add(btnSub);
refreshCalculation.ContentTemplateContainer.Controls.Add(newLiteralControl(
"<br>"
));
refreshCalculation.ContentTemplateContainer.Controls.Add(tbResult);
this
.Controls.Add(scriptHandler);
this
.Controls.Add(refreshCalculation);
}
void
btnAdd_Click(
object
sender,
EventArgs
e)
{
int
a =
int
.Parse(tbA.Text);
int
b=
int
.Parse(tbB.Text);
int
c = a + b;
tbResult.Text = c.ToString();
}
void
btnSub_Click(
object
sender,
EventArgs
e)
{
int
a =
int
.Parse(tbA.Text);
int
b =
int
.Parse(tbB.Text);
int
c = a - b;
tbResult.Text = c.ToString();
}
private
void
EnsurePanelFix()
{
if
(
this
.Page.Form!=
null
)
{
String
fixupScript =
@"
_spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
function_initFormActionAjax()
{
if(_spEscapedFormAction == document.forms[0].action)
{
document.forms[0]._initialAction =
document.forms[0].action;
}
}
varRestoreToOriginalFormActionCore =
RestoreToOriginalFormAction;
RestoreToOriginalFormAction = function()
{
if(_spOriginalFormAction != null)
{
RestoreToOriginalFormActionCore();
document.forms[0]._initialAction =
document.forms[0].action;
}
}"
;
ScriptManager
.RegisterStartupScript(
this
,
typeof
(
Calculator
),
"UpdatePanelFixup"
,
"_spOriginalFormAction =document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;"
,
true
);
}
}
}
}