Link to home
Start Free TrialLog in
Avatar of kevinvw1
kevinvw1

asked on

ASP.NET GridView unbound checkbox issue

I am populating a gridview with data from a stored procedure.  I also have a checkbox template column that is not bound.  I populate the grid just fine and then I check a checkbox and click a button to read the checkbox, but it always returns Checked = false.

I have set breakpoints so I know that the code is being executed.

Here is the code behind the button -

protected void btnUpdatePreferences_Click(object sender, EventArgs e)
    {              
        if (gvwPreferences.Rows.Count >= 1)
        {            
            foreach (GridViewRow row in gvwPreferences.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {                        
                    CheckBox subscribe = (CheckBox)row.FindControl("Checkbox1");                
                }
            }
        }
    }

Any suggestions?
Thanks, Kevin.
Avatar of Peter Kirubakaran
Peter Kirubakaran

here i have a sample :

.aspx file
<asp:TemplateField HeaderText="IsRejected">
                        <EditItemTemplate>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="chkIsRejected_CheckedChanged" />
                        </ItemTemplate>
                    </asp:TemplateField>
.cs file
protected void chkIsRejected_CheckedChanged(object sender, EventArgs e)
        {
 CheckBox chkReject = (CheckBox)sender;
//to get the grid view row where this checkbox is present
GridViewRow gvr = (GridViewRow)chkReject.NamingContainer;
if (chkReject.Checked)
                {
        //do any operations
               }
}

if you follow this then you wnt need a button to check the checked property
ASKER CERTIFIED SOLUTION
Avatar of rushShah
rushShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of rajeeshmca
rajeeshmca
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevinvw1

ASKER

thanks for the info.

at the moment, the check box is not bound at all and I don't have AutoPostBack set.

Here is the definition of the checkbox template -

<asp:TemplateField HeaderText="Subscribe">
                    <EditItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        &nbsp;<asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
</asp:TemplateField>
do you have viewstate enabled for the page?
Hi,

I have tried with the following code and it works good... i was able to get the checked value


aspx Page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptMan1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:GridView ID="GridView1" runat="Server" AutoGenerateColumns="False"
                ShowFooter="True" AllowPaging="True" PageSize="5" GridLines="None">
                <Columns>
                    <asp:TemplateField HeaderText="Card Id">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("carid") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("carid") %>'></asp:LinkButton>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="FooterLabel" runat="server"></asp:Label>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Year" HeaderText="year" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="TestChkBox" runat="server" AutoPostBack="false"  />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="TestDdl" runat="server" AutoPostBack="true" >
                                <asp:ListItem Text="One" Selected="true"></asp:ListItem>
                                <asp:ListItem Text="select Two" Value="Select two"></asp:ListItem>
                                <asp:ListItem Text="Three"></asp:ListItem>
                            </asp:DropDownList>
                            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
                                Style="display: none;">
                                <asp:ListItem Text=" select One" Value="select One"></asp:ListItem>
                                <asp:ListItem Text="Two"></asp:ListItem>
                                <asp:ListItem Text="Three"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnTest" runat="Server" CommandName="Test" Text="Insert" />
                        </ItemTemplate>
                    </asp:TemplateField>
                   
                </Columns>
            </asp:GridView>
            <asp:Button ID="TestButton" runat="server" Text="TestChkBox" OnClick="TestButton_Click1" />
           
        </div>
    </form>
</body>
</html>

cs Page

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = GetData();
            GridView1.DataBind();
            dataring.DataSource = GetData();
            dataring.DataBind();
        }
    }

    private DataTable GetData()
    {
        DataTable data = new DataTable();
        DataColumn primaryColumn = new DataColumn("carid", typeof(int) );
        data.Columns.Add(primaryColumn);
        data.Columns.Add(new DataColumn("year", typeof(int)));
        data.Columns.Add(new DataColumn("make", typeof(string)));
        data.Columns.Add(new DataColumn("model", typeof(string)));
        for (int i = 1;i<50;i++)
        {
            DataRow dr1 = data.NewRow();
            dr1[0] = i;
            dr1[1] = 2000 + i;
            dr1[2] = "Swift" + i;
            dr1[3] = "Tata" + i;
            data.Rows.Add(dr1);
        }
        return data;
    }

protected void TestButton_Click1(object sender, EventArgs e)
    {
        foreach (GridViewRow objRow in GridView1.Rows)
        {
            bool b = ((CheckBox)objRow.FindControl("TestChkBox")).Checked;
        }
    }
hi..
although checkbox is not bound, if binds automatically if GridView is binding again in PostBack...

As per me the problem may be due to GridView is binding again so although you checked the checkbox, you get checkbox.checked = false.

so tell us where you bind GridView and if your GridView is not bind again on PostBack then check whether your ViewState is enabled or not.
Thanks for the help.  I was getting confused about the AutoPostBack property versus actually checking for post back when binding.  That was my problem.

I added the check - if (!IsPostBack)
and all is well now.

Thanks,

Kevin.