Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I am trying to use checkbox list for the first time in gridview
here is my asp code
<asp:GridView ID="gdvPermissions" runat="server" AutoGenerateColumns="False" OnRowCommand="gdvPermissions_RowCommand" OnRowDataBound="gdvPermissions_RowDataBound" DataKeyNames="Module_Name">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>Module Name</HeaderTemplate>
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPageName" Text='<%# Eval("Module_Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>Read Permission</HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="cbxRead"></asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>Write Permission</HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="cbxWrite"></asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>None</HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="cbxEdit"></asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="btnSave" runat="server" CommandName="Save" Text="Save" ToolTip="Add Permissions" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


And in the row data bound i am trying to access the grid values to store to database

protected void gdvPermissions_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Save")
    {
        GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
        string PageID = Convert.ToString(gdvPermissions.DataKeys[row.RowIndex].Value);
        CheckBox cbxRead = new CheckBox();
        cbxRead = (CheckBox)row.FindControl("cbxRead");
        CheckBox cbxWrite = new CheckBox();
        cbxWrite = (CheckBox)row.FindControl("cbxWrite"); 
        CheckBox cbxEdit = new CheckBox();
        cbxEdit = (CheckBox)row.FindControl("cbxEdit");
        string val = objBal.InsertPermissions(ddlRoles.SelectedValue.ToString(), PageID, cbxRead.Checked.ToString(), cbxWrite.Checked.ToString(), cbxEdit.Checked.ToString(), User);
        if (val == "9990")
        {
            lblError.Text = "Right Updated Successfully";
        }
        else if (val == "9991")
        {
            lblError.Text = "Right Inserted Successfully";
        }
        else
        {
            lblError.Text = "Error Occured while Assigning User the Rights";
        }
    }
}


so i have 5 modules which will be displayed in the grid along with read write none check box beside it and save button at end of every column

when i click on save button i want the check box values of the particular module to be saved in database.

Please help me.

Thanks in Advance.
Posted
Updated 22-Jun-11 5:03am
v3
Comments
Christoph Keller 22-Jun-11 11:20am    
Hi,

What is the exact problem? Do you get any error, executing the above code?

Best regards and happy coding,
Stops
[no name] 22-Jun-11 11:40am    
are you getting any error?
Shahriar Iqbal Chowdhury/Galib 22-Jun-11 16:43pm    
missing the question? :)
Ali Al Omairi(Abu AlHassan) 22-Jun-11 16:55pm    
try to replace (GridViewRow)((Control)e.CommandSource).NamingContainer with e.Row and see if this helps.
[no name] 23-Jun-11 3:01am    
Please provide page load code.
Also table structure where you want to store the data.

1 solution

Try this

bool a=false;
if(cbxEdit.checked == true)
{
a=true;

///insert a into DB
}
else
a=false;
//insert it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900