Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code behind

Status
Not open for further replies.

ifntech

Programmer
Nov 4, 2004
80
US
How do you pass session variables in asp.net if code behind is not supported by the web developer studio?
 
What IDE are you using? If it's Visual Web Developer then there is an option to separate code into a code-behind page when you first create the page (checkbox at the bottom).

Otherwise I really don't know the answer to your question (I only use code-behind ;-) I'm assuming the same way as usual, just in a script block...

session("whatever") = ds
 
I am using Visual Web Developer 2005. At the bottom it has an option of putting code in a separate file. The choice is VB or C#.
When I use script, it seems to have no effect on the performance of the interface files.
<script>
session("whatver") = ds
</script>
Besides, I need to pass "whatever" it from page to page. Placing it on every page has no effect, when it is in the script.
 
Sessions are persistant through pages, but you need to create it somewhere, and in your code I don't believe that you have it in any sub, like "onLoad()", "onClick()", etc. So you probably aren't creating the session in the first place.

When you want to get it in another page then you do the opposite

ds = session("whatever")
- ds is for a dataset, but you can store whatever in a session


As far as the performance, that is not really what it is about. It is more about separation of layers and tiers, it's a not so "new" code model.
 
You can select you language from the combo box.
If you are adding a webpage, there should be 2 check boxes; 1 to select a masterPage and 2 if you wish to put the code in a different file.

### Code in a different file: By moving into code-view, you will see the default Class / End Class

### If you choose not to put the code in a separate file, your code-hehind will be in the same area as the XML code. So, you will see the <script> tags.


"Sessions are persistant through pages", and you set in the configuration file the expiration time.
 
Code behind is seeming to be just as difficult as script code. As for putting variable from script to query, might be harder, what is the possible correction for code
<script>
Protected Sub Button1_Click
Dim var As String
Session.Contents("variable") = "whatever"
var = Session("variable")
End Sub
</script>

Select column1 From table1 Where table1.variable = N'var';

Could that be right?
 
>> Code behind is seeming to be just as difficult as script code

-- The script tags will appear only if you write the code sameplace with the XML code (something like designer). This script code IS the code-behind.
Do not mistake the code behind with :

<script lang=jscript type=text/javascript>
function foo (sender, e)
{
// code here
}
</script>

This is client side code. The code behind is SERVER side.

_________________
What are you trying to do ?
 
If you were to use a qs then you should build it at code behind:

dim myQueryString as string = _
"Select column1 From table1 Where table1.variable = N'" & var & "'"

It would also be good practice if you did a little check like:

' Suppose that the session var has been set; check the existance.
If Not Session("variable") Is Nothing then
' Session variable "variable" exists
' This check could prevent a null reference.
End If


_________________
What are you trying to do ?
 
I am trying to pass a session thru the query. Be it query in script, or inside the client side code, or server code.
 
You can't pass a session in a querystring, they are two ways of getting information from page to page. The above ways show how to set/retreive a session variable.

To pass information in the querystring then you would do something like...

Dim myQueryString as string = "blah"
response.redirect("NewPage.aspx?QueryString=" & myQueryString, False)

Then to get that string in another page you would do...

Dim myString as string = request.querystring("QueryString")
 
I have 3 masters and I am trying to pass a session variable on load of each of the masters, or a page that is connected to master page file, once one of the masters is loaded all the rest of the pages need to have that session variable passed to them. If another master is clicked tha pages need to have the second variable passed. There are 3 variables and 3 masters. The session variable needs to be passed thru a query.
 
Dim myString as string = request.querystring("QueryString")

is code behind, in the recievers page, but there is got to be something else to the code since there is error outputed by system.web.httpexception, stating that QueryString cannot be used in this context.
 
That's all you have to do, I use it all the time. Are you passing that variable in the querystring of the sending page? (also maybe try using a different variable name, like MyString - maybe "QueryString" is messing it up - but I doubt it)

The addressbar should look somthing like "
If that doesn't work, just post your sending and recieving page's code.
 
2 questions

1) posting sender
<sript>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Session.Timeout = 40
Session.Contents("var") = "whatever"


Dim myvar As String
myvar = Session.Contents("var")


Dim myQueryString As String = "SELECT table1.columnt1, table2.column1 WHERE table1.column1 = N'" & myvar & "' and (table2.column1 = N'filename.aspx')"

Response.Redirect("filename.aspx?QueryString=" & myQueryString, False)
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

//same code as above with variations
End Sub
</script>
Recievers side
<script>
Dim myvar As String = Session.Contents("var")
Dim myString as string = request.querystring("myQueryString")
</script>
2) How do you request a session variable within this line? Can I? Whats the syntax?

filename.aspx?pn=filename.aspx
someid= request.querystring("pn")
 
I have posted code I have in sender and reciever, as asked.
Plus, I imposed a new question.
 
In your reciever code, all I see is <script>, so when are you calling that script? It has to be associated with an event.

I don't even understand what you are asking when you say "How do I request a session variable with this line?

I repeat from earlier, you can not request a session through the querystring, they are two entirely different concepts, the only thing they have in common is that they are ways of passing information between pages.

Here is an example:

Code:
For Webform1.aspx (the sending page)
-----------------------------
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        
        'sessions can be anything
        Session("Testing") = "This is a session test."

        'querystrings can only be strings
        Response.Redirect("Webform2.aspx?MyString=This is a query string test.", False)
End Sub
--------------------------------

For Webform2.aspx (the receiving page)

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'sessions can be anything, mine is a string
        Dim mySession As String = Session("Testing")

        'this can only be a string
        Dim myQueryString As String =                  
        Request.QueryString("MyString")

End Sub

I just tested it and it works as expected. Are you sure that your sub procedures are being called? Step through the code to make sure...
 
What would be the syntax to call a session within the page tag.

IE.
<%@ Page Language="VB" MasterPageFile "~/Home_" & Mysessionvariable & ".master" %>
 
A few more questions,

1)What would be the syntax for
calling session variable in a query ?
2)What would be the syntax for request.querystring in a query as a variable?
3)If you call a session variable in a query, what is the syntax for calling the same session variable when uploading different pages of the same master? Is this question clear? I am not sure...
4)Where do you recommend to call the session variable in a query, in code behind or inside the content of the page?
5)What would be the syntax for calling two variables in a query? Or variable and a request.querystring?
6)And again, what is the syntax for calling in a master a sesson variable as part of the name of the master?

ie.
"Select column from table where variable1 = N'" & MyVariable 1 & "' and variable2 = N'" & request.QueryString("MyString") & "'
 
Where am I calling sesson variable?
If I load the page and call my session in a different file, isn't it when I call the session variable? I have a page load on master page, is that enought to calll the sub, or I need to have a page load on every page, not only the master?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top