DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   Transferring Variables (http://www.daniweb.com/forums/thread151844.html)

planemaniac Oct 17th, 2008 7:01 pm
Transferring Variables
 
Hi,

I have a really quick question. I have Sub Page_Load(...), and in that sub, I set a couple of variables. In a form, further down the page, in the <body> tag, I have a button, set so when I click it, it operates another Sub, Btn_Click(...). How can I access the variable I set in Page_Load in Btn_Click without using a URL transfer method?

Thanks

George

serkan sendur Oct 17th, 2008 8:21 pm
Re: Transferring Variables
 
just create your fields after your class declaration before any event handlers, then you can reach them from everywhere in your codefile

planemaniac Oct 18th, 2008 5:50 am
Re: Transferring Variables
 
I don't know what you mean by Class Declaration, but what I have is:

<html><head><script language="VB" runat="server">
Sub Page_Load(s As Object, e As EventArgs)
Dim DayNum As Integer
DayNum = 3

End Sub

Sub Btn_Click(s As Object, e As EventArgs)

Response.Write(DayNum)

End Sub
</script>
</head><body>
... HTML ... </body>
</html>

How can I transfer that variable DayNum from the Sub Page_Load to the Sub Btn_Click?

serkan sendur Oct 18th, 2008 7:46 am
Re: Transferring Variables
 
why dont you use code behind files? are you migrating from classical asp or php?

planemaniac Oct 18th, 2008 8:44 am
Re: Transferring Variables
 
Yes, I'm migrating from PHP. Is there anyway I can do this without using Code Behind files, or will I have to?

serkan sendur Oct 18th, 2008 3:09 pm
Re: Transferring Variables
 
yes you can do it without codebehind files but after a while you will confront them everywhere so that will be inevitable. asp.net is organized in code behind manner so just use them.

n4naeem Oct 21st, 2008 7:41 am
Re: Transferring Variables
 
Sub Page_Load(s As Object, e As EventArgs)
Dim DayNum As Integer
DayNum = 3
Session["DayNum"] = DayNum;

End Sub

Sub Btn_Click(s As Object, e As EventArgs)

Response.Write(System.Convert.ToInt32(Session["DayNum"]));

Session["DayNum"] = null;

End Sub


Quote:

Originally Posted by serkansendur (Post 715661)
yes you can do it without codebehind files but after a while you will confront them everywhere so that will be inevitable. asp.net is organized in code behind manner so just use them.



All times are GMT -4. The time now is 1:42 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC