如何在保存单击事件中获取复选框选中的值并将其保存在数据库中

时间:2011-03-22 06:56:16

标签: c# asp.net

我在gridview中有一个复选框和一个保存按钮.....到目前为止,我已经完成了维护pageindexchanging中复选框的状态...在保存按钮中单击我要保存其中的值选中复选框并将其保存在数据库中我需要代码吗?

protected void ManageCalenderShift_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    StoreOldValue();
    EmployeeDetails.PageIndex = e.NewPageIndex;
    BindDataToGrid();
    PupulateoldCheckValue();
}
private void StoreOldValue()
{
    ArrayList categoryIDList = new ArrayList();
    foreach (GridViewRow row in EmployeeDetails.Rows)
    {
        Label can_id = (Label)row.FindControl("UserACENumber");
        bool result = ((CheckBox)row.FindControl("Chkgrid")).Checked;

        // Check in the Session
        if (Session["CHECKED_ITEMS"] != null)
            categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
        if (result)
        {
            if (!categoryIDList.Contains(can_id.Text))
                categoryIDList.Add(can_id.Text);
        }
        else
            categoryIDList.Remove(can_id.Text);
    }
    if (categoryIDList != null && categoryIDList.Count > 0)
        Session["CHECKED_ITEMS"] = categoryIDList;
}
private void PupulateoldCheckValue()
{
    ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
    if (categoryIDList != null && categoryIDList.Count > 0)
    {
        foreach (GridViewRow row in EmployeeDetails.Rows)
        {
            Label can_id = (Label)row.FindControl("UserACENumber");
            if (categoryIDList.Contains(can_id.Text))
            {
                CheckBox myCheckBox = (CheckBox)row.FindControl("Chkgrid");
                myCheckBox.Checked = true;
            }
        }
    }
}

在保存按钮中单击如何查找已检查的控件并将其保存在数据库中?

protected void Save_Click(object sender, EventArgs e)
{


foreach (GridViewRow row in EmployeeDetails.Rows)
    {
        CheckBox chkGrid = (CheckBox)EmployeeDetails.Rows[i].FindControl("chkGrid");
        if (chkGrid.Checked == true)
        {
            //There are two pages and four records in that i have checked the four records but controlis finding for the first two records in the first page if i use this code....
        }
    }



}

2 个答案:

答案 0 :(得分:1)

  

string strIDs;

     

string [] arrIDs;

     

strIDs =   的Request.Form [ “chkBox”]。替换( “'”,   “”);

     

arrIDs = strIDs.Split(',');

使用request.form [“CheckBoxID”],您在gridview中获得了复选框的值。

答案 1 :(得分:0)

只需使用ID搜索复选框控件,您可以使用Page.fincontol(控件ID)找到它,通过foreach查看选中的值,打开连接并保存在db中。不要伪造关闭连接