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

Referencing a control in a template

4 Answers 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 28 Nov 2011, 12:09 PM
Hey,

I'm trying to dynamically create various levels of detail tables for a project I'm working on. I have almost everything working now (in terms of display anyway) thanks to help from this forum, but there's just one more problem. I'm trying to reference the controls in various edit/insert sections but I've hit something of a problem.

At the top level I've added a hardcoded template in the front-end code, like so:
<EditFormSettings EditFormType="Template">
  <EditColumn UniqueName="EditCommandColumn1">
  </EditColumn>
  <FormTemplate>
    <asp:Panel ID="pnlZForm" runat="server">
      <div id="divZEdit">
        <asp:Label ID="lblMerchantCode" Width="150px" runat="server" Text="Merchant Code:" />
        <telerik:RadTextBox ID="rtxtMerchantCode" Runat="server" Width="150px" />
        <br />
        <asp:Label ID="lblAccountName" Width="150px" runat="server" Text="Account Name:" />
        <telerik:RadTextBox ID="rtxtAccountName" Runat="server" Width="150px" />
        <br />
        <asp:Label ID="lblSharedSecret" Width="150px" runat="server" Text="Shared Secret:" />
        <telerik:RadTextBox ID="rtxtSharedSecret" Runat="server" Width="150px" />
        <br />
        <telerik:RadButton ID="btnUpdate" runat="server" Text="Update" />
        <telerik:RadButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
      </div>
    </asp:Panel>
  </FormTemplate>
</EditFormSettings>

Now I can access these controls (or could on Friday when I was trying to get it to work) in the code by trying to access them in the ItemDataBound. However when I tried to create a template, use that for the form instead in the code and then access control from there it no longer seems to work. The code I'm using to assign the template is as follows (cut down for convenience to just the relevant code):
Private Sub rgdNewRadGrid_DetailTableDataBind(ByVal source As Object, ByVal e As GridDetailTableDataBindEventArgs) Handles rgdNewRadGrid.DetailTableDataBind
  Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
  
  Select Case e.DetailTableView.Name
    Case "myDetailTable"
      Dim myTrackingGUID As Guid = New Guid(dataItem.Item("MerchantTrackingGUID").Text)
      Dim myDataSet As New DataSet
  
      '===Get the DetailTable data===
      myDataSet = *get dataset from database*
  
      e.DetailTableView.DataSource = myDataSet
      e.DetailTableView.CommandItemDisplay = GridCommandItemDisplay.Top
      e.DetailTableView.CommandItemSettings.AddNewRecordImageUrl = "../Img/AddRecord.gif"
      e.DetailTableView.CommandItemSettings.AddNewRecordText = "Add New Details"
      e.DetailTableView.CommandItemSettings.ShowRefreshButton = False
      e.DetailTableView.EditFormSettings.EditFormType = GridEditFormType.Template
      e.DetailTableView.EditFormSettings.FormTemplate = LoadTemplate("Templates/myNewAdd.ascx")
  End Select
End Sub

The code for myNewAdd.ascx is just simply as follows, and it DISPLAYS absolutely fine on the form.
<%@ Control Language="VB" ClassName="myNewAdd" %>
<asp:Panel ID="pnlNewForm" runat="server">
  <div id="divNewEdit">
    <asp:Label ID="lblTerminalUserGroupID" Width="150px" runat="server" Text="TerminalUserGroupID:" />
    <telerik:RadComboBox ID="rcbxTerminalUserGroupID" Width="200px" runat="server" />
    <br />
    <telerik:RadButton ID="btnDetailUpdate" runat="server" Text="Update" />
    <telerik:RadButton ID="btnDetailCancel" runat="server" Text="Cancel" CommandName="Cancel" />
  </div>
</asp:Panel>

I'm aware the update button doesn't do anything at the moment, but at the moment I haven't gotten that far quite frankly. I'm more concerned with getting this code to work. So, I set that template as the template and it works, but when I try to access the control it doesn't work. I'm trying to access it in the ItemDataBound. Maybe I'm trying to access things in the wrong order and it's already run ItemDataBound before it's bound the template or something, in which case where do I bind the template? But here's the code I'm trying to use:
Protected Sub rgdNewRadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgdNewRadGrid.ItemDataBound
  If e.Item.IsInEditMode AndAlso e.Item.OwnerTableView.Name = "myDetailTable" Then
    Dim editItem As GridEditFormItem = e.Item
    Dim rcbxTerminalUserGroupID As RadComboBox = editItem.FindControl("rcbxTerminalUserGroupID")
    Dim myDataSet As Data.DataSet
  
    myDataSet = *get data from database*
  
    For Each row As DataRow In myDataSet.Tables(0).Rows
      Dim myItem As New RadComboBoxItem
      myItem.Text = row.Item("NAME").ToString
      If Not rcbxTerminalUserGroupID Is Nothing Then
        rcbxTerminalUserGroupID.Items.Add(myItem)
      End If
    Next
  End If
End Sub

The reason I have "If Not rcbxTerminalUserGroupID Is Nothing Then" is because I was continuously getting a NullReferenceException because evidently it couldn't find the control, even though the control blatantly displays on the page. Like I said above, I'm going to assume this is because of the way that the RadGrids run the procedures possibly, or something else related to the template, but it's immensely annoying and I really want to get this working.

If anyone can help I'll be massively grateful.

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 Nov 2011, 12:24 PM
Hello Pete,

Try the following code snippet.
VB:
Protected Sub RadGrid2_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
        Dim item As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
        Dim userControl As UserControl = DirectCast(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
        Dim combo As RadComboBox = DirectCast(userControl.FindControl("RadComboBox1"), RadComboBox)
    End If
End Sub

-Shinu.
0
Pete
Top achievements
Rank 1
answered on 28 Nov 2011, 01:36 PM
Hey Shinu,

Thanks for the reply. I'm confused by the second line though.

Dim userControl As UserControl = DirectCast(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)

I'm assuming I'm meant to change something to be specific to the template? I just don't know what, but running it like that I also get a NullReferenceException.

Sorry for possibly being an idiot.
0
Shinu
Top achievements
Rank 2
answered on 29 Nov 2011, 04:47 AM
Hello Pete,

If you want to access the controls in ascx(UserControl)page, try accessing the controls as shown in above code. Also check the following demo which explains the same.
Grid / User Control Edit Form

-Shinu.
0
Pete
Top achievements
Rank 1
answered on 30 Nov 2011, 01:07 PM
Hi again,

The problem was the way I was binding the template. After investigating the example you gave me I changed the EditFormSettings.FormTemplate = LoadTemplate method to the EditFormSettings.UserControlName method and it worked.

Thanks for your help.
Tags
Grid
Asked by
Pete
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Pete
Top achievements
Rank 1
Share this question
or