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

Client-Side coding for RadGrid programmatically Created

2 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Kucharski
Top achievements
Rank 1
David Kucharski asked on 14 Feb 2013, 08:22 PM
I am creating my RadGrid programmatically in the Page_Init. During the process I am creating a GridTemplateColumn that will be used as a CheckBox. I am doing this to convert my database value to a boolean. I have also added an attribute to the checkbox so that when they click the checkbox I want to run some javascript. In that javascript I would like to gather some of the values in the other cells on the row of the checkbox I just clicked in. So say I have 5 columns in my grid, the third column has checkboxes and there are a total of 100 rows. When I click the checkbox of the the third row, I want to gather the values of all 5 columns for the row of the checkbox I clicked. I see a lot of Client-side scripting methods but unfortunately am new to the RadGrid. I did set the OnGridSelected to a javascript function to get the RadGrid object but that is as far as I know how to go. Please let me know.

RadGrid1.ClientSettings.ClientEvents.OnGridCreated =

 

"GetGridObject"

 

 

 

var RadGrid1;

 

 

 

function GetGridObject(sender, eventArgs)

 

{

RadGrid1 = sender;

}

 

 

 

 

 

Public Class MyTemplate
    Implements ITemplate
    Protected textBox As TextBox
    Protected boolValue As CheckBox
    Private colname As String
    Public Sub New(ByVal cName As String)
        colname = cName
    End Sub
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
        boolValue = New CheckBox()
        boolValue.ID = colname
        AddHandler boolValue.DataBinding, _
                   AddressOf boolValue_DataBinding
        boolValue.Enabled = True
        container.Controls.Add(boolValue)
    End Sub
    Sub boolValue_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
        Dim cBox As CheckBox = DirectCast(sender, CheckBox)
        Dim container As GridDataItem = DirectCast(cBox.NamingContainer, GridDataItem)
        If ((DirectCast(container.DataItem, DataRowView))(colname)) = 1 Or ((DirectCast(container.DataItem, DataRowView))(colname)) = True Then
            cBox.Checked = True
        Else
            cBox.Checked = False
        End If
        cBox.Attributes.Add("onClick", "Javascript:fnOpenGeoInfoView('')")
    End Sub
    Sub textBox_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
        Dim cTextBox As TextBox = DirectCast(sender, TextBox)
        Dim container As GridDataItem = DirectCast(cTextBox.NamingContainer, GridDataItem)
        cTextBox.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()
    End Sub
End Class

2 Answers, 1 is accepted

Sort by
0
David Kucharski
Top achievements
Rank 1
answered on 19 Feb 2013, 03:35 PM
Can anyone help with this post? Hopefully there is a way, or I would need to pass in all of the information needed into my MyTemplate class and use it within.

Please let me know.
0
Eyup
Telerik team
answered on 20 Feb 2013, 07:32 AM
Hi David,

You can use the following approach to achieve the requested functionality:
checkBox.Attributes.Add("onclick", "checkBoxClicked(this,event,"
    + dataItem.ItemIndex + ")");
JavaScript:
function checkBoxClicked(checkBox, event, index) {
    var masterTable = $find("<%= grid.ClientID %>").get_masterTableView();
    var row = masterTable.get_dataItems()[index];
    var value = row.get_cell("ContactName").textContent;
}

That should do the trick. Please give it a try and let me know if it works for you.

Additionally, please ensure that you are closely following the steps provided in the following topic:
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section4
( Section Creating Template Columns Programmatically )

You can also check out this article for accessing the cell values on client side:
http://www.telerik.com/help/aspnet-ajax/grid-getting-cell-values-for-selected-rows-client-side.html

Hope this helps.

Regards,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
David Kucharski
Top achievements
Rank 1
Answers by
David Kucharski
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or