Categories
ASP.NET

Get ViewState from custom class

The easiest way would be to pass the ViewState from you Page class.
Here is an example:

In Page code:
….
‘Create an instance of custom class
Dim objMyClass As MyClass
Set objMyClass = New MyClass( Me.ViewState )
….

In class code:
….
Private viewState As System.Web.UI.StateBag

Public New( ByRef pageViewState As System.Web.UI.StateBag )
Set viewState = pageViewState
End
….

However, if your class is stand-alone custom class, it either can take the ViewState collection as a reference in and operate ith it, or it can just return the value(s) to the caller (Page) and let that handle operating with the ViewState (there are examples of both in this thread).

Only controls (Page and its controls) have ViewState collection (each control has its own) and therefore accessing it from a custom class needs certain approach. Sessions you could access from class via System.Web.HttpContext.Current.Session, but there’s not similar way to access ViewState.

Reference 
 

Categories
ASP.NET

Get control when the control is within a content page of Master Page

The problem is when you have a content page of master page, you can’t get the control directly by using

FindControl(“<control_name>”);

This is because since we are using a Master Page, the ID of the conrtol nested in the ContentPlaceHolder has been changed. It is changed like that:

ctl00$ContentPlaceHolder1$TextBox1     //(let’s asume the id of the control is TextBox1)

So to get the control text value from the Request.Form, the fastest way is to use:
Request.Form[“ctl00$ContentPlaceHolder1$TextBox1”];

However if you need to get the control instead of only its text value, you can get it like this:

ContentPlaceHolder cn = (ContentPlaceHolder)Master.FindControl(“ContentPlaceHolder1”);
if (cn != null)
{
    TextBox tb = (TextBox)cn.FindControl(“TextBox1”);
    //if (tb != null)  { Response.Write(tb.Text); }
}

This is when the postback is made by the page itself. When the postback is made by another page, instad of Master, there should be used PreviousPage.Master:

ContentPlaceHolder cn =
    (ContentPlaceHolder)PreviousPage.Master.FindControl(“ContentPlaceHolder1”);

Reference1, Reference2

Categories
ASP.NET

Session State or ViewState?

There are certain cases where holding a state value in ViewState is not the best option. The most commonly used alternative is Session state, which is generally better suited for:

  • Large amounts of data.
    As ViewState increases the size of both the HTML page sent to the browser and the size of form posted back, it’s a poor choice for storing large amounts of data.
  • Sensitive data that is not already displayed in the UI.
    While the ViewState data is encoded and may optionally be encrypted, your data is most secure if it is never sent to the client. So, Session state is a more secure option.
  • Objects not readily serialized into ViewState, for example, DataSet.
    As already discussed the ViewState serializer is optimized for a small set of common object types. Other types that are serializable may be persisted in ViewState, but are slower and can generate a very large ViewState.
  • Author:www.dotnetjohn.com

    Categories
    ASP.NET

    HowTo: Get the current page file name

    Mnoo lesno i kratko:
    System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath);

    😉

    Reference

    Categories
    ASP.NET

    Error: The name ‘Session’ does not exist in the current context

    This error normally occurs when you are trying to access the value of a session variable such as Session[“UserID”] and you’re not doing that from a webform, usercontrol or a class that inherits from System.Web.UI.

    In this situation you can still access the session variable, but using a different path.
    For example to access a session variable named UserID you would normally use Session[“UserID”]; however, if the error The name ‘Session’ does not exist in the current context is returned, use the following path for retrieving the value of the session variable:

    HttpContext.Current.Session[“UserID”];

    Reference