Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Checkbox inside gridview.

Checkbox inside gridview.

Scheduled Pinned Locked Moved ASP.NET
questionsysadmin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fififlowertot
    wrote on last edited by
    #1

    Hi, I have a checkbox inside a gridview which is bound to data at runtime except the checkbox templatefield. At server side I am trying to capture the row details for the row which is selected. I get the checkbox but the checkbox.checked is always false even when it is selected.

    protected void btnSelect_Click(object sender, EventArgs e)
    {
    foreach (GridViewRow row in GridView1.Rows)
    {

            CheckBox chk = (CheckBox)row.FindControl("chkAgency");
                       
            
            if (chk != null && chk.Checked)             
            {
                Response.Cookies\["thistestcookie"\].Value = row.Cells\[0\].Text.ToString();
             }         
        }     
    

    how do i get the checked checkbox row details ?! Thanks, Fifi

    V 1 Reply Last reply
    0
    • F fififlowertot

      Hi, I have a checkbox inside a gridview which is bound to data at runtime except the checkbox templatefield. At server side I am trying to capture the row details for the row which is selected. I get the checkbox but the checkbox.checked is always false even when it is selected.

      protected void btnSelect_Click(object sender, EventArgs e)
      {
      foreach (GridViewRow row in GridView1.Rows)
      {

              CheckBox chk = (CheckBox)row.FindControl("chkAgency");
                         
              
              if (chk != null && chk.Checked)             
              {
                  Response.Cookies\["thistestcookie"\].Value = row.Cells\[0\].Text.ToString();
               }         
          }     
      

      how do i get the checked checkbox row details ?! Thanks, Fifi

      V Offline
      V Offline
      Vimalsoft Pty Ltd
      wrote on last edited by
      #2

      lets see the markup where you defined the checkbox ?

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

      F 1 Reply Last reply
      0
      • V Vimalsoft Pty Ltd

        lets see the markup where you defined the checkbox ?

        Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

        F Offline
        F Offline
        fififlowertot
        wrote on last edited by
        #3
        <Columns>
                <asp:BoundField DataField = "Name1" HeaderText = "NAME" />
                <asp:BoundField DataField ="Name2" HeaderText = "NAME2"/>
                <asp:BoundField DataField = "Name3" HeaderText = "NAME3"/>
                <asp:TemplateField>
                   <ItemTemplate>
                      <asp:CheckBox id ="chkAgency" runat="server"  Enabled />
                   </ItemTemplate>            
                </asp:TemplateField>
         </Columns>
        
        V 1 Reply Last reply
        0
        • F fififlowertot
          <Columns>
                  <asp:BoundField DataField = "Name1" HeaderText = "NAME" />
                  <asp:BoundField DataField ="Name2" HeaderText = "NAME2"/>
                  <asp:BoundField DataField = "Name3" HeaderText = "NAME3"/>
                  <asp:TemplateField>
                     <ItemTemplate>
                        <asp:CheckBox id ="chkAgency" runat="server"  Enabled />
                     </ItemTemplate>            
                  </asp:TemplateField>
           </Columns>
          
          V Offline
          V Offline
          Vimalsoft Pty Ltd
          wrote on last edited by
          #4

          ok i see where your problem is. You assume if you add a checkbox, it will carry the ID of the checked field , that is not true. I normally create an extra template field and bind the label with the value and as you did on the server side, i find the label and get the value. I have compiled an example and this will help you html

          <asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" 
              DataSourceID="SqlDataSource1">
              <Columns>
                  <asp:TemplateField>
                  <ItemTemplate>
                  <asp:CheckBox ID="chkAgency" runat="server" />
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField>
                  <ItemTemplate>                     
                  
                  <asp:Label ID="lblid" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:BoundField DataField = "ID" HeaderText = "ID" InsertVisible="False" 
                      ReadOnly="True" SortExpression="ID" />
                  <asp:BoundField DataField ="Descr" HeaderText = "Descr" SortExpression="Descr"/>
                  <asp:BoundField DataField = "LongName" HeaderText = "LongName" 
                      SortExpression="LongName"/>
                  <asp:BoundField DataField="Capacity" HeaderText="Capacity" 
                      SortExpression="Capacity" />
                  <asp:BoundField DataField="Note" HeaderText="Note" SortExpression="Note" />
           </Columns>
          </asp:GridView>
          <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
              ConnectionString="<%$ ConnectionStrings:TTBLv3MasterConnectionString %>" 
              SelectCommand="select \* from dbo.TBL\_VENUE"></asp:SqlDataSource>
          

          <asp:Button ID="btntest" Text="Test" runat="server" onclick="btntest_Click" />

          and the server side

          protected void btntest_Click(object sender, EventArgs e)
          {
          foreach (GridViewRow row in GridView1.Rows)
          {

                  CheckBox chk = (CheckBox)row.FindControl("chkAgency");
          
          
                  Label lblid = (Label)row.FindControl("lblid");
          
                  List<string> lstList = new List<string>();
          
                  if (chk != null && chk.Checked)
                  {
                      //Response.Cookies\["thistestcookie"\].Value = row.Cells\[0\].Text.ToString();
                      lstList.Add(lblid.Text);
                  }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups