如何在viewstate中存储多个复选框值?

时间:2012-11-29 09:06:13

标签: c# asp.net

这是我的viewstate的代码。但它只存储一个值。我需要的是它会在复选框中保留选定的多个值。这种方法是在分页情况的gridview中保持/保持复选框的值。

public void chkAssignee_OnCheckedChanged(object sender, EventArgs e)
       {
        CheckBox selectBox = (CheckBox)sender;
        GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent;  // the row
        GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview
        string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString();
        GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent;

        int a = rowSelect.RowIndex;

        ViewState["id"] = ID;
        }

1 个答案:

答案 0 :(得分:1)

试试以下 以下代码可能对您有帮助。


public void chkAssignee_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox selectBox = (CheckBox)sender;
        GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent;  // the row
        GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview
        string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString();
        GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent;

    int a = rowSelect.RowIndex;
    ArrayList SelecterdRowIndices=new ArrayList();
    if(ViewState["SelectedRowIndices"]!=null)
    {
        SelecterdRowIndices=(ArrayList)ViewState["SelectedRowIndices"];
        bool flag=false;
        foreach (int i in SelecterdRowIndices)
        {
            if(i==Convert.ToInt32(ID))
            {
                flag=true;
                break;
            }   
        }
        if(!flag)
        {
            SelecterdRowIndices.Add(ID);
        }
    }  
    else
    {
         SelecterdRowIndices.Add(ID);
    }
    ViewState["SelectedRowIndices"] = SelecterdRowIndices;

}

int a = rowSelect.RowIndex; ArrayList SelecterdRowIndices=new ArrayList(); if(ViewState["SelectedRowIndices"]!=null) { SelecterdRowIndices=(ArrayList)ViewState["SelectedRowIndices"]; bool flag=false; foreach (int i in SelecterdRowIndices) { if(i==Convert.ToInt32(ID)) { flag=true; break; } } if(!flag) { SelecterdRowIndices.Add(ID); } } else { SelecterdRowIndices.Add(ID); } ViewState["SelectedRowIndices"] = SelecterdRowIndices; }