469,927 Members | 1,853 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="_BigCart.ascx.cs" Inherits="_BigCart" %>
  2. <link href="Main.css" rel="stylesheet" type="text/css" />
  3.  
  4.  
  5.        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
  6.  
  7.    <asp:GridView id="gridViewBigCart" Height="118px"  AutoGenerateColumns="False" runat="server" CssClass="checkout" Width="615px" DataKeyNames="Quantity" >
  8.  
  9.    <Columns>
  10.  
  11.      <asp:TemplateField HeaderText="Remove">
  12.       <ItemTemplate>
  13.           <asp:CheckBox ID="checkBoxDelete" runat="server" />
  14.       </ItemTemplate>
  15.      </asp:TemplateField>
  16.  
  17.      <asp:TemplateField HeaderText="Quantity">
  18.       <ItemTemplate>
  19.         <asp:TextBox id="textBoxQuantity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity")%>' width="40px" Columns="4">
  20.         </asp:TextBox>
  21.       </ItemTemplate>
  22.      </asp:TemplateField>
  23.  
  24.      <asp:TemplateField Visible="False">
  25.        <ItemTemplate>
  26.          <asp:Label id="labelProductID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductID")%>' Visible="False" Width="298px">
  27.          </asp:Label>
  28.        </ItemTemplate>
  29.     </asp:TemplateField>
  30.  
  31.     <asp:BoundField DataField="ProductName" HeaderText="Product Name">
  32.     </asp:BoundField>
  33.  
  34.     <asp:BoundField DataField="QuantityPerUnit" HeaderText="Qty Per Unit">
  35.     </asp:BoundField>
  36.  
  37.     <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" DataFormatString="{0:c}">
  38.     </asp:BoundField>
  39.  
  40.     <asp:BoundField DataField="TotalDue" HeaderText="Total Due" DataFormatString="{0:c}">
  41.     </asp:BoundField>
  42.  
  43.   </Columns>
  44.  </asp:GridView>
  45.  
  46.        <asp:Button ID="Button1" runat="server" Text="Update Cart" OnClick="Button1_Click" />
  47.        <br />
  48.        <asp:Button ID="Button2" runat="server" Text="Proceed To Checkout" />
  49.        <br />
  50.        <asp:Label ID="labelTotalDue" runat="server"></asp:Label>
  51.  
  52.  
  53.        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  54.  
  55.  
  56.        <asp:Label ID="errorMessage" runat="server"></asp:Label>
-------------------------------------------------------------------------------

_BigCart.ascx.cs

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12.  
  13. public partial class _BigCart : System.Web.UI.UserControl
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         GridViewDataBind();
  18.     }
  19.     protected void GridViewDataBind()
  20.     {
  21.         string connection = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString.ToString();
  22.         string cartId = Request.Cookies["CartID"].Value.ToString();
  23.         if (cartId != string.Empty)
  24.         {
  25.             gridViewBigCart.DataSource = ShoppingCart.GetCart(cartId, connection);
  26.             gridViewBigCart.DataBind();
  27.             labelTotalDue.Text = "Total Order Amount: " + string.Format("{0:0.00}", ShoppingCart.GetCartSum(cartId, connection));
  28.         }
  29.     }
  30.  
  31.  
  32.  
  33.     protected void Button1_Click(object sender, EventArgs e)
  34.     {
  35.  
  36.         string cartID = Request.Cookies["CartID"].Value.ToString();
  37.         string connection = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString.ToString();
  38.  
  39.             foreach (GridViewRow gvRow in gridViewBigCart.Rows)
  40.             {
  41.                 if (gvRow.RowType == DataControlRowType.DataRow)
  42.                 {
  43.                     CheckBox checkBoxDelete = (CheckBox)gvRow.Cells[0].FindControl("checkBoxDelete");---(Here checkbox 'null' getting)
  44.                     Label labelProductID = (Label)gvRow.FindControl("labelProductID");
  45.                     TextBox textBoxCount = (TextBox)gvRow.FindControl("textBoxQuantity");
  46.  
  47.                     if (checkBoxDelete != null)
  48.                     {
  49.                         if (checkBoxDelete.Checked)---(checkbox 'false' getting)
  50.                         {
  51.                             ShoppingCart.RemoveFromCart(int.Parse(labelProductID.Text), cartID, connection);
  52.                         }
  53.                     }
  54.                     if (textBoxCount.Text.Trim() == "")
  55.                     {
  56.                         ShoppingCart.RemoveFromCart(int.Parse(labelProductID.Text), cartID, connection);
  57.                     }
  58.  
  59.                 }
  60.  
  61.             }
  62.  
  63.         }
  64.  
  65. }
Apr 21 '10 #1
1 4741
Frinavale
9,735 Expert Mod 8TB
On line 43 in the above post code for the _BigCart.ascx.cs file you have are using the FindControl on Cell[0]. Instead of using the FindControl method on Cell[0], use it on the GridViewRow:

Expand|Select|Wrap|Line Numbers
  1. 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...just 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..thanks!)

-Frinny
Apr 21 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by mercercreek | last post: by
3 posts views Thread by =?Utf-8?B?QnJldA==?= | last post: by
3 posts views Thread by =?Utf-8?B?Um9iZXJ0IFNtaXRo?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.