Accessing Checkbox on GridView

Last post 12-04-2009 2:25 PM by hoangnm. 14 replies.

Sort Posts:

  • Accessing Checkbox on GridView

    09-02-2007, 11:42 PM
    • Member
      177 point Member
    • willydavidjr
    • Member since 01-07-2007, 8:58 AM
    • Developer St., Programmers Homes, IT City
    • Posts 166

    Hi guys, I've create a template on aspxgrid using checkbox using checkbox field. But my problem is, how will I access the checkbox property? I've created this code:

     

    for (int x = 0; x < GridView1.Rows.Count; x++)

    {

     

    CheckBox chk = GridView1.Rows[x].FindControl("CheckBox1") as CheckBox;

    if (chk.Checked == true)

    {

    //my logic here

    }

    }

    But I always get a chk,checked property to false even though I've check the checkbox. Is there anyway to access those checkboxes without any postback? Thanks in advance!

     

    Microsoft Student Partners

    Willy David Jr
    Junior Software Development Engineer
    Gurango Software Corporation

    Programming is a Passion, Software Development is an Adventure - willydavidjr
    Filed under:
  • Re: Accessing Checkbox on GridView

    09-03-2007, 1:01 AM
    • Contributor
      2,598 point Contributor
    • Sathesh_Pandian
    • Member since 07-23-2007, 3:55 PM
    • Chennai
    • Posts 441

    you can use javascript for this.

    var gridItem;

    var itemIndex = 0;

    var grid = <%=grvPosicionesCatalogonoPresupuestal.ClientObjectId %>;while(gridItem = grid.Table.GetRow(itemIndex))

    {

    var checkBox=document.getElementById('ctl00_cphContenido_ctl00_cphContenido_grvPosicionesCatalogonoPresupuestal_0_1_'+ itemIndex +'_Check');

    itemIndex++;

    if(state==true)

    {

    checkBox.checked=
    true;

    }

    else

    {

    checkBox.checked=
    false;

    }

    }

     

    it will work.

    Got what you needed. Please mark as answer.
  • Re: Accessing Checkbox on GridView

    09-03-2007, 1:04 AM
    • Member
      177 point Member
    • willydavidjr
    • Member since 01-07-2007, 8:58 AM
    • Developer St., Programmers Homes, IT City
    • Posts 166

    By the way, I've converted my checkboxfield into a templatefield because it has no connection with the database. And here is my source code:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True">

    <Columns>

    <asp:TemplateField>

     

    <ItemTemplate>

    <asp:CheckBox ID="CheckBox1" runat="server" />

    </ItemTemplate>

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

     

    Microsoft Student Partners

    Willy David Jr
    Junior Software Development Engineer
    Gurango Software Corporation

    Programming is a Passion, Software Development is an Adventure - willydavidjr
  • Re: Accessing Checkbox on GridView

    09-03-2007, 1:07 AM
    • Member
      177 point Member
    • willydavidjr
    • Member since 01-07-2007, 8:58 AM
    • Developer St., Programmers Homes, IT City
    • Posts 166

    Hi satesth, thanks for that, but I really need a server side code for this because I am using a different framework. Thank you very much!

     

    Microsoft Student Partners

    Willy David Jr
    Junior Software Development Engineer
    Gurango Software Corporation

    Programming is a Passion, Software Development is an Adventure - willydavidjr
  • Re: Accessing Checkbox on GridView

    09-03-2007, 1:11 AM
    • Participant
      1,339 point Participant
    • Mahadeomatre
    • Member since 04-23-2007, 11:00 AM
    • Pune, India
    • Posts 230

    r u marked checkbox as AutoPostback='true'?

    but it will do the postback when u check any check box..

    to avoid it u can use AJAX controls.. just put ur grid in UpdatePanel, it will do postback , but not complete postback..

    hope this will help u.

    Thanks,
    Mahadeo

    -----------------------------------------------
    Best is Not the END point, but a starting point for Innovation
    ------------------------------------------------
    Remember to mark posts as the "Answer" to help future users.
  • Re: Accessing Checkbox on GridView

    09-03-2007, 1:17 AM
    • Member
      178 point Member
    • AshwiniK
    • Member since 08-29-2007, 10:58 AM
    • India
    • Posts 36

    Hi

    i had already faced this problem before, but it was with the Datagrid

    here is some code lines which may solve your problem

     

    protected void Button1_Click(object sender, EventArgs e)

    {      

    // Response.Write("<script>alert('Are You Sure to Delete the Selected Row(s) ? ');</script>");

    con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"]);

    DataGridItemCollection items = DGRetrive.Items;for (int i = 0; i < items.Count; i++)

    {

    CheckBox cb = (CheckBox)items[i].FindControl("Chk");

    if (cb.Checked == true)

    {

    string AssId = items[i].Cells[1].Text.Trim();

     

    con.Open();

    cmd =
    new SqlCommand("Delete XYZ where AssessmentID = '" + Convert.ToInt32(AssId) + "'", con);

    int rows = cmd.ExecuteNonQuery();

    if (rows != 0)

    {

    lblMsg.Text =
    " Row(s) Deleted Successfully... ";

    }

    }

    con.Close();

    }

     

    }

    and in your Html Source

    <asp:DataGrid ID="DGRetrive" runat="server" >

    <Columns>

    <asp:TemplateColumn>

    <ItemTemplate>

    <asp:CheckBox ID="Chk" runat="server" />

    </ItemTemplate>

    </asp:TemplateColumn>

    </Columns>

    </DataGrid >

     

    Ashwini

    Plz mark as ANSWER, if my POST help u

  • Re: Accessing Checkbox on GridView

    09-03-2007, 1:25 AM
    • Member
      177 point Member
    • willydavidjr
    • Member since 01-07-2007, 8:58 AM
    • Developer St., Programmers Homes, IT City
    • Posts 166

    Hi thanks for all the reply but still, I still, I cannot solve it. I enabled the autopostback property but everytime I checked the checkbox, it refreshes the page, and then it will uncheck itself.

    @ashwinik,

    I've tried your method but still, even though I check my checkbox, still I always get the false value on its checked property. I am still clueless on how to access the checked property of it.

     

    Microsoft Student Partners

    Willy David Jr
    Junior Software Development Engineer
    Gurango Software Corporation

    Programming is a Passion, Software Development is an Adventure - willydavidjr
  • Re: Accessing Checkbox on GridView

    09-03-2007, 2:14 AM
    Answer
    • Member
      178 point Member
    • AshwiniK
    • Member since 08-29-2007, 10:58 AM
    • India
    • Posts 36

    Hi ,

     

    protected void Page_Load(object sender, EventArgs e)

    {

     

    if ( ! IsPostBack )

    {

    FillDGRetrive();   // Method for Filling the datagrid

     

    }

     

    }

    For every time you have to check that "is the page Post back"

    if yes then ...

    if no ...

    when you check your checkBox and click for a Button, that time you have to chek that is the page post back, when it returns false then you have too delete(or your opration)

     

    Ashwini

    Plz mark as ANSWER, if my POST help u

  • Re: Accessing Checkbox on GridView

    09-03-2007, 2:30 AM
    • Member
      177 point Member
    • willydavidjr
    • Member since 01-07-2007, 8:58 AM
    • Developer St., Programmers Homes, IT City
    • Posts 166

    Hi Ashwinik,

    Thanks for that, it works fine now, I don't know what happened but it seems that I need to databind my grid once only so that I will not lost my property of my checkbox. Again thanks for that!

     

    Microsoft Student Partners

    Willy David Jr
    Junior Software Development Engineer
    Gurango Software Corporation

    Programming is a Passion, Software Development is an Adventure - willydavidjr
  • Re: Accessing Checkbox on GridView

    09-03-2007, 3:06 AM
    • Member
      177 point Member
    • willydavidjr
    • Member since 01-07-2007, 8:58 AM
    • Developer St., Programmers Homes, IT City
    • Posts 166

    Hi guys, I have one final question. How can I get the value on each row or each cell on a row when the checkbox is check? How to retrieve those values? Thanks in advance again!

     

    Microsoft Student Partners

    Willy David Jr
    Junior Software Development Engineer
    Gurango Software Corporation

    Programming is a Passion, Software Development is an Adventure - willydavidjr
  • Re: Accessing Checkbox on GridView

    04-27-2008, 11:33 AM
    • Participant
      1,169 point Participant
    • abhilashca
    • Member since 02-29-2008, 10:01 AM
    • India
    • Posts 248

    Here is ur answer.

    Ive created a table :

    Table Name  : yMail
    Field  : id (int) | sender(varchar) | subject(varchar)

    Now pick a GridView, Connect it to the table by selecting all the fields.

    - Go to the 'Edit-Columns' of the GridView.
    - Add a 'template-field' (push it up to the top using the arrow mark)
    - Also, convert the 'id' field into 'Template-Field' (using Convert to Template)
    - Hit OK.

    - Now, go to the 'Edit-Template' of the GridView.
    - In the 'Column[0]' of the Item-Template, drag-drop a CheckBox
    - Check out whats the ID of the CheckBox (must be CheckBox1)

    - Also, check the 'Column[1]' of the 'Item-Template
    - You'll see a Label; check the ID of the Label (must be Label1)

    - That's it.
    Now Stop 'Editing Template'

    Drag-Drop a Button on the Page & name it as Delete.
    Double-Click the button & include the following code in the Code-Behing of the Double-Click event.

    ***************************************************
    protected void Button1_Click(object sender, EventArgs e)
        {
            string str = "";

            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)gr.Cells[0].FindControl("CheckBox1");
                if (cb.Checked)
                {
                    Label lbl = (Label)gr.Cells[1].FindControl("Label1");

                    // Delete
                    str = "delete from yMail where id=" + lbl.Text;
                    SqlDataSource1.DeleteCommand = str;
                    SqlDataSource1.Delete();
                }
            }
        }

    ***********************************************************

    Ya, this will help u.

    Note : If u dont want id to be displayed in ur GridView, then u can set the 'id' field visibility to false, in 'Edit-Colums' option.

    But still u can access the Label Filed

    "Sometimes, Difficult things are possible"
  • Re: Accessing Checkbox on GridView

    09-05-2008, 2:53 PM
    • Member
      4 point Member
    • freesmile50
    • Member since 09-05-2008, 6:33 PM
    • Posts 2

     I have same or similar question regarding the checkbox in gridview - I want to pass the id from different column to CheckBox1_CheckedChanged event where i am doing database call to update the field. For example - If the user checks the checkbox then i will update as in 1 for that particular id in DB and similarly if unchecks the checked one then i will update as 0 in the DB. I am posting my code. I need some help on this asap.

    C# code

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {

            string Ischecked;
            CheckBox checkbox = (CheckBox)sender;
            i = checkbox.Text; // Currently i am passing the checkbox control value.
          

            if (checkbox.Checked == true)
            {
                Ischecked = "1";
            }
            else
            {
                Ischecked = "0";
            }
            boTimeLine ActivityComplete = new boTimeLine();
            int Done = ActivityComplete.UpdateActivityFlag(i, Ischecked);
        }

     ASP.net part

     <asp:TemplateField HeaderText="Keys">
                                                                        <ItemTemplate>
                                                                            <asp:Label ID="ToDoIds" runat="server"
                                                                                Text='<%# DataBinder.Eval(Container,"DataItem.ToDoId") %>' Visible="false"></asp:Label>
                   </ItemTemplate>
       <ItemStyle Width="230px" />
     </asp:TemplateField>


    <asp:TemplateField HeaderText="IsComplete">

           <ItemTemplate>
            <%-- <asp:HiddenField ID="HiddenField1" runat="server" />--%>
                               
         <asp:CheckBox runat="server" Text='<%# Eval("ToDoId") %>' id="CheckBox1"
           Checked= '<%# IsChecked(DataBinder.Eval(Container,"DataItem.IsComplete")) %>'
                                                                                OnCheckedChanged="CheckBox1_CheckedChanged"    AutoPostBack="true"/>

    </ItemTemplate>

    <ItemStyle Width="30px" />
      </asp:TemplateField>

  • Re: Accessing Checkbox on GridView

    09-09-2008, 3:12 PM
    • Member
      4 point Member
    • freesmile50
    • Member since 09-05-2008, 6:33 PM
    • Posts 2

    I finally figured and fixed the issue. Thanks anyways.

  • Re: Accessing Checkbox on GridView

    09-25-2009, 6:20 PM
    • Member
      186 point Member
    • KevInKauai
    • Member since 04-23-2007, 12:51 AM
    • Posts 251

     Odd, indeed! I realize this thread is 2+ years old, but why am I now seeing the first CheckBox anomalies?

    I typically use a GridView for a "master/detail" relationship, where I use the GriVBiew to show the available items and then have a "Select" button allow the use to inspect the complete details of the item and make revisions on a more traditional manner. For this, I typically use statements like the following to extract the row details:

    txtPresOrder.Text = GridView1.Rows(j).Cells(3).Text


    The seeming equivalent would be:

    Dim lvIsNewWindow As Boolean = GridView1.Rows(j).Cells(5).ToString
    Conversion from string "System.Web.UI.WebControls.DataCo" to type 'Boolean' is not valid.
    So WHAT is the standard, approved straightforward way of retrieving a simple checkbox/boolean value in a GridView?
     
     
     


     

  • Re: Accessing Checkbox on GridView

    12-04-2009, 2:25 PM
    • Member
      2 point Member
    • hoangnm
    • Member since 10-12-2009, 9:58 AM
    • Posts 1

    @willydavidjr:thank you!

Page 1 of 1 (15 items)