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;

        ViewState["id"] = ID;
        }
4

1 に答える 1

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;

}

于 2012-11-29T11:31:05.153 に答える