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

How to get a radio buttonlist selected value in a grid

5 Answers 812 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe Contreras
Top achievements
Rank 1
Joe Contreras asked on 21 Dec 2011, 03:33 PM
Hi,

I'm working for a client that needs to display a list of programs in a grid and have a list of sub-programs under each program.  Those subprograms AKA locals need to have radio buttonlist, because the user can have the choice to select any options and update those options.

I have three datatables:
1. Program data
2. All locals
3. Userlocals
Please note that dt 2 & 3 need to be merged.  In order for the selectedvalue in the radio buttonlist to be selected (represents a user selection).

I'd like to know how to accomplish this.  This is what I have so far

HTML:
<telerik:RadGrid ID="rgMyGrid" runat="server" Skin="Office2007" AutoGenerateColumns="false">
            <MasterTableView CommandItemDisplay="Top" ShowHeader="true">
                <CommandItemTemplate>
                    <div style="padding: 5px;">
                        Below is a listing of programs for the selected client. You can add this user to
                        one or more programs by expanding the program node and selecting a localization.
                        You must select at least one program/localization.
                    </div>
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridBoundColumn DataField="ProgramName" HeaderText="Programs" />
                    <telerik:GridButtonColumn CommandName="Delete" Text="Remove" UniqueName="RemoveColumn"
                        ButtonType="PushButton" />
                </Columns>
                <NestedViewTemplate>
                    <div style="width: 100%; padding-left: 10px;">
                        <asp:RadioButtonList runat="server" ID="rdoLocals"/>
                    </div>
                </NestedViewTemplate>
            </MasterTableView>
        </telerik:RadGrid>
        <telerik:RadTreeView ID="rtvProgramsLocals" runat="server" Skin="Outlook" Visible="false">
        </telerik:RadTreeView>

Here is what I have in the VB.NET code-behind

  If Not IsPostBack Then
  
            rgMyGrid.DataSource = LoadPrograms(100)
            rgMyGrid.DataBind()
  
   End If
  
Private Function LoadPrograms(ByVal ClientId As Integer) As DataTable
        'Dim p360programs As Programs = Nothing
        Dim programsDT As New DataTable("Programs")
        Dim localsDT As New DataTable("Locals")
        Dim userLocalDT As New DataTable("UserLocal")
        Dim localOptions As RadioButtonList
        'p360programs = New Programs(ConfigurationManager.ConnectionStrings("P360ConnectionString").ConnectionString)
        Dim i As Integer
        Dim j As Integer
        Dim programNode As RadTreeNode
        Dim localNode As RadTreeNode
  
        programsDT.Columns.Add("ProgramID", GetType(Int32))
        programsDT.Columns.Add("ClientID", GetType(Int32))
        programsDT.Columns.Add("UserID", GetType(Int32))
        programsDT.Columns.Add("ProgramName", GetType([String]))
        programsDT.PrimaryKey = New DataColumn() {programsDT.Columns("ProgramID")}
        programsDT.Rows.Add(New Object() {100, 100, 7628, "Internal Administration"})
        programsDT.Rows.Add(New Object() {106, 100, 7628, "TestProgram"})
        programsDT.Rows.Add(New Object() {107, 100, 7628, "TestProgram1"})
        programsDT.AcceptChanges()
  
        localsDT.Columns.Add("LocalizationID", GetType(Int32))
        localsDT.Columns.Add("ProgramID", GetType(Int32))
        localsDT.Columns.Add("LocalizationName", GetType([String]))
        localsDT.PrimaryKey = New DataColumn() {localsDT.Columns("LocalizationID")}
        localsDT.Rows.Add(New Object() {100, 100, "Setup Tool"})
        localsDT.Rows.Add(New Object() {105, 106, "MyLocal"})
        localsDT.Rows.Add(New Object() {106, 106, "MyLocal2"})
        localsDT.Rows.Add(New Object() {107, 107, "Local1a"})
        localsDT.AcceptChanges()
  
        userLocalDT.Columns.Add("LocalizationID", GetType(Int32))
        userLocalDT.Columns.Add("LocalizationName", GetType([String]))
        userLocalDT.PrimaryKey = New DataColumn() {userLocalDT.Columns("LocalizationID")}
        userLocalDT.Rows.Add(New Object() {100, "Setup Tool"})
        userLocalDT.Rows.Add(New Object() {105, "MyLocal From the secondary Table"})
        userLocalDT.AcceptChanges()
        'programsDT = p360programs.GetPrograms(ClientId)
  
        If programsDT.Rows.Count > 0 Then
            For i = 0 To programsDT.Rows.Count - 1
                If localsDT.Rows.Count > 0 Then
  
                    localsDT.Merge(userLocalDT, False, MissingSchemaAction.Ignore)
                    localOptions = New RadioButtonList
  
                    Dim gridRow As GridDataItem
  
                    For Each gridRow In rgMyGrid.MasterTableView.Items
  
                        localOptions = CType(gridRow.ChildItem.FindControl("rdoLocals"), RadioButtonList)
  
                        For j = 0 To userLocalDT.Rows.Count - 1
                            localOptions.Items.Add(New ListItem(localsDT(j)("LocalizationName"), localsDT(j)("LocalizationID")))                            
                        Next
  
                        ' add the radio button to the localization radtree node
                        localNode = New RadTreeNode()
                        localNode.Controls.Add(localOptions)
  
                        ' add the localization node to the program node
                        programNode = New RadTreeNode(programsDT.Rows(i)("ProgramName"))
                        programNode.Nodes.Add(localNode)
  
                        ' add the program node to the tree view control
                        rtvProgramsLocals.Nodes.Add(programNode)
  
                    Next
                      
  
                End If
  
            Next
  
        End If
          
        Return programsDT
  
    End Function

I've also attached a snapshot of what I'm trying to achieve.

Thanks,

5 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 23 Dec 2011, 09:52 AM
Hello Joe,

Where in the life cycle of the page you are calling the LoadPrograms function? If it is in Page_Init or Page_Load it is too early and RadGrid has not yet created its items and you still can not access them.

You should access the RadioListButtton on the ItemDataBound event like this:

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridNestedViewItem Then
        Dim nestedViewItem As GridNestedViewItem = TryCast(e.Item, GridNestedViewItem)
        Dim productName As String = DirectCast(nestedViewItem.DataItem, System.Data.DataRowView)("ProductName").ToString()
        Dim buttonList As RadioButtonList = TryCast(nestedViewItem.FindControl("RadioButtonList1"), RadioButtonList)
 
        buttonList.SelectedIndex = 1
    End If
End Sub


If you want to use the DataTable for binding you could make it global for the class. The above approach uses the data item of the respective nested view item. You could check this help topic for more information about RadGrid hierarchy elements.

Also note that if you want to use advanced functions of RadGrid like Paging, Sorting, Filtering and so on you need to use advanced databinding, like described in this help topic.

Regards,
Andrey
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
0
Joe Contreras
Top achievements
Rank 1
answered on 24 Dec 2011, 12:18 AM
Hi,
Thanks for the reply:
I have the LoadPrograms method in the Page_Load event .  

The workflow is 
1. Retrieve all Programs load them into a DT (datatable)
2. Create a secondary DT containing locals for each program which have a programID(FK)
3. Create the users locals DT which has a relationship with locals which have a localizationID(FK)
The functionality here is to display each program with it's associated locals and in the radio button only the user locals selected.
 
UI representation:

Program1 - (ProgramsDT)
Local1
Local2
Local3 - this is the userLocalDT selected item in the radiobuttonlist - please note that this userLocalDT matches the localsDT id hence why it's the selected item
Program2
Local1
Local2 - this is the selected item in the radionbuttonlist
Program3
Local1 - This is the selected in the radiobuttonlist

The only way I could think of is by doing three loops within each DT.  I can't think of any other way.  Please note that I can't use a stored procedure to get all data for each relationship.

Where I'm struggling is how to program this within the UI?

I hope this helps in explaining my scenario further.

Thanks,
0
Andrey
Telerik team
answered on 27 Dec 2011, 05:06 PM
Hello Joe,

PageLoad event is too early in the page life cycle and the RadGrid items are not created yet, because of that you could not access them. In order to be able to access the RadGrid items you need to move the logic from the LoadPrograms function to the ItemDataBound event. In this case you won't need any loops, because ItemDataBound is raised for every item in RadGrid.

In my previous post you should change the "ProductName" field with the one you are depending on to bind the Radio Button List.

Additionally I have created sample project based on your scenario. Please give it a try and check whether suits your needs.

All the best,
Andrey
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
0
Joe Contreras
Top achievements
Rank 1
answered on 27 Dec 2011, 05:54 PM
Thanks.  Now for whatever my ID is not being passed.  Here's the HTML and code-behind:

<telerik:RadGrid ID="rgMyGrid" runat="server" Skin="Office2007" AutoGenerateColumns="false"
            DataSourceID="ProgramDataSource">
            <MasterTableView CommandItemDisplay="Top" ShowHeader="true" DataKeyNames="ProgramID"
                DataSourceID="ProgramDataSource">
                <CommandItemTemplate>
                    <div style="padding: 5px;">
                        Below is a listing of programs for the selected client. You can add this user to
                        one or more programs by expanding the program node and selecting a localization.
                        You must select at least one program/localization.
                    </div>
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridBoundColumn DataField="ProgramName" HeaderText="Programs" />
                    <telerik:GridButtonColumn CommandName="Delete" Text="Remove" UniqueName="RemoveColumn"
                        ButtonType="PushButton" />
                </Columns>
                <NestedViewSettings DataSourceID="LocalDatSource">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="ProgramID" MasterKeyField="ProgramID" />
                    </ParentTableRelation>
                </NestedViewSettings>
                <NestedViewTemplate>
                                        <div style="width: 100%; padding-left: 10px;">
                        <asp:RadioButtonList runat="server" ID="rdoLocals" DataTextField="LocalizationName"
                            DataValueField="LocalizationID" DataSourceID="LocalDataSource">
                        </asp:RadioButtonList>
                </NestedViewTemplate>
            </MasterTableView>
        </telerik:RadGrid>
        <telerik:RadTreeView ID="rtvProgramsLocals" runat="server" Skin="Outlook" Visible="false">
        </telerik:RadTreeView>
    </div>
      <asp:ObjectDataSource ID="LocalDataSource" runat="server" SelectMethod="GetTestLocalizations"
        TypeName="CPLDataSourceFunctions">
        <SelectParameters>
            <asp:Parameter Name="ProgramID"/>            
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:ObjectDataSource ID="ProgramDataSource" runat="server" SelectMethod="GetTestPrograms"
        TypeName="CPLDataSourceFunctions"></asp:ObjectDataSource>

Code-Behind:
Public Function GetTestLocalizations(ByVal ProgramID As Integer) As DataTable
        Dim localsDT As New DataTable("Locals")
        Dim userLocalDT As New DataTable("UserLocal")
        Dim dtAll As New DataTable("All")
  
        localsDT.Columns.Add("LocalizationID", GetType(Int32))
        localsDT.Columns.Add("ProgramID", GetType(Int32))
        localsDT.Columns.Add("LocalizationName", GetType([String]))
        localsDT.Columns.Add("LocalMatch", GetType([Boolean]))
        localsDT.PrimaryKey = New DataColumn() {localsDT.Columns("LocalizationID")}
        localsDT.Rows.Add(New Object() {100, 100, "Setup Tool", False})
        localsDT.Rows.Add(New Object() {105, 106, "MyLocal", False})
        localsDT.Rows.Add(New Object() {106, 106, "MyLocal2", False})
        localsDT.Rows.Add(New Object() {107, 107, "Local1a", False})
        localsDT.AcceptChanges()
  
        userLocalDT.Columns.Add("LocalizationID", GetType(Int32))
        userLocalDT.Columns.Add("LocalizationName", GetType([String]))
        userLocalDT.Columns.Add("LocalMatch", GetType([Boolean]))
        userLocalDT.PrimaryKey = New DataColumn() {userLocalDT.Columns("LocalizationID")}
        userLocalDT.Rows.Add(New Object() {100, "Setup Tool", False})
        userLocalDT.Rows.Add(New Object() {105, "MyLocal From the secondary Table", False})
        userLocalDT.AcceptChanges()
  
        dtAll = localsDT.Copy()
        dtAll.Merge(userLocalDT)
        dtAll.Select("ProgramID='" + Convert.ToString(ProgramID) + "'")
                  
        Return dtAll
End Function

Note that when I step through the code the ProgramID is 0.  I'm stuck on why this isn't working BTW, here is the program code:

Public Function GetTestPrograms() As DataTable
        Dim programsDT As New DataTable("Programs")
  
        Try
            programsDT.Columns.Add("ProgramID", GetType(Int32))
            programsDT.Columns.Add("ClientID", GetType(Int32))
            programsDT.Columns.Add("UserID", GetType(Int32))
            programsDT.Columns.Add("ProgramName", GetType([String]))
            programsDT.PrimaryKey = New DataColumn() {programsDT.Columns("ProgramID")}
            programsDT.Rows.Add(New Object() {100, 100, 7628, "Internal Administration"})
            programsDT.Rows.Add(New Object() {106, 100, 7628, "TestProgram"})
            programsDT.Rows.Add(New Object() {107, 100, 7628, "TestProgram1"})
            programsDT.AcceptChanges()
  
            Return programsDT
  
        Catch ex As Exception
            Throw ex
        Finally
            Try
  
            Catch ex As Exception
            End Try
        End Try
    End Function

Once I get this right then I can be able to use the item_databound event

Thanks,
0
Andrey
Telerik team
answered on 28 Dec 2011, 02:48 PM
Hi Joe,

As I see from your code the ProgramID variable is passed as a parameter to the GetTestLocalizations function and because you are not changing its value in the body of the function I think the problem with the "0" value is outside of this function.  I think the problem is in the function call and the parameter you are passing in this function call.

Could you share more of your code-behind so we could be able to find the source of issue.


Regards,
Andrey
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
Joe Contreras
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Joe Contreras
Top achievements
Rank 1
Share this question
or