views:

428

answers:

4

How to store the state of my check box list tick boxes accross the page life cycle?

Here i have a unbound checkbox list control where values comes from xml file.

how to retain the state?

Moreover, when i click on Next button to the new page and comeback state is retained.

But when i click on the back button and come to the same page state is not retained

Protected Sub chkBx_SR_wu_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkBx_SR_wu.SelectedIndexChanged
Dim i As Integer
i = 0
Try
    For i = 0 To chkBx_SR_wu.Items.Count - 1
        If chkBx_SR_wu.Items(i).Selected Then
            Select Case chkBx_SR_wu.Items(i).Value
                Case "SR_MR"
                    chkbx_SR.Checked = True
                Case "Sk_MR"
                    chkbx_SkRoom.Checked = True
                Case "SC_SS"
                    chkbx_admin.Checked = True
                Case "CS_MR"
                    chkbx_salesFloor.Checked = True
                Case "TEAM_LEADER"
                    rb_tl.Enabled = True
                    chkbx_tlAdmin.Enabled = True
                    chkbx_tlJewellery.Enabled = True
                    chkbx_tlSalesFloor.Enabled = True
                    chkbx_tlSkRoom.Enabled = True
                    rb_tl.Items(0).Enabled = True
                    rb_tl.Items(1).Enabled = True
                    rb_tl.Items(2).Enabled = True
                    rb_tl.Items(3).Enabled = True
                    ReqiredFieldValidator1.Enabled = True



            End Select
        Else
            Select Case chkBx_SR_wu.Items(i).Value
                Case "SR_MR"
                    chkbx_SR.Enabled = False
                    chkbx_SR.Checked = False
                Case "Sk_MR"
                    chkbx_SkRoom.Enabled = False
                    chkbx_SkRoom.Checked = False
                Case "SC_SS"
                    chkbx_admin.Enabled = False
                    chkbx_admin.Checked = False
                Case "CS_MR"
                    chkbx_salesFloor.Enabled = False
                    chkbx_salesFloor.Checked = False
                Case "TEAM_LEADER"
                    chkbx_tlAdmin.Enabled = False
                    chkbx_tlAdmin.Checked = False
                    chkbx_tlJewellery.Enabled = False
                    chkbx_tlJewellery.Checked = False
                    chkbx_tlSalesFloor.Enabled = False
                    chkbx_tlSalesFloor.Checked = False
                    chkbx_tlSkRoom.Enabled = False
                    chkbx_tlSkRoom.Checked = False
                    rb_tl.Items(0).Enabled = False
                    rb_tl.Items(1).Enabled = False
                    rb_tl.Items(2).Enabled = False
                    rb_tl.Items(3).Enabled = False
                    ReqiredFieldValidator1.Enabled = False
            End Select
        End If
    Next

Catch ex As Exception

End Try

End Sub"

A: 

My first guess, without seeing your code, is that you're binding the checkboxlist on Page_Load without checking to see if the page load is a postback. Do that and it should fix the problem.

jcollum
what i have to do ?
Code is give above.. there is no code in page_load event
i am a newbie here please help
if there's no code in your page load then how does the checkbox get its items?
jcollum
A: 

You need to check where you are binding data to the checklist box. The problem isn't in the code you posted but where ever you are binding the code. Make sure you are checking for IsPostBack to be false or else every time your page loads, you will be rebinding your data and losing all your state. You should only bind once. Eg (in C#):

if (!IsPostBack)
{
   BindMyDataToCheckBoxList();
}

Put a break point on where you binding the data, I bet everytime you doing anything like click a button or whatever, that binding code is getting hit which it probably shouldn't be.

Kelsey
A: 

There's no guaranteed method of having the values retained if they visit the page again but not if they use the back button. You could try setting the page headers so that the page is not cached as a precaution against the user seeing invalid data.

In the Page_Init stage of the page lifecycle, you can populate your checkbox list with the values from your XML file, and then in the Page_Load stage, check that the page isn't be posted back and select the checkboxes accordingly using the values in the session.

Eg. in C# -

protected void Page_Init(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Populate checkbox list from XML
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        List<string> list = Session["MyList"] as List<string>;

        if (list != null)
        {
            foreach (int val in list)
            {
                ListItem chk = myCheckBoxList.Item.FindByValue(val);

                if (chk != null)
                    chk.Checked = true;
            }
        }
    }
}

protected void SaveButton_Click(object sender, EventArgs e)
{
    List<string> list = new List<string>();

    foreach (ListItem li in myCheckBoxList.Items)
    {
        if (li.Checked)
        {
            list.Add(li.Value);
        }
    }

    Session["MyList"] = list;
}

This code hasn't been tested, but could be used a starting point. You can convert the code to VB.NET using a code converter, such as the one from Telerik.

Mun
A: 

You can retain state of checkboxes across/on pages either by:

  • Cookie
  • Sessions
  • ViewState
  • or Save it in Database.

I would recommend Sessions as it is quite trivial to use, and does not require them to have cookies enabled.

A simple tutorial on sessions can be found here. That is a c# version.

Here is a vb.net example (but not from MSDN)

waqasahmed