This is a migrated thread and some comments may be shown as answers.

Checkbox in GridTemplateColumn

9 Answers 864 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chase Florell
Top achievements
Rank 1
Chase Florell asked on 08 Nov 2008, 05:57 PM
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> 

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Nov 2008, 07:30 AM
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.

0
Chase Florell
Top achievements
Rank 1
answered on 10 Nov 2008, 04:00 PM
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]
0
Chase Florell
Top achievements
Rank 1
answered on 10 Nov 2008, 06:25 PM
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.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 11 Nov 2008, 03:51 AM
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.
0
Chase Florell
Top achievements
Rank 1
answered on 11 Nov 2008, 04:25 AM
Thanks Shinu, that is what I needed.  I guess I just don't work with these controls often enough.
0
Spiros
Top achievements
Rank 1
answered on 30 Jun 2014, 04:27 PM
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
0
Shinu
Top achievements
Rank 2
answered on 01 Jul 2014, 05:33 AM
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
0
Spiros
Top achievements
Rank 1
answered on 01 Jul 2014, 08:59 AM
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
0
Shinu
Top achievements
Rank 2
answered on 02 Jul 2014, 03:13 AM
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
Tags
Grid
Asked by
Chase Florell
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Chase Florell
Top achievements
Rank 1
Spiros
Top achievements
Rank 1
Share this question
or