?
Solved

GridView with a Checkbox

Posted on 2009-05-15
8
Medium Priority
?
599 Views
Last Modified: 2013-11-07
Hello Experts,
I have a GridVeiw which is populated and Binded on Form Load. When the user selects a checkbox from the grid and clicks submit button it cant find the Checkbox which is checked. Therefore chbTemp is always retruning false.
Your help would be appreciated  thanks in advance.
D
protected void Button1_Click(object sender, EventArgs e)
        {
string text = String.Empty;
                    for (int i = 0; i < GrdVw.Rows.Count; i++)
            {
                CheckBox chbTemp = GrdVw.Rows[i].FindControl("Chk") as CheckBox;
                if (chbTemp.Checked)
                {
                    text = (GrdVw.Rows[i].Cells[1].Text);
                }
            }
            txtBox.Text = text;
            }
 
<asp:GridView ID="GrdVw" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="insert">
            <ItemTemplate>
                <asp:CheckBox ID="Chk"  runat="server"/>
            </ItemTemplate>
        </asp:TemplateField>        
    </Columns>
</asp:GridView>

Open in new window

0
Comment
Question by:drgorsia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Accepted Solution

by:
abel earned 1000 total points
ID: 24394075
You shouldn't have to loop through all the rows to find your checkbox. If the checkbox is checked, then the checkBox_Changed event will fire. You can use that instead.

If you don't want that, and the button is on a different location then the current row (if it is on the current row you can use NamingContainer), and you want, say, a lot of checked checkboxes treated in one go, then your method is good.

But, it seems that your gridview is not filled anymore. And all you are looking at is an empty set of datarows. the rows that you do find are the headerrows. Can you try the following code to find out whether that's the case (paste the output of the output window here, please)?

foreach (GridViewRow row in GrdVw.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        // found a datarow, it must have a checkbox:
        CheckBox chbTemp = row.FindControl("Chk") as CheckBox;
        Debug.Print("datarow found, has checkbox: " + (chbTemp != null)); 
    }
}

Open in new window

0
 
LVL 3

Expert Comment

by:channa_m
ID: 24395195
Try the following code, this is from one of my project, which works perfectly.
         for (int i = 0; i < gv.Rows.Count; i++)//loop the GridView Rows
            {
                CheckBox cbApprove = (CheckBox)gv.Rows[i].Cells[0].FindControl("cbApprove"); //find the CheckBox
                if (cbApprove != null)
                {
                    if (cbApprove.Checked)
                    {
                         Response.write("Checked");
                    }
                }
            }
 
 
 
 
   <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="cbApprove" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24396215
@channa_m: never use Response.write, unless your whole page is created with Response.Writes (it should be Write, not write, btw). Instead, use a Literal or a Label and write your data there.

In the event that you mix Response.Write with ASPX code, the .Write either comes totally at the end, or totally at the beginning (other situations also possible) and that means before the <html>: wrong markup and often browsers don't even show it....
0
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 
LVL 3

Expert Comment

by:channa_m
ID: 24396354
Abel: I know Response.Write is not the best approach, thanks for the info though.
0
 
LVL 7

Expert Comment

by:ajolly
ID: 24401071
Check if you have set the EnableViewState property true for your gridview and checkbox.
0
 
LVL 39

Expert Comment

by:abel
ID: 24401918
@ajolly: agreed and good point, but do note (@OP) that the default is TRUE for this. So if you don't set it explicitly, it is on.
0
 
LVL 7

Expert Comment

by:ajolly
ID: 24401999
yes, its true by default.

but still we don't see the code for the page and the web.config.
If you set the enableviewstate property for the page or in web.config for all pages, it will be false.
In that case you will have to explicitly enable it for the control/page you want.
0
 

Author Closing Comment

by:drgorsia
ID: 31587283
Abel thanks for your help....used your method and it worked fine. Shame is that the project has been put on hold. Hopefully it will kick start soon...thanks for all the help.
D
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question