开发者

How to get the checkbox checked value in the save click event and save it in the database

I have checkbox in the gridview and a save button.....so far i have done to maintain the state of the checkbox in the pageindexchanging...in the save button click i want to save the values in which the checkbox is checked and save it in the database i need the code?

protected void ManageCalenderShift_PageIndexChanging(object sender, GridVi开发者_Go百科ewPageEventArgs 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;
            }
        }
    }
}

in the save button click how to find the controls which is checked and save it in the database?

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....
        }
    }



}


string strIDs;

string[] arrIDs;

strIDs = Request.Form["chkBox"].Replace("'", "");

arrIDs = strIDs.Split(',');

Using request.form["CheckBoxID"] you got value of checkbox in gridview.


simply search for checkbox control with ID, u can find it by using Page.fincontol(control ID), chk the checked values by foreach, open connection and save in db. dont forge to close connection

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜