Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I'm unable find check box control in my webpage.
When building the page I'm getting "checkbox null" and "false".
Can any help me to find the solution.
I'm pasting the code here.
-------------------------------------------------------------------------------------------------------------------
_BigCart.ascx
XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="_BigCart.ascx.cs" Inherits="_BigCart" %>
<link href="Main.css" rel="stylesheet" type="text/css" />

<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:GridView id="gridViewBigCart" Height="118px" AutoGenerateColumns="False" runat="server" CssClass="checkout" Width="615px" DataKeyNames="Quantity" >
<Columns>
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:CheckBox ID="checkBoxDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox id="textBoxQuantity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity")%>' width="40px" Columns="4">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label id="labelProductID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductID")%>' Visible="False" Width="298px">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="Product Name">
</asp:BoundField>
<asp:BoundField DataField="QuantityPerUnit" HeaderText="Qty Per Unit">
</asp:BoundField>
<asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" DataFormatString="{0:c}">
</asp:BoundField>
<asp:BoundField DataField="TotalDue" HeaderText="Total Due" DataFormatString="{0:c}">
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Update Cart" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="Proceed To Checkout" />
<br />
<asp:Label ID="labelTotalDue" runat="server"></asp:Label>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<asp:Label ID="errorMessage" runat="server"></asp:Label>

------------------------------------------------------------------------------------------------------------------
_BigCart.ascx.cs

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _BigCart : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
GridViewDataBind();
}
protected void GridViewDataBind()
{
string connection = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString.ToString();
string cartId = Request.Cookies["CartID"].Value.ToString();
if (cartId != string.Empty)
{
gridViewBigCart.DataSource = ShoppingCart.GetCart(cartId, connection);
gridViewBigCart.DataBind();
labelTotalDue.Text = "Total Order Amount: " + string.Format("{0:0.00}", ShoppingCart.GetCartSum(cartId, connection));
}
}

protected void Button1_Click(object sender, EventArgs e)
{
string cartID = Request.Cookies["CartID"].Value.ToString();
string connection = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString.ToString();
foreach (GridViewRow gvRow in gridViewBigCart.Rows)
{
if (gvRow.RowType == DataControlRowType.DataRow)
{

CheckBox checkBoxDelete = (CheckBox)gvRow.Cells[0].FindControl("checkBoxDelete");---(Here checkbox 'null' getting)

Label labelProductID = (Label)gvRow.FindControl("labelProductID");
TextBox textBoxCount = (TextBox)gvRow.FindControl("textBoxQuantity");
if (checkBoxDelete != null)
{

if (checkBoxDelete.Checked)---(checkbox 'false' getting)

{
ShoppingCart.RemoveFromCart(int.Parse(labelProduct ID.Text), cartID, connection);
}
}
if (textBoxCount.Text.Trim() == "")
{
ShoppingCart.RemoveFromCart(int.Parse(labelProduct ID.Text), cartID, connection);
}
}
}
}
}
Posted
Updated 20-Apr-10 19:36pm
v2

Gridrows contain everything, even header!
Go through this article, it explain lots of feature handling in Grids:
GridView all in one[^]
 
Share this answer
 
Hi,

Please check the below sample code and vote for it so i can know if it fixed the issue or not

XML
<body>
    <form id="form1" runat="server">
    <asp:GridView runat="server" ID="gvProjects" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="chkDelete" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Projcet Name" DataField="ProjectName" />
        </Columns>
    </asp:GridView>

    <br />
    <asp:Button runat="server" ID="btnSubmit" Text="Submit"
        onclick="btnSubmit_Click" />
    </form>
</body>



and add the below to your code behind:
C++
public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        private void BindGrid()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ProjectName");
            dt.Columns.Add("IsDeleted");
            for (int i = 0; i < 20; i++)
            {
                DataRow row = dt.NewRow();
                row["ProjectName"] = "Name" + i.ToString();
                row["IsDeleted"] = true;
                dt.Rows.Add(row);
            }
            gvProjects.DataSource = dt;
            gvProjects.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvRow in gvProjects.Rows)
            {
                if (gvRow.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkDelete = (CheckBox)gvRow.Cells[0].Controls[1];
                }
            }
        }

    }


so all what you have to do is to change the following line of code:
C++
CheckBox checkBoxDelete = (CheckBox)gvRow.Cells[0].FindControl("checkBoxDelete");


instead use :
C++
CheckBox checkBoxDelete = (CheckBox)gvRow.Cells[0].Controls[1];



Regards,
Jamil
 
Share this answer
 
v2

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