|
 |
aspx_beginners thread: Questions While I studying "Objects in ASP.Net"
Message #1 by "Chong Kim" <rimuris@h...> on Fri, 21 Mar 2003 16:42:50
|
|
While i'm practicing about Objects in the asp.net,
I got some questions.
1. What is different between
a. public sub Click (ByVal Sender As Object, ByVal e as
system.EventArgs)
b. public sub Click (ByRef Sender As Object, ByRef e as
System.EventArgs)
c. public sub Click (Sender As Object,e as System.EventArgs)
???
2. "If not isPostBack then" .. what's "isPostBack" for?
<%@ page language="vb" %>
<script language="VB" runat="server">
Sub page_load (Source As Object, E as EventArgs)
If not isPostBack then
MyButton.Text = "OK"
MyDropdownList.items.add
("http://www.microsoft.com")
MyDropdownList.items.add("http://www.wrox.com")
MyDropdownList.items.add
("http://msdn.microsoft.com")
End if
end sub
public sub Click (ByVal Sender As Object, ByVal e as
System.EventArgs)
Response.redirect (myDropdownlist.selecteditem.text)
end sub
</script>
Message #2 by "Robert Sindall" <rsindall@z...> on Fri, 21 Mar 2003 16:37:44 -0000
|
|
1. do you mean: whats the difference between: ByVal and ByRef
2. Do you know what a postback is?
Message #3 by "Peter Lanoie" <planoie@n...> on Fri, 21 Mar 2003 11:57:52 -0500
|
|
When you have a function/sub with arguments, you have the option of passing
the values to the argument by Reference (ByRef) or by Value (ByVal).
Consider this simple code...
Using ByVal:
Sub MySub(ByVal MySubString as String)
MySubString = "good bye"
End Sub
Now I do this...
Dim MyString as String
MyString = "Hello"
MySub(MyString)
Print MyString 'This results in: "Hello"
Explained: ByVal says Take the VALUE passed and put it in this new variable.
In the sub, I modify the contents of the NEW variable but after the call to
the sub I'm still looking at the original.
Using ByRef:
Sub MySub(ByRef MySubString as String)
MySubString = "Good Bye"
End Sub
Now I do this...
Dim MyString as String
MyString = "Hello"
MySub(MyString)
Print MyString 'This results in: "Good Bye"
Reason: Now when I pass the argument to the sub, I'm passing a REFERENCE to
the original variable. So any modifications I make to the variable IN the
sub will be made to the original. So after the call to the sub, MyString
has been changed.
Now for the postback question...
When you make an initial request to a web page, there is nothing in the POST
data of the request. When you make any .Net request, i.e. you have a web
form, and you press a button which causes a postback, all the form data is
in the POST data of the request. If you are familiar with Classic ASP,
isPostBack is essentially the same as
"Request.ServerVariables("CONTENT_LENGTH") > 0". CONTENT_LENGTH is the
length of the post data. This says "yup, this request is a postback
request.
The importance of using isPostBack is visible when you start doing things
like adding items to some server control that's on the page (like the
dropdown list in your example). For example, you have a select box on the
form and in page_load() you have several occurrences of
"selectBox.items.add(...)". When page_load gets called, all your items are
added to the select box. This happens EVERY time the page loads (on the
first request and subsequent postback requests). This in itself is not a
problem... but... in dot net, a default server control gets added to the
form with "EnableViewstate" set to true. When the page is drawn the first
time, all the items in the control (those you added in page_load()) become
part of the encoded view state value that is saved in the form. On
postback, ASP.Net recreates the select box from the viewstate. This means,
all those items you added the FIRST time are already in there. Then you
also have the ones that you are programmatically adding into it in
page_load(). Now you'll have twice as many as you'd expect. Each time you
postback the form, you'll end up with as many more items as there are in the
page_load routine. SO... to prevent this, you use "If Not isPostBack
Then..." in page_load around the code that adds the items to the list.
-----Original Message-----
From: Chong Kim [mailto:rimuris@h...]
Sent: Friday, March 21, 2003 16:43
To: aspx_beginners
Subject: [aspx_beginners] Questions While I studying "Objects in
ASP.Net"
While i'm practicing about Objects in the asp.net,
I got some questions.
1. What is different between
a. public sub Click (ByVal Sender As Object, ByVal e as
system.EventArgs)
b. public sub Click (ByRef Sender As Object, ByRef e as
System.EventArgs)
c. public sub Click (Sender As Object,e as System.EventArgs)
???
2. "If not isPostBack then" .. what's "isPostBack" for?
<%@ page language="vb" %>
<script language="VB" runat="server">
Sub page_load (Source As Object, E as EventArgs)
If not isPostBack then
MyButton.Text = "OK"
MyDropdownList.items.add
("http://www.microsoft.com")
MyDropdownList.items.add("http://www.wrox.com")
MyDropdownList.items.add
("http://msdn.microsoft.com")
End if
end sub
public sub Click (ByVal Sender As Object, ByVal e as
System.EventArgs)
Response.redirect (myDropdownlist.selecteditem.text)
end sub
</script>
Message #4 by "Chong Kim" <rimuris@h...> on Fri, 21 Mar 2003 19:50:24
|
|
Thank you so much. That was great help to me!!
> When you have a function/sub with arguments, you have the option of
passing
the values to the argument by Reference (ByRef) or by Value (ByVal).
Consider this simple code...
Using ByVal:
Sub MySub(ByVal MySubString as String)
MySubString = "good bye"
End Sub
Now I do this...
Dim MyString as String
MyString = "Hello"
MySub(MyString)
Print MyString 'This results in: "Hello"
Explained: ByVal says Take the VALUE passed and put it in this new
variable.
In the sub, I modify the contents of the NEW variable but after the call to
the sub I'm still looking at the original.
Using ByRef:
Sub MySub(ByRef MySubString as String)
MySubString = "Good Bye"
End Sub
Now I do this...
Dim MyString as String
MyString = "Hello"
MySub(MyString)
Print MyString 'This results in: "Good Bye"
Reason: Now when I pass the argument to the sub, I'm passing a REFERENCE to
the original variable. So any modifications I make to the variable IN the
sub will be made to the original. So after the call to the sub, MyString
has been changed.
Now for the postback question...
When you make an initial request to a web page, there is nothing in the
POST
data of the request. When you make any .Net request, i.e. you have a web
form, and you press a button which causes a postback, all the form data is
in the POST data of the request. If you are familiar with Classic ASP,
isPostBack is essentially the same as
"Request.ServerVariables("CONTENT_LENGTH") > 0". CONTENT_LENGTH is the
length of the post data. This says "yup, this request is a postback
request.
The importance of using isPostBack is visible when you start doing things
like adding items to some server control that's on the page (like the
dropdown list in your example). For example, you have a select box on the
form and in page_load() you have several occurrences of
"selectBox.items.add(...)". When page_load gets called, all your items are
added to the select box. This happens EVERY time the page loads (on the
first request and subsequent postback requests). This in itself is not a
problem... but... in dot net, a default server control gets added to the
form with "EnableViewstate" set to true. When the page is drawn the first
time, all the items in the control (those you added in page_load()) become
part of the encoded view state value that is saved in the form. On
postback, ASP.Net recreates the select box from the viewstate. This
means,
all those items you added the FIRST time are already in there. Then you
also have the ones that you are programmatically adding into it in
page_load(). Now you'll have twice as many as you'd expect. Each time you
postback the form, you'll end up with as many more items as there are in
the
page_load routine. SO... to prevent this, you use "If Not isPostBack
Then..." in page_load around the code that adds the items to the list.
-----Original Message-----
From: Chong Kim [mailto:rimuris@h...]
Sent: Friday, March 21, 2003 16:43
To: aspx_beginners
Subject: [aspx_beginners] Questions While I studying "Objects in
ASP.Net"
While i'm practicing about Objects in the asp.net,
I got some questions.
1. What is different between
a. public sub Click (ByVal Sender As Object, ByVal e as
system.EventArgs)
b. public sub Click (ByRef Sender As Object, ByRef e as
System.EventArgs)
c. public sub Click (Sender As Object,e as System.EventArgs)
???
2. "If not isPostBack then" .. what's "isPostBack" for?
<%@ page language="vb" %>
<script language="VB" runat="server">
Sub page_load (Source As Object, E as EventArgs)
If not isPostBack then
MyButton.Text = "OK"
MyDropdownList.items.add
("http://www.microsoft.com")
MyDropdownList.items.add("http://www.wrox.com")
MyDropdownList.items.add
("http://msdn.microsoft.com")
End if
end sub
public sub Click (ByVal Sender As Object, ByVal e as
System.EventArgs)
Response.redirect (myDropdownlist.selecteditem.text)
end sub
</script>
|
|
 |