Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet249/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   245   246   247   248   249   250   251   252   ...   453
234

CHAPTER 11
Developing ASp.neT ApplicATionS
# Perform the task and return the result.
Value = Value + Addition
txtResult.Text = str(Value)
# Respond to a Subtraction button click.
def btnSub_Click(sender, e):
# Get the current value.
Value = Int32.Parse(txtResult.Text)
# Reset the error message label.
lblError.Visible = False
# Obtain the new input value.
try:
Subtract = Int32.Parse(txtInput.Text)
except:
# Display an error message when necessary.
lblError.Visible = True
return
# Perform the task and return the result.
Value = Value - Subtract
txtResult.Text = str(Value)
# Respond to a Multiplication button click.
def btnMul_Click(sender, e):
# Get the current value.
Value = Int32.Parse(txtResult.Text)
# Reset the error message label.
lblError.Visible = False
# Obtain the new input value.
try:
Multiply = Int32.Parse(txtInput.Text)
except:
# Display an error message when necessary.
lblError.Visible = True
return
# Perform the task and return the result.
Value = Value * Multiply
txtResult.Text = str(Value)
# Respond to a Division button click.
def btnDiv_Click(sender, e):
# Get the current value.
lISTINg 11-2 
(continued)
548592c11.indd 234
2/24/10 12:48:40 PM
www.finebook.ir


Building a Basic ASP.NET Site Using IronPython
 

 
235
Value = Int32.Parse(txtResult.Text)
# Reset the error message label.
lblError.Visible = False
# Obtain the new input value.
try:
Divide = Int32.Parse(txtInput.Text)
except:
# Display an error message when necessary.
lblError.Visible = True
return
# Perform the task and return the result.
Value = Value / Divide
txtResult.Text = str(Value)
# Respond to a Clear button click.
def btnClear_Click(sender, e):
txtResult.Text = ‘0’
The code begins by importing the required assemblies. As with any IronPython application, you 
can use a combination of Python modules and .NET assemblies to create your application. You also 
have full access to both Python and .NET functionality in your application, so the considerable flex-
ibility that IronPython provides is still available in this environment.
Each of the event handlers must provide both the 
sender
and 
e
arguments as shown. You don’t 
include a 
self
argument in this case, as you would with other IronPython code. As you might expect, 
the 
sender
argument contains a reference to the control that called the event handler, while 
e
contains 
a list of event arguments (normally set to 
None
).
The four math buttons begin by obtaining the current value of 
txtResult
(the output 
TextBox
) as 
an 
Int32
value. Because 
txtResult
is read-only, you don’t need to worry about someone putting an 
incorrect value into it. Consequently, this task doesn’t provide any error trapping code.
The next step is to obtain the new value for the math operation from 
txtInput
. In this case, you’re 
relying on the user to provide the correct input value, which means that the application code could 
receive anything. Someone might even try to enter a script in order to fool your application into 
doing something improper. Using the 
Int32.Parse()
method means that any input other than a 
number triggers an exception, which your code can handle by simply not processing the input. The 
try...except
structure does just that. If the user inputs an incorrect value, the Web page displays 
an error message, rather than doing anything with the input. 
Now that the code has two inputs to process, it performs the required math operation. After the 
math operation is complete, the code outputs the result to 
txtResult.Text
.
The 
btnClear_Click()
event handler is relatively simple. All it does is place a 0 in 
txtResult 
.Text
. The next math operation starts with a zero value, which means that 
txtResult
is cleared.
548592c11.indd 235
2/24/10 12:48:40 PM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   245   246   247   248   249   250   251   252   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish