Wednesday, March 21, 2012

passing a value into a global variable

hi, i have a declared a global variable at the top of my code. then within a subroutine, i am passing a value into the global variable. when i do a response.write WITHIN the subroutine, the value is being passed into the global variable because the variable prints out the value. now i want this global variable with the value stored in it, to be used within another subroutine. so far i cant get this to work. my code is shown below....

Dim QNo As Integer
Dim QAn As Integer

Sub Begin_Click (Src As Object, E As EventArgs)

'other code goes here......

QNo = objDataReader("QNo1")
QAn = objDataReader("QAn1")

End Sub

Sub Finish_Click (Src As Object, E As EventArgs)

Response.write (QNo)

If QNo = QAn Then
message100.text = "correct"
Else
message100.text = "wrong"
End If
End Sub

this will not work as in the sub Finish_Click, the variable QNo is being recognised as 0 and not the value taken from Begin_Click sub. can anyone tell me how i can pass the value recieved from Begin_Click and store it so i can use it in Finish_Click. thanx...
This is because your events are firing during two seperate instances of your page class. Each time you click a button, a new request is issued to your application, a new instance of your page is created in memory, the new page instance executes and generates HTML output, and finally the new instance of your page is destroyed (removed from memory). The next time you click a button on the page, the cycle starts over again, with the new instance of your page having no knowledge of any previous instances (other than what's available from Viewstate).

You need to maintain state somehow. You can use Session variables, Cookies, Application variables, Cache variables, hidden fields, or persist the values in a database or a file system. The choices are endless (not really but there are lots), and there are many considerations to account for. The simplest solution is to use a Session variable, but I would suggest that you read up on the advantages and disadvantages of each alternative in the SDK help files.

Let me know if you have any questions.

Thanks,

ok im already using session variables as security for accessing my pages so im ok with that but isnt there another more simpler solution to get the value passed into the global variable from the first subroutine. i have been told that you can call the subroutine in the second one where all ther variables are also taken from the first into the second and i will be able to use them. is that possible? if so, then can you give me an example piece of code of how to do it. i am looking for the easiest solution ;), so another source has told me using the hidden fields method is the best way. can you help?
hi thanks for the guidance again mate. looking at session variables method looks quite complicated. and it may cause me some problems because i have a number of values that need to be passed to a number of global variables. i have also been informed to try pass by reference. would this work? isn't there any small few lines of code that will input the values generated from Begin_Click into the global variables so i can use those values in the second subroutine Finish_Click?

The session variables are your easiest solution.

If you want to use a hidden field, just drop one on your design form from the HTML Controls toolbox. Right click on the hidden field and select "Run as Server Control". Take note of the id generated for the hidden control; better yet, change it to a more meaningful name. Using this id, you can assign a value to it in your code behind. If you are not using VS .Net, just give the hidden field an id and specify the runat="server" attribute. Here is a quick sample:


<%@. Page Language="VB" %>
<script runat="server"
Sub Page_Load(sender As Object, e As EventArgs)
txtResult.Text = ""
End Sub

Sub cmdSet_Click(sender As Object, e As EventArgs)
myHiddenField.Value = txtValue.Text
txtResult.Text = "Value has been set."
txtValue.Text = ""
End Sub

Sub cmdGet_Click(sender As Object, e As EventArgs)
txtResult.Text = "Value is " + myHiddenField.Value
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="txtValue" runat="server"></asp:TextBox
<!-- HIDDEN FIELD --> ;)
<input id="myHiddenField" type="hidden" runat="server" />
</p>
<p>
<asp:Button id="cmdSet" onclick="cmdSet_Click" runat="server" Text="Set Value"></asp:Button>
<asp:Button id="cmdGet" onclick="cmdGet_Click" runat="server" Text="Get Value"></asp:Button>
</p>
<p>
<asp:Label id="txtResult" runat="server"></asp:Label>
</p>
</form>
</body>
</html>


Sorry - you lost me here. What is it you want to again?
ok sorry about the bad explanations....anyway i got it working. thanks alot for the hidden fiels example you gave it. i got it working nicely....im doing a project for university so if i com across any more problems (im sure i will) ill poat another thread up....but thanks for the help mate, much appreciated!!!!!

0 comments:

Post a Comment