Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced GridView question/issue 2

Status
Not open for further replies.

tperri

Programmer
Apr 8, 2006
728
0
0
US
Please help me if I'm being dense...

I'm building a datagrid by hand. The columns will represent Privilege's and the Rows represent FunctionID's.

Here is the Gridview HTML

Code:
                        <asp:GridView ID="grdSecurity" runat="server" AutoGenerateColumns="false" GridLines="None" OnRowDataBound="grdSecurity_RowDataBound" DataKeyNames="id">
                        <Columns>
                            <asp:BoundField DataField="name" HeaderText="Access" />
                            <asp:BoundField DataField="id" Visible="false" />
                        </Columns>
                        </asp:GridView> 
                        <table border="0" width="600">
                            <tr>
                                <td style="text-align:center;"><asp:Button ID="btnSaveSecurity" runat="server" 
                                        Text="Save" onclick="btnSaveSecurity_Click" /></td>
                            </tr>
                        </table>

I query my lookup tables to dynamically get their values so I can set values for a particular user. So, I add checkboxes to each cell... and read from the database and check boxes accordingly (This all is functional)

Code:
    protected void grdSecurity_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Add all checkboxes

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int iFunctionID = Convert.ToInt32(grdSecurity.DataKeys[e.Row.RowIndex].Value);

            for (int x = 0; x < dtPrivileges.Rows.Count; x++)
            {
                CheckBox chk = new CheckBox();
                chk.ID = "chk" + iFunctionID.ToString() + (x).ToString() ;
                e.Row.Cells[x + 2].Controls.Add(chk);
            }

            //Populate checkboxes

            for (int i = 0; i < dtPrivileges.Rows.Count; i++)
            {
                // If there is a functionID, privilegeID match, get a reference ot the checkbox and check it
                DataRow[] foundRows = dtUserPrivileges.Select("function_id=" + iFunctionID.ToString() + " and privilege_id=" + dtPrivileges.Rows[i]["id"].ToString() , "privilege_id asc");

                if (foundRows.Length > 0)
                {
                    CheckBox chkMe = (CheckBox)e.Row.Cells[i + 2].FindControl("chk" + iFunctionID.ToString() + (i).ToString());
                    chkMe.Checked = true;

                }

            }
        }

    }

Now, under my Gridview I have a "Save" button. When I click the Save button, I need to iterate through all of the checkboxes, add checked values to a DataTable so I can perform an Insert/Update off of these values. However, when I try to get reference to the Checkboxes I created, they are still null after I issue a FindControl to locate them in the row.

Do I need to gather the ID for the checkbox a different way? This GridView is on a master page, so the names for the checkboxes in the View Source are very obnoxious.

Here is the code that doesn't work...

Code:
   protected void btnSaveSecurity_Click(object sender, EventArgs e)
    {
        // Set up table to hold new settings
        
        DataTable dtSettings = new DataTable();
        dtSettings.Columns.Add("function_id");
        dtSettings.Columns.Add("privilege_id");

        foreach (GridViewRow grv in grdSecurity.Rows)
        {
            int iFunctionID = Convert.ToInt32(grdSecurity.DataKeys[grv.RowIndex].Value);

            for (int x = 0; x < dtPrivileges.Rows.Count; x++)
            {
                CheckBox chk = (CheckBox)grv.FindControl("chk" + iFunctionID.ToString() + (x).ToString());

                if (chk.Checked == true)
                {
                    // Add data to dtSettings
                    DataRow dr = dtSettings.NewRow();
                    dr["function_id"] = iFunctionID.ToString();
                    dr["privilege_id"] = x.ToString();
                    dtSettings.Rows.Add(dr);

                }
            }
        }

chk is always returning null, which shouldn't be the ase as I created the controls by hand up above. At my break point in the btnSaveSecurity where chk is, and grdSecurity has 21 rows, 9 columns, as I expect, so the controls must be there. I can not figure out how to properly name them in my FindControl method, or if I need to take a different approach?

Thanks for taking the time to look,
T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top