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

oncheckchanged event inside rad grid

3 Answers 1210 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Web Services
Top achievements
Rank 2
Web Services asked on 08 Dec 2011, 07:18 PM
I have a grid template column as such

<telerik:GridTemplateColumn HeaderText="Don't Export" HeaderStyle-Width="40px" DataField="exportCheck"
                SortExpression="exportCheck" UniqueName="exportCheck">
                    <ItemStyle Width="40px" />
                    <ItemTemplate>
                        <asp:CheckBox ID="printCheck" runat="server" AutoPostBack="false" OnCheckedChanged="printCheckChanged" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>


I am then editing the checkboxes in the code behind to dynamically set them to checked or not

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
 
        If TypeOf e.Item Is GridDataItem Then
 
            'get the row item
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
 
            'check to see if we need to check the box
            Dim export = DirectCast(item("export").Text, String)
 
            'get the checkbox
            Dim check As CheckBox = DirectCast(item("exportCheck").Controls(1), CheckBox)
 
            'if we need to check the box then we will get it and check it
            If (export = "no") Then
 
                check.Checked = True
 
            End If 'if export
 
        End If 'if e.item
         
    End Sub 'itemDataBound

What I need to do is add the oncheckchanged event. Essentially, when a user either checks a box, or unchecks a box, I need to update a database and then show a tool tip telling them it was successful. I tried doing this programmatically, and adding it in the aspx but I can't get anything to work. Can you help me out?

3 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Dec 2011, 08:34 PM
Hello,

please check below code snippet.
<telerik:GridTemplateColumn UniqueName="MyColumn">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged"
                                AutoPostBack="true" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox CheckBox1 = sender as CheckBox;
             
            // you can access grid row here
            GridDataItem item = CheckBox1.NamingContainer as GridDataItem;
 
            // perform here DB operation
            TableCell cell = item["MyColumn"];
            cell.ToolTip = DateTime.Now + "_Updated";
        }


Thanks,
Jayesh Goyani
0
Natalya
Top achievements
Rank 1
answered on 05 Sep 2018, 08:57 PM

Hello Jayesh,

I used your code for my checkbox event. It works great. Could you also suggest how to manipulate with values from drop-down list on selected index changed event? I tried to use the same code you have suggested for checkbox, but I got an error: "Unable to cast object of type 'PanelNamingContainer' to type 'Telerik.Web.UI.GridDataItem ".

This is my code:

protected void ddlAssign_SelectedIndexChanged(object sender, DropDownListEventArgs e)
        {
            RadDropDownList ddlAssign = (RadDropDownList)sender;
            GridDataItem item = (GridDataItem)ddlAssign.NamingContainer;

            HiddenField hqc_DateAsigned = (HiddenField)item.FindControl("hqc_DateAsigned");
            hqc_DateAsigned.Value = DateTime.Now.ToShortDateString();
        }

0
Tsvetomir
Telerik team
answered on 10 Sep 2018, 02:43 PM
Hi Natalya,

The error you have encountered probably stems from the return type of the NamingContainer. If you have included the RadDropDownList into an EditTemplate, and you are using an Edit mode different than the "InPlace" one, the editable item is of type GridEditFormItem or GridEditFormInsertItem, rather than GridDataItem or GridDataInsertItem. You can find more information here: Accessing Controls in Edit/Insert Mode.

I suggest you to go over the articles mentioned in the Control inside EditTemplate forum thread.  

If the issue persists, it would be helpful if you could share the column definition and the code snippet responsible for initializing the RadDropDownList control. Ideally, you can modify one of the samples provided in the forum post from the above and send it back to us.

Kind regards,
Tsvetomir
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Web Services
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Natalya
Top achievements
Rank 1
Tsvetomir
Telerik team
Share this question
or