3

I have below code,

    If Not Page.IsPostBack Then
        ViewState("ItemID") = 0
        If Not Request.QueryString("id") Is Nothing Then
            ViewState("ItemID") = Request.QueryString("id")
            ItemID = Integer.Parse(ViewState("ItemID"))
        End If
        If ItemID > 0 Then
            ltrTitle.Text = "Edit Item"
            bindEditData()
        End If
    End If

When I tried to get ViewState("ItemID") on button click, it returns nothing. Any help?

Nemi Pujara
  • 73
  • 1
  • 7
  • Are you intentionally using ViewState instead of Session? [What is the difference between SessionState and ViewState?](http://stackoverflow.com/a/733486/1115360) – Andrew Morton Apr 08 '16 at 21:13
  • Could you give us more information, for example: where this code is used, the body of bindEditData and the body of button click. – Henrique Apr 11 '16 at 12:46

2 Answers2

1

I see that you have your code in not postback method. So once your page postback, viewstate value will be lost.

Please change your code to,

ViewState("ItemID") = 0
If Not Request.QueryString("id") Is Nothing Then
    ViewState("ItemID") = Request.QueryString("id")
    ItemID = Integer.Parse(ViewState("ItemID"))
End If
If Not Page.IsPostBack Then        
    If ItemID > 0 Then
        ltrTitle.Text = "Edit Item"
        bindEditData()
    End If
End If
INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
0

The ViewState is probably turned off on your page. The EnableViewState attribute is set to false or the ViewStateMode attribute is set to Disabled:

<%@ Page EnableViewState="false" ... />
<%@ Page ViewStateMode="Disabled" ... />

You can enable the ViewState by changing these settings:

<%@ Page EnableViewState="true" ... />
<%@ Page ViewStateMode="Enabled" ... />

These are the default settings, and are necessary only if the ViewState is disabled at the application level (in Web.config).

ConnorsFan
  • 70,558
  • 13
  • 122
  • 146