Checkbox in GridTemplateColumn

10 posts, 1 answers
  1. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 08 Nov 2008 Link to this post

    I am using a GrdTemplateColumn in order to display a CheckBox (Instead of a CheckBoxColumn) because I need the user to be able to check and uncheck the checkbox without first having to click EDIT.

    The checkbox needs to be able to do an Asynchronus postback to the database (bit value).

    I can get the checkbox to display, but I don't know how to get it to fire in the CodeBehind in order to update the appropriate row in the database.

                    <telerik:GridTemplateColumn Reorderable="true" DataField="isSold" HeaderText="Sold" UniqueName="Sold">  
                        <HeaderStyle ForeColor="Blue" Font-Underline="true" /> 
                        <ItemTemplate> 
                            <asp:CheckBox AutoPostBack="true" OnCheckedChanged="CheckBox1_Changed" ID="CheckBox1" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "isSold") %>' /> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 Nov 2008 Link to this post

    Hi Chasel,

    Try the following code snippet to access the CheckBox value in the CheckChanged event.

    ASPX:
    <telerik:GridTemplateColumn UniqueName="TempCol1" HeaderText="TempCol1"
          <ItemTemplate> 
              <asp:CheckBox ID="CheckBox1" runat="server"   AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged"  /> 
          </ItemTemplate> 
        </telerik:GridTemplateColumn> 

    CS:
     protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
        { 
            CheckBox chkbx = (CheckBox)sender; 
     
            //to access the row containg the Corresponding CheckBox 
            GridDataItem item = (GridDataItem)chkbx.NamingContainer; 
            // to get the CheckBox value 
            bool checkValue = chkbx.Checked; 
        } 


    Thanks
    Shinu.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 10 Nov 2008 Link to this post

    Ok this makes some sense to me, however I need to figure out how to pass the ID record back to the database in order to update the appropriate field.

    (
    @ID  int,
    @check  bit
    )

    Update Table
    Set check = @check
    Where ID = @ID

    Unless you can think of a better way  [:S]
  5. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 10 Nov 2008 Link to this post

    I have changed my GridTemplateColumn to look like this
                        <ItemTemplate> 
                            <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ID") %>' /> 
                            <asp:CheckBox AutoPostBack="true" OnCheckedChanged="CheckBox1_Changed" ID="CheckBox1" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "isFound") %>' /> 
                        </ItemTemplate> 
     so now I just need to access the HiddenField somehow.  This obviously doesn't work
        Protected Sub CheckBox1_Changed(ByVal sender As ObjectByVal e As EventArgs)  
            Dim chkbx As CheckBox = DirectCast(sender, CheckBox)  
            Dim hid As HiddenField = DirectCast(sender, HiddenField)  
            'TODO::  
            'to access the row containg the Corresponding CheckBox    
            Dim item As GridDataItem = DirectCast(chkbx.NamingContainer, GridDataItem)  
            ' to get the CheckBox value    
            Dim checkValue As Boolean = chkbx.Checked  
            Response.Write(hid.Value & " " & If(chkbx.Checked = True"Yes""No"))  
        End Sub 'CheckBox1_Changed 
    because the "sender" is not a HiddenField.  I know I need to probably use FindControl somehow, just not sure how.
  6. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 Nov 2008 Link to this post

    Hi Chase,

    Try the following code snippet to access the hiddenfield in the CheckChanged event of CheckBox.

    VB:
     
     
         Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) 
             Dim chkbx As CheckBox = DirectCast(sender, CheckBox) 
             
             'to access the row containg the Corresponding CheckBox 
             Dim item As GridDataItem = DirectCast(chkbx.NamingContainer, GridDataItem) 
             
             'to access the hidden field 
             
             Dim hiddenfld As HiddenField = DirectCast(item.FindControl("HiddenField1"), HiddenField) 
             Dim checkValue As Boolean = chkbx.Checked 
         End Sub 
     
     


    Thanks
    Shinu.
  7. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 10 Nov 2008 Link to this post

    Thanks Shinu, that is what I needed.  I guess I just don't work with these controls often enough.
  8. Spiros
    Spiros avatar
    15 posts
    Member since:
    May 2010

    Posted 30 Jun 2014 Link to this post

    Hi Shinu.
    I have a gridtemplateCollum and i wand to display a chechbox or textbox based on a simple criteria.
    Any Idea how this can be done ?
    Thanks in advance
    Spiros
  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 01 Jul 2014 in reply to Spiros Link to this post

    Hi Spiros,

    Please try the following code snippet.

    ASPX:
    <telerik:GridTemplateColumn>
        <ItemTemplate>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            <asp:CheckBox ID="chkName" runat="server" Visible=false />
        </ItemTemplate>
    </telerik:GridTemplateColumn>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
      if (e.Item is GridDataItem)
      {
        GridDataItem dataItem = (GridDataItem)e.Item;
        if (condition)
        {
         TextBox txtName = (TextBox)dataItem.FindControl("txtName");
         txtName.Visible = false;
         CheckBox chkName = (CheckBox)dataItem.FindControl("chkName");
         chkName.Visible = true;
        }
      }
    }

    Thanks,
    Shinu
  10. Spiros
    Spiros avatar
    15 posts
    Member since:
    May 2010

    Posted 01 Jul 2014 in reply to Shinu Link to this post

    Hi Shinu  and thanks for your quick answer.
    I implemented that and works fine except when i click paging or filtering , the grid loose the initial selection(check box ).
    Any Idea why this happens ?

    protected void grdData_ItemCreated(object sender, GridItemEventArgs e)
            {
                foreach (GridDataItem item in grdData.MasterTableView.Items)
                {
                    CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
                    Image img = (Image)item.FindControl("Image1");

                    // TextBox1
                    TextBox txt = (TextBox)item.FindControl("TextBox1");
                    String ID = SafeConvert.StringToEmpty(item["paymentlistid"].Text);
                    String contract = SafeConvert.StringToEmpty(item["ContrNum"].Text);
                    String Plafon = SafeConvert.StringToEmpty(item["plafon"].Text);

                    if (Plafon.Equals("True"))
                        img.ImageUrl = "~/images/py.png";
                    else
                        img.ImageUrl = "~/images/np.png";


                    if (ID.Equals(""))
                    {
                        chk.Visible = true;
                        txt.Visible = false;
                    }
                    else
                    {
                        chk.Visible = false;
                        txt.Visible = true;
                        txt.Text = ID;
                    }
                }

            }

     protected void grdData_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
            {

                IList<ReceiptsGrid> lst = null;

                if (e.RebindReason != GridRebindReason.InitialLoad)
                {
                    lst = productReceiptedService.GetGridReceipts(0, SafeConvert.TryCastInteger(cmbPraktorio.SelectedValue.ToString()),
                                            SafeConvert.TryCastString(dtFrom.SelectedDate),
                                            SafeConvert.TryCastString(dtTo.SelectedDate));
                    grdData.DataSource = lst;
                }
            }

    Thanks in advance
    Spiros
  11. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 01 Jul 2014 in reply to Spiros Link to this post

    Hi Spiros,

    Its the default behavior, a checkbox selection is not persisted on postback. Please take a look at the following documentation to know how to set Persisting CheckBox control state in GridTemplateColumn on rebind.

    Thanks,
    Shinu
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017