Hello All, :rolleyes:

I found this code from and artical posted back in may and i had a newbie question that i would like to add to it.

1. Is this the proper all around method to pass values?
2. How would i capture these values? Could i do it in a textbox, label, ect..
3. Coluld i capture it in hidden fields some way.

* Anything someone would like to inform my on about the ((((HttpContext))) and what it is all about ((From a developers standpoint))) i would love to hear about it!

Thank you ahead of time.

Erik.....

Dallas..


==============WebForm1=========

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub


Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim context As HttpContext = HttpContext.Current
context.Items.Add("strFirstName", TextBox1.Text)
Server.Transfer("Webform2.aspx", True)
End Sub


End Class

=========================Webform2=====================

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim context As HttpContext = HttpContext.Current
Response.Write("Your First Name is: " & context.Items("strFirstName"))
End Sub

End Class

Dani AI

Generated

Good question — and thanks to for the links he posted. Short answer: using HttpContext.Items together with Server.Transfer is a valid, simple way to pass objects between pages during the same server request. Items is a per-request dictionary (discarded at the end of the request), so it’s perfect for short-lived, in-memory handoffs but not for persisting across separate requests. For cross-request needs use Session, QueryString/Post, Cache, or a database depending on scope and security requirements. HttpContext.Items (docs)Server.Transfer doc. (learn.microsoft.com)

Controls “tie into” the Items collection because Items stores actual object references; a grid/DataList just needs an object that implements the right shape (DataTable/DataSet/IEnumerable). Example workflow (VB) that avoids an extra DB hit when using Server.Transfer:

' PageA: fetch and stash
HttpContext.Current.Items("CustomerTable") = customersTable
Server.Transfer("PageB.aspx", True)

' PageB: bind if present, otherwise fall back to DB
Dim tbl = TryCast(HttpContext.Current.Items("CustomerTable"), DataTable)
If tbl IsNot Nothing Then
    MyDataList.DataSource = tbl
    MyDataList.DataBind()
Else
    ' fallback: query DB
End If

This per-request caching pattern is a common performance tactic. (learn.microsoft.com)

Hidden fields and ViewState are client-side (sent in the page), so they persist across postbacks but are visible to/alterable by the client — not suitable for secrets or large blobs. Session holds data server-side across requests but has memory and scalability implications. See the HiddenField control and ViewState guidance for tradeoffs and security. (learn.microsoft.com)

Quick troubleshooting notes: always check for the key and null/cast safely (avoid direct indexing), keep stored objects small, and provide a DB fallback if Items is empty. If moving to ASP.NET Core note there is no HttpContext.Current or Server.Transfer; access HttpContext via DI or IHttpContextAccessor and use middleware/pipeline techniques instead. (learn.microsoft.com)

Recommended Answers

All 2 Replies

Here is a good article reviewing the use of HttpContext

LINK

another

LINK 2

and an excellent article from those guys from Rolla!

Hope this helps

:cool:

Hello All, :rolleyes:

I found this code from and artical posted back in may and i had a newbie question that i would like to add to it.

1. Is this the proper all around method to pass values?
2. How would i capture these values? Could i do it in a textbox, label, ect..
3. Coluld i capture it in hidden fields some way.

* Anything someone would like to inform my on about the ((((HttpContext))) and what it is all about ((From a developers standpoint))) i would love to hear about it!

Thank you ahead of time.

Erik.....

Dallas..


==============WebForm1=========

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub


Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim context As HttpContext = HttpContext.Current
context.Items.Add("strFirstName", TextBox1.Text)
Server.Transfer("Webform2.aspx", True)
End Sub


End Class

=========================Webform2=====================

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim context As HttpContext = HttpContext.Current
Response.Write("Your First Name is: " & context.Items("strFirstName"))
End Sub

End Class

Dani,

I have a small request for you. The second article was really informative and very explanatory. The only draw that I am having is that I need to see this in a real world code scenario.

In the code below the author is describing how to access a hash table using the httpcontext.items. My question to you is how are the controls tied to this call to the hash table?

I understand everything other than how the code merges into the regular code.

My example of regular code-à dim cnn as new sqlconnection(“)

Dim da as new sqldataadapter(“) ‘ all of this code is what I am use to.

Dim ds as new dataset ‘Now how is the other used to tie the control?

Da.fill (ds,Hashtest) ‘I understand the concept of checking the httpcontext

MyDatalist.Datasource = ds to see if any data is there to be used so that a call

MyDatalist.databind to the database would not be necessary. So how do this

Contol (MyDatalist) tie into the hashtable?

Page the example is at-->

Thank you for your help!!! GREAT RESOURCES!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.