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

reference control in nestedviewtemplate

3 Answers 85 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dingen
Top achievements
Rank 1
dingen asked on 15 Jan 2010, 11:54 AM
HI

i am trying to get the selectedvalue of a dropdownlist in a nestedviewtemplate? what could be wrong?

reg

D

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
</head> 
<body class="BODY"
    <form runat="server" id="mainForm" method="post"
    <telerik:RadScriptManager runat="server" ID="ScriptManager1"
    </telerik:RadScriptManager> 
    <!-- content start --> 
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" /> 
    <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" 
        AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True" PageSize="5" 
        GridLines="None" ShowGroupPanel="true"
        <PagerStyle Mode="NumericPages"></PagerStyle> 
        <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="EmployeeID" AllowMultiColumnSorting="True"
            <NestedViewTemplate> 
                <telerik:RadGrid runat="server" ID="nestedgrid" ShowFooter="true" AllowSorting="true" 
                    EnableLinqExpressions="false"
                    <MasterTableView ShowHeader="true" AutoGenerateColumns="true" AllowPaging="true" 
                        PageSize="7"
                        <Columns> 
                            <telerik:GridEditCommandColumn> 
                            </telerik:GridEditCommandColumn> 
                        </Columns> 
                        <EditFormSettings EditFormType="Template"
                            <FormTemplate> 
                                                                
                                <asp:ImageButton ID=imgb runat=server CommandName="go"  /> 
                                <asp:DropDownList id=ddl1  runat=server></asp:DropDownList> 
                            </FormTemplate> 
                        </EditFormSettings> 
                    </MasterTableView> 
                </telerik:RadGrid> 
            </NestedViewTemplate> 
            <Columns> 
                <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="First Name" HeaderButtonType="TextButton" 
                    DataField="FirstName" UniqueName="FirstName"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="LastName" HeaderText="Last Name" HeaderButtonType="TextButton" 
                    DataField="LastName" UniqueName="LastName"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                    DataField="Title" UniqueName="Title"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="BirthDate" DataFormatString="{0:MM/dd/yyyy}" 
                    HeaderText="Birth Date" HeaderButtonType="TextButton" DataField="BirthDate" UniqueName="BirthDate"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="City" HeaderText="City" HeaderButtonType="TextButton" 
                    DataField="City" UniqueName="City"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="Country" HeaderText="Country" HeaderButtonType="TextButton" 
                    DataField="Country" UniqueName="Country"
                </telerik:GridBoundColumn> 
            </Columns> 
        </MasterTableView> 
        <ClientSettings AllowDragToGroup="true" /> 
    </telerik:RadGrid> 
    <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Employees" 
        runat="server"></asp:SqlDataSource> 
    <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        ProviderName="System.Data.SqlClient" runat="server"></asp:SqlDataSource> 
    <!-- content end --> 
    </form> 
</body> 
 
Imports Telerik.Web.UI 
Imports System 
Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Web.UI.WebControls 
Imports System.Configuration 
Partial Class _Default 
    Inherits System.Web.UI.Page 
 
 
    Dim ConnString As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString 
 
    Protected Sub nestedgrid_ItemCreated(ByVal sender As ObjectByVal e As GridItemEventArgs) 
        Dim btn As Button 
        Dim ddl As DropDownList 
        If e.Item.ItemType = GridItemType.EditFormItem And e.Item.IsInEditMode Then 
            Dim edititem As GridEditFormItem = CType(e.Item, GridEditFormItem) 
            ddl = CType(edititem.FindControl("ddl1"), DropDownList) 
            btn = CType(edititem.FindControl("btngo"), Button) 
 
            ddl.DataSource = dsddl() 
            ddl.DataTextField = "ShipName" 
            ddl.DataValueField = "OrderID" 
            ddl.DataBind() 
 
        End If 
    End Sub 
 
    Protected Sub nestedgrid_itemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) 
 
         
        If e.CommandName = RadGrid.UpdateCommandName Then 
            If e.Item.ItemType = GridItemType.EditFormItem Then 
                Dim nestedview As GridNestedViewItem = CType((CType(e.Item, GridEditFormInsertItem)).OwnerTableView.Parent.NamingContainer, GridNestedViewItem) 
                Dim ddl1 As DropDownList = CType(nestedview.FindControl("ddl1"), DropDownList) 
                'i would like to assign to get the selected value of the ddl1  !! 
                'Dim result = ddl1.SelectedValue 
            End If 
        End If 
 
 
    End Sub 
 
 
 
    Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated 
 
        If TypeOf e.Item Is GridNestedViewItem Then 
            Dim rg As RadGrid = e.Item.FindControl("NestedGrid"
            Dim imgb As ImageButton = rg.MasterTableView.FindControl("imgb"
            AddHandler rg.ItemCreated, AddressOf nestedgrid_ItemCreated 
            AddHandler rg.ItemCommand, AddressOf nestedgrid_itemCommand 
            AddHandler rg.UpdateCommand, AddressOf nestedgrid_UpdateCommand 
            rg.DataSource = myds() 
            rg.DataBind() 
        End If 
 
    End Sub 
 
    Protected Sub nestedgrid_UpdateCommand() 
 
    End Sub 
 
    Public Function myds() As DataSet 
        Dim conn As SqlConnection = New SqlConnection(ConnString) 
        Dim adapter As SqlDataAdapter = New SqlDataAdapter 
        adapter.SelectCommand = New SqlCommand("SELECT TOP 10 OrderID, ShipName, ShipAddress FROM Orders", conn) 
        Dim myDataSet As DataSet = New DataSet 
 
        conn.Open() 
        Try 
            adapter.Fill(myDataSet, "Orders"
            adapter.SelectCommand = New SqlCommand("SELECT OrderID, Quantity FROM [Order Details]", conn) 
            adapter.Fill(myDataSet, "OrderDetails"
        Finally 
            conn.Close() 
        End Try 
        Return myDataSet 
 
    End Function 
 
 
    Public Function dsddl() As DataSet 
        Dim conn As SqlConnection = New SqlConnection(ConnString) 
        Dim adapter As SqlDataAdapter = New SqlDataAdapter 
        Dim myDataSet As DataSet = New DataSet 
 
        conn.Open() 
        Try 
 
            adapter.SelectCommand = New SqlCommand("SELECT TOP 10 OrderID, ShipName FROM Orders", conn) 
            adapter.Fill(myDataSet, "OrderDetails"
        Finally 
            conn.Close() 
        End Try 
        Return myDataSet 
 
    End Function 
 
End Class 
 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 15 Jan 2010, 02:10 PM
Hello,

I implemented the following code in order to achieve a similar scenario as yours, by using AdvancedDataBinding for the nested radgrid. Check this out and see if it helps:

VB:
 
Protected Sub nestedgrid_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
    If e.CommandName = "Go" Then 
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem) 
        Dim editItem As GridEditFormItem = DirectCast(item.EditFormItem, GridEditFormItem) 
         
        Dim doptdown As DropDownList = DirectCast(editItem.FindControl("ddl1"), DropDownList) 
        Dim value As String = doptdown.SelectedItem.Text 
        Response.Write(value) 
    End If 
End Sub 
Protected Sub nestedgrid_NeedDataSource(ByVal source As ObjectByVal e As GridNeedDataSourceEventArgs) 
    Dim grid As RadGrid = DirectCast(source, RadGrid) 
        ' Populate the grid        
    grid.DataSource = GetDataTable("SELECT * FROM Customers"
End Sub 

-Shinu.
0
dingen
Top achievements
Rank 1
answered on 15 Jan 2010, 11:26 PM
am i missing something ? it is not working? can you plse run the code

thnks

D

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
</head> 
<body class="BODY"
    <form runat="server" id="mainForm" method="post"
    <telerik:RadScriptManager runat="server" ID="ScriptManager1"
    </telerik:RadScriptManager> 
    <!-- content start --> 
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" /> 
    <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" 
        AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True" PageSize="5" 
        GridLines="None" ShowGroupPanel="true"
        <PagerStyle Mode="NumericPages"></PagerStyle> 
        <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="EmployeeID" AllowMultiColumnSorting="True"
            <NestedViewTemplate> 
                <telerik:RadGrid runat="server" ID="nestedgrid" ShowFooter="true" AllowSorting="true" 
                    EnableLinqExpressions="false"
                    <MasterTableView ShowHeader="true" AutoGenerateColumns="true" AllowPaging="true" 
                        PageSize="7"
                        <Columns> 
                            <telerik:GridEditCommandColumn> 
                            </telerik:GridEditCommandColumn> 
                        </Columns> 
                        <EditFormSettings EditFormType="Template"
                            <FormTemplate> 
                                                                
                                <asp:ImageButton ID=imgb runat=server CommandName="go"  /> 
                                <asp:Button ID=btn1 runat=server CommandName="buttonclicked" /> 
                                <asp:DropDownList id=ddl1  runat=server></asp:DropDownList> 
                            </FormTemplate> 
                        </EditFormSettings> 
                    </MasterTableView> 
                </telerik:RadGrid> 
            </NestedViewTemplate> 
            <Columns> 
                <telerik:GridBoundColumn SortExpression="FirstName" HeaderText="First Name" HeaderButtonType="TextButton" 
                    DataField="FirstName" UniqueName="FirstName"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="LastName" HeaderText="Last Name" HeaderButtonType="TextButton" 
                    DataField="LastName" UniqueName="LastName"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                    DataField="Title" UniqueName="Title"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="BirthDate" DataFormatString="{0:MM/dd/yyyy}" 
                    HeaderText="Birth Date" HeaderButtonType="TextButton" DataField="BirthDate" UniqueName="BirthDate"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="City" HeaderText="City" HeaderButtonType="TextButton" 
                    DataField="City" UniqueName="City"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn SortExpression="Country" HeaderText="Country" HeaderButtonType="TextButton" 
                    DataField="Country" UniqueName="Country"
                </telerik:GridBoundColumn> 
            </Columns> 
        </MasterTableView> 
        <ClientSettings AllowDragToGroup="true" /> 
    </telerik:RadGrid> 
    <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Employees" 
        runat="server"></asp:SqlDataSource> 
    <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        ProviderName="System.Data.SqlClient" runat="server"></asp:SqlDataSource> 
    <!-- content end --> 
    </form> 
</body> 
 


Imports Telerik.Web.UI 
Imports System 
Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Web.UI.WebControls 
Imports System.Configuration 
Partial Class _Default 
    Inherits System.Web.UI.Page 
 
 
    Dim ConnString As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString 
 
    Protected Sub nestedgrid_ItemCreated(ByVal sender As ObjectByVal e As GridItemEventArgs) 
        Dim btn As Button 
        Dim ddl As DropDownList 
        If e.Item.ItemType = GridItemType.EditFormItem And e.Item.IsInEditMode Then 
            Dim edititem As GridEditFormItem = CType(e.Item, GridEditFormItem) 
            ddl = CType(edititem.FindControl("ddl1"), DropDownList) 
            btn = CType(edititem.FindControl("btngo"), Button) 
 
            ddl.DataSource = dsddl() 
            ddl.DataTextField = "ShipName" 
            ddl.DataValueField = "OrderID" 
            ddl.DataBind() 
 
        End If 
    End Sub 
 
 
 
 
    Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated 
 
        If TypeOf e.Item Is GridNestedViewItem Then 
            Dim rg As RadGrid = e.Item.FindControl("NestedGrid"
            'Dim imgb As ImageButton = rg.MasterTableView.FindControl("imgb") 
            AddHandler rg.ItemCreated, AddressOf nestedgrid_ItemCreated 
            AddHandler rg.ItemCommand, AddressOf nestedgrid_itemCommand 
            AddHandler rg.NeedDataSource, AddressOf nestedgrid_NeedDataSource 
            rg.DataSource = myds() 
            rg.DataBind() 
        End If 
 
    End Sub 
 
     
 
    Public Function myds() As DataSet 
        Dim conn As SqlConnection = New SqlConnection(ConnString) 
        Dim adapter As SqlDataAdapter = New SqlDataAdapter 
        adapter.SelectCommand = New SqlCommand("SELECT TOP 10 OrderID, ShipName, ShipAddress FROM Orders", conn) 
        Dim myDataSet As DataSet = New DataSet 
 
        conn.Open() 
        Try 
            adapter.Fill(myDataSet, "Orders"
            adapter.SelectCommand = New SqlCommand("SELECT OrderID, Quantity FROM [Order Details]", conn) 
            adapter.Fill(myDataSet, "OrderDetails"
        Finally 
            conn.Close() 
        End Try 
        Return myDataSet 
 
    End Function 
 
 
    Public Function dsddl() As DataSet 
        Dim conn As SqlConnection = New SqlConnection(ConnString) 
        Dim adapter As SqlDataAdapter = New SqlDataAdapter 
        Dim myDataSet As DataSet = New DataSet 
 
        conn.Open() 
        Try 
 
            adapter.SelectCommand = New SqlCommand("SELECT TOP 10 OrderID, ShipName FROM Orders", conn) 
            adapter.Fill(myDataSet, "OrderDetails"
        Finally 
            conn.Close() 
        End Try 
        Return myDataSet 
 
    End Function 
 
 
    Protected Sub nestedgrid_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
        If e.CommandName = "Go" Then 
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem) 
            Dim editItem As GridEditFormItem = DirectCast(item.EditFormItem, GridEditFormItem) 
 
            Dim doptdown As DropDownList = DirectCast(editItem.FindControl("ddl1"), DropDownList) 
            Dim value As String = doptdown.SelectedItem.Text 
            Response.Write(value) 
        End If 
    End Sub 
    Protected Sub nestedgrid_NeedDataSource(ByVal source As ObjectByVal e As GridNeedDataSourceEventArgs) 
        Dim grid As RadGrid = DirectCast(source, RadGrid) 
        ' Populate the grid         
        grid.DataSource = myds().Tables(0) 'GetDataTable("SELECT * FROM Customers") 
    End Sub 
 
End Class 
 

0
Accepted
Pavlina
Telerik team
answered on 20 Jan 2010, 02:19 PM
Hello,

I recommend you take a look at the following online examples showing different approached for extracting values from a GridDropDownColumn:
Extracting values using Column Editors
Extracting values using Server-side API

I hope this helps.

Best wishes,
Pavlina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
dingen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
dingen
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or