Unable to find checkbox control in Gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandeepsangshetty
    New Member
    • Aug 2008
    • 12

    Unable to find checkbox control in Gridview

    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
    Code:
    <%@ 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.c s

    Code:
    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(labelProductID.Text), cartID, connection);
                            }
                        }
                        if (textBoxCount.Text.Trim() == "")
                        {
                            ShoppingCart.RemoveFromCart(int.Parse(labelProductID.Text), cartID, connection);
                        }
                            
                    }
                    
                }
    
            }
    
    }
    Last edited by Frinavale; Apr 21 '10, 03:25 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    On line 43 in the above post code for the _BigCart.ascx.c s file you have are using the FindControl on Cell[0]. Instead of using the FindControl method on Cell[0], use it on the GridViewRow:

    Code:
    CheckBox checkBoxDelete = (CheckBox)gvRow.FindControl("checkBoxDelete");
    In the future please post your code in code tags. It makes it easier for us to read your code and it provides us with line numbers that we can refer back to when helping you.

    Also, please don't post Everything...ju st post the code that is relevant to the question (like the GridView markup for the column containing the CheckBox...and the function where you are having problems retrieving it in your C# code). It just makes it easier for us to help you if we don't have to scan through everything to find the line that's causing the problem (at least you were kind enough to point out the line using comments..thank s!)

    -Frinny

    Comment

    Working...