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
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="_BigCart.ascx.cs" Inherits="_BigCart" %>
------------------------------------------------------------------------------------------------------------------
_BigCart.ascx.cs
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);
}
}
}
}
}
Amit ChoudharyPosted Apr 22, 2010, 3:50 AM
you can find the control by:
simply
if (gvRow.RowType == DataControlRowType.DataRow)
{
CheckBox checkBoxDelete = (CheckBox)gvRow.FindControl("checkBoxDelete");
}
The problem you are facing of finding control will be solved by this
but when you check the value of checkbox it'll always gives you false cause you have rebind the grid inside the page load event
Do one thing..Enable the viewstate of grid to true and
on page load write the code like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridViewDataBind();
}
}
and your both problem will be solved.
Please mark as answer if it helps.
Posted Apr 21, 2010, 3:56 AM
here is code below to find out checkbox:
in foreach loop u write this code k
Divya MittalPosted Apr 21, 2010, 2:54 AM
Rather trying
CheckBox checkBoxDelete = (CheckBox)gvRow.Cells[0].FindControl("checkBoxDelete")
try
CheckBox checkBoxDelete = (CheckBox)gvRow.FindControl("checkBoxDelete");
it must give you the control !
Good Luck.
Divya