2

My problem is losing value.I have a DataGrid with standart asp.net pagination. When I change page index, global variable named "id" loses its value. Help me Please.

int id = 0;

void Payments()
{
    radioBtnList = GetData();
    radioBtnList.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
    Payments();
    Response.Write(id); //  I get value 0 :(
}

protected void radioBtnList_Changed(object sender, EventArgs e)
{
    id = int.Parse(radioBtnList.SelectedItem.Text);
}

protected void dgw_pagechange(object source, DataGridPageChangedEventArgs e)
{
    dgw.CurrentPageIndex = e.NewPageIndex;
    dgw.DataBind();
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
coci
  • 21
  • 1
  • 5

2 Answers2

2

Instead of storing the ID in the page which will be reloaded when the page changes you should store it somewhere that will persist over the postback.

Examples might be:

  • The session object
  • A database
  • A text file
SBFrancies
  • 3,987
  • 2
  • 14
  • 37
0

You can use ViewState like this.

int id = 0;
void Payments ()
{
    radioBtnList = GetData();
    radioBtnList.DataBind ();
}

protected void Page_Load (object sender, EventArgs e)
{
  Payments();
  Response.Write(ViewState["id"]);
}

protected void radioBtnList_Changed (object sender, EventArgs e)
{
   id = int.Parse (radioBtnList.SelectedItem.Text);
   ViewState["id"]=id;
}


protected void dgw_pagechange (object source, DataGridPageChangedEventArgs e)
{
    dgw.CurrentPageIndex = e.NewPageIndex;
    dgw.DataBind();
}