嵌套gridview内的复选框值不存储在数据库中 [英] checkbox values inside nested gridview is not stored in database

查看:70
本文介绍了嵌套gridview内的复选框值不存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 

    CellPadding="4" ForeColor="#333333" GridLines="None" 

    onrowdeleting="GridView2_RowDeleting" onrowdatabound="OnRowDataBound">
    <alternatingrowstyle backcolor="White" />
    <columns>
        <asp:TemplateField HeaderText="Edit">
            <itemtemplate>
                <asp:Image ID="Image1" alt = "" style="cursor: pointer" runat="server" ImageUrl="~/plus.png" />
                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

    CellPadding="4" ForeColor="#333333" 



    GridLines="None" OnItemDataBound="GridView1_ItemDataBound"

Width="693px"  Font-Bold="False" onpageindexchanged="Button3_Click" >
   
    <alternatingrowstyle backcolor="White" />
    <columns>
        <asp:BoundField HeaderText="ID" DataField="ID" >
        <HeaderStyle BackColor="#F7D5FF" Font-Size="8.5pt" ForeColor="Black" />
        
        <asp:BoundField HeaderText="Pages" DataField="Pages" >
        <HeaderStyle BackColor="#F7D5FF" Font-Bold="True" Font-Size="8.5pt" 

            ForeColor="Black" />
        
        <asp:TemplateField HeaderText="ADD">
            <itemtemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" 

                    EnableViewState="False" AutoPostBack="True" />
            </itemtemplate>
            <HeaderStyle BackColor="#F7D5FF" Font-Bold="True" Font-Size="8.5pt" 

                ForeColor="Black" />
        
        <asp:TemplateField HeaderText="View">
            <itemtemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" />
            </itemtemplate>
            <HeaderStyle BackColor="#F7D5FF" Font-Size="8.5pt" ForeColor="Black" />
        
        <asp:TemplateField HeaderText="Edit">
            <itemtemplate>
                <asp:CheckBox ID="CheckBox3" runat="server" />
            </itemtemplate>
            <HeaderStyle BackColor="#F7D5FF" Font-Size="8.5pt" ForeColor="Black" />
        
        <asp:TemplateField HeaderText="Delete">
            <itemtemplate>
                <asp:CheckBox ID="CheckBox4" runat="server" />
            </itemtemplate>
            <HeaderStyle BackColor="#F7D5FF" Font-Size="8.5pt" ForeColor="Black" />
        
    </columns>
    <editrowstyle backcolor="#2461BF" />
    <footerstyle backcolor="#507CD1" font-bold="True" forecolor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center" />
    <rowstyle backcolor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <sortedascendingcellstyle backcolor="#F5F7FB" />
    <sortedascendingheaderstyle backcolor="#6D95E1" />
    <sorteddescendingcellstyle backcolor="#E9EBEF" />
    <sorteddescendingheaderstyle backcolor="#4870BE" />

   <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Save" />                
    
                 
            </itemtemplate>
        
        <asp:TemplateField HeaderText="Role_ID">
            <itemtemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Role_ID") %>'>
            </itemtemplate>
        
        <asp:TemplateField HeaderText="Role_Name">
            <itemtemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Role_Name") %>'>
            </itemtemplate>
        
        <asp:TemplateField HeaderText="Description">
            <itemtemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'>
            </itemtemplate>
        
        <asp:TemplateField HeaderText="Delete">
            <itemtemplate>
                <asp:ImageButton CssClass="txtBtn" ID="lnkDelete" runat="server" CommandName="Delete"

                    CommandArgument='' OnClientClick="javascript:return confirm('Are you sure you want to delete the record?')"

                    ImageUrl="~/images/icon_delete.gif" ToolTip="Delete" />
           
            </itemtemplate>
        
    </columns>
    <editrowstyle backcolor="#2461BF" />
    <footerstyle backcolor="#507CD1" font-bold="True" forecolor="White" />
    <HeaderStyle BackColor="#F7D5FF" Font-Bold="True" ForeColor="Black" />
    <pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center" />
    <rowstyle backcolor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <sortedascendingcellstyle backcolor="#F5F7FB" />
    <sortedascendingheaderstyle backcolor="#6D95E1" />
    <sorteddescendingcellstyle backcolor="#E9EBEF" />
    <sorteddescendingheaderstyle backcolor="#4870BE" />



code


code

protected void Button3_Click(object sender, EventArgs e)
{
    foreach (GridViewRow ro in GridView2.Rows)
    {
        if (ro.RowType == DataControlRowType.DataRow)
        {
            GridView gv = (GridView)ro.FindControl("GridView1");
            {
                if (gv != null)
                {
                    foreach (GridViewRow row in gv.Rows)
                    {
                        if (row.RowType == DataControlRowType.DataRow)
                        {
                            for (int i = 0; i < gv.Rows.Count; i++)
                            {
                                Label ID = (ro.Cells[1].FindControl("Label1") as Label);
                                int Role_ID = Convert.ToInt32(ID.Text);
                                CheckBox chkAdd = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
                                CheckBox chkEdit = (row.Cells[3].FindControl("CheckBox2") as CheckBox);
                                CheckBox chkView = (row.Cells[4].FindControl("CheckBox3") as CheckBox);
                                CheckBox chkDelete = (row.Cells[5].FindControl("CheckBox4") as CheckBox);
                                string strID = row.Cells[0].Text;
                                string Pages = row.Cells[1].Text;

                                con.Open();
                                string query = "Update Role1 set Role_ID= '" + Role_ID + "',  [Add]='" + (chkAdd.Checked == true ? 'Y' : 'N') + "', [View]='" + (chkView.Checked == true ? 'y' : 'N') + "' ,[Edit]='" + (chkEdit.Checked == true ? 'y' : 'N') + "' ,[Delete]='" + (chkDelete.Checked == true ? 'y' : 'N') + "' where [ID]='" + strID + "'  and Pages ='" + Pages + "'";
                                SqlCommand cmd = new SqlCommand(query, con);
                                cmd.ExecuteNonQuery();
                                gv.DataBind();

                                con.Close();
                            }
                        }
                    }
                }
            }
        }
    }
}

推荐答案

这篇关于嵌套gridview内的复选框值不存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆