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

[Solved] get access to label in DetailTables FormTemplate

4 Answers 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mike
Top achievements
Rank 1
mike asked on 09 Apr 2013, 03:05 PM
I've been searching everywhere and not found the right combination of what will fix my problem. I have a DetailTables inside a grid, It has the internal Add new record from the CommandItemDisplay and a GridEditCommandColumn. It is using an FormTemplate for Editing and in that formtemplate I have a Label which is filled with the VcnCd from the parent grid. My problem is that when I click on the edit button that label is filled correctly with the VcnCd, but when I click the Add new button that label is empty. I have spent a lot of hours trying to find how to fill that label in the code behind with no luck. the label I'm trying to fill with the correct value is lblvType. I could use some help I'm doing this in VB. Here is my grid.
 
<telerik:RadGrid ID="grdVcntn" runat="server" AutoGenerateColumns="false">
  <ClientSettings>
    <Scrolling AllowScroll="True" UseStaticHeaders="True" />
  </ClientSettings>
  <MasterTableView EditMode="PopUp" CommandItemDisplay="Top" ShowHeader="true" AutoGenerateColumns="false" DataKeyNames="VcnCd">
     <NoRecordsTemplate>
      There are no records for this person.
    </NoRecordsTemplate>
    <CommandItemTemplate>
      <asp:LinkButton ID="LinkButton1" runat="server" CommandName="InitInsert" >
        <img style="border:0px" alt="Add" src="../images/plus.gif" /> Add new Record
      </asp:LinkButton>
    </CommandItemTemplate>
    <Columns>
      <telerik:GridEditCommandColumn ButtonType="imageButton" EditImageUrl="../images/Edit.gif"></telerik:GridEditCommandColumn>
      <telerik:GridBoundColumn HeaderText="VcnCd" DataField="VcnCd" UniqueName="VcnCd"></telerik:GridBoundColumn>
      <telerik:GridBoundColumn HeaderText="VcnNm" DataField="VcnNm" UniqueName="VcnNm"></telerik:GridBoundColumn>
    </Columns>
    <EditFormSettings InsertCaption="Add record" CaptionDataField="VcnCd" CaptionFormatString="Edit record: {0}"
      EditFormType="Template" PopUpSettings-Modal="true" PopUpSettings-Height="260px" PopUpSettings-Width="500px">
      <FormTemplate>
        <table>
          <tr>
            <td>
              Code: <asp:DropDownList ID="dpdVcnCd" DataTextField="VcnNm" DataValueField="VcnCd" SelectedValue='<%# Bind("VcnCd") %>' 
              runat="server" DataSourceID="SqlDataSource1"></asp:DropDownList>
            </td>
          </tr>
          <tr>
            <td>
              <asp:Button ID="btnSubmitVcnCd" Text='<%# Iif(typeof Container is GridEditFormInsertItem, "Insert", "Update") %>'
                runat="server" CommandName='<%# Iif(TypeOf Container is GridEditFormInsertItem, "PerformInsert", "Update") %>'
                CommandArgument='<%# Eval("VcnCd") %>' />
              <asp:Button ID="btnCancelVcnCd" Text="Cancel" runat="server" CausesValidation="false" CommandName="Cancel" />
            </td>
          </tr>
        </table>
      </FormTemplate>
    </EditFormSettings>
    <DetailTables>
      <telerik:GridTableView Name="DetailTables" EditMode="PopUp" DataKeyNames="ID" DataSourceID="SqlDataSource3"
        Width="100%" runat="server" CommandItemDisplay="Top">
        <ParentTableRelation>
          <telerik:GridRelationFields DetailKeyField="VcnCd" MasterKeyField="VcnCd" />
        </ParentTableRelation>
        <Columns>
          <telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="../images/Edit.gif" UniqueName="Edit"></telerik:GridEditCommandColumn>
          <telerik:GridBoundColumn HeaderText="ID" DataField="ID" UniqueName="ID"></telerik:GridBoundColumn>
          <telerik:GridBoundColumn HeaderText="VcnCd" DataField="VcnCd" UniqueName="VcnCd"></telerik:GridBoundColumn>
          <telerik:GridBoundColumn HeaderText="Date" DataField="VcntnDt" UniqueName="VcntnDt" DataFormatString="{0:d}"></telerik:GridBoundColumn>
        </Columns>
        <EditFormSettings InsertCaption="Edit Item" CaptionFormatString="Edit ID: {0}" CaptionDataField="ID" EditFormType="Template"
          PopUpSettings-Modal="true" PopUpSettings-Height="260px" PopUpSettings-Width="400px">
          <PopUpSettings Modal="true" />
          <EditColumn UniqueName="EditCommandColumn1" ButtonType="ImageButton" InsertImageUrl="../images/plus.gif" EditImageUrl="../images/Edit.gif"></EditColumn>
          <FormTemplate>
            <table id="tblEditVcnDte">
              <tr>
                <td colspan="2" align="left">Vaccination Type: <asp:Label ID="lblvType" Text='<%# Bind("VcnCd") %>' runat="server"></asp:Label></td>
              </tr>
              <tr>
                <td align="right">Date :</td>
                <td>
                  <asp:TextBox ID="txtVcnDte" runat="server" Text='<%# Bind("VcntnDt","{0:d}") %>'></asp:TextBox>
                </td>
              </tr>
            </table>
            <table style="width: 100%">
              <tr>
                <td align="right" colspan="2">
                  <asp:Button ID="btnSubmitVcn" Text='<%# Iif(typeof Container is GridEditFormInsertItem, "Insert", "Update") %>'
                    runat="server" CommandName='<%# Iif(TypeOf Container is GridEditFormInsertItem, "InsertCdDt", "UpdateCdDt") %>'
                    CommandArgument='<%# Eval("ID") %>'  />
                  <asp:Button ID="btnCancelVcn" Text="Cancel" runat="server" CausesValidation="false" CommandName="Cancel" />
                </td>
              </tr>
            </table>
          </FormTemplate>
        </EditFormSettings>
      </telerik:GridTableView>
    </DetailTables>
  </MasterTableView>
  <ClientSettings>
    <ClientEvents OnPopUpShowing="PopUpShowing" />
    <Selecting AllowRowSelect="true" />
  </ClientSettings>
 
</telerik:RadGrid>

4 Answers, 1 is accepted

Sort by
0
mike
Top achievements
Rank 1
answered on 09 Apr 2013, 04:54 PM
Well I finally got it working with the code below.
Private Sub grdVcntn_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles grdVcntn.ItemDataBound
    If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
      If (e.Item.OwnerTableView.DataSourceID = "SqlDataSource3") Then
        Dim parentItem As GridDataItem = e.Item.OwnerTableView.ParentItem
        Dim VcnCd As String = CType(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("VcnCd"), String)
        Dim EditFormItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
        Dim lbl As Label = DirectCast(EditFormItem.FindControl("lblvType"), Label)
        lbl.Text = VcnCd
      End If
    End If
  End Sub
0
mike
Top achievements
Rank 1
answered on 09 Apr 2013, 09:49 PM
New Problem now, on trying to rebind to close the popup edit window. I now get an error of
{"There was a problem extracting DataKeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource."}. I don't understand why I am getting this error when I am editing and falling in the commandname UpdateCdDt but I do not get it when I am inserting a record and falling under commandname InsertCdDt.
Private Sub grdVcntn_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles grdVcntn.ItemCommand
   'you click the insert button on the sub grid for the VcnCd
  If e.CommandName = "InsertCdDt" Then
    e.Canceled = True
    'Clicking the insert button
    Dim parentItem As GridDataItem = e.Item.OwnerTableView.ParentItem
    If (Not parentItem Is Nothing) Then
      'Parent item primary field value is: " & VcnCd1
      Dim VcnCd As String = CType(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("VcnCd"), String)
      Dim editItem As GridEditFormInsertItem = CType(e.Item, GridEditFormItem)
      Dim VcntnDt As String = CType(editItem.FindControl("txtVcnDte"), TextBox).Text()
      Call InsertVcnCdDt(VcnCd, VcntnDt)
      'lblMsg.Text = " ItemCommand InsertCdDt Date= " & VcntnDt & " VcnCd= " & VcnCd
      'This will make the popup close
      grdVcntn.MasterTableView.IsItemInserted = False
      grdVcntn.MasterTableView.Rebind()
    End If
  End If
 
  If e.CommandName = "UpdateCdDt" Then
     e.Canceled = True
    'this is the ID of the record editing
    Dim ID As String = e.Item.OwnerTableView.Items(e.Item.ItemIndex)("ID").Text
    Dim parentItem As GridDataItem = e.Item.OwnerTableView.ParentItem
    If (Not parentItem Is Nothing) Then
      'Parent item primary field value is: " & VcnCd
      Dim VcnCd As String = CType(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("VcnCd"), String)
      'Dim editItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
      Dim edititem As GridEditFormItem = DirectCast(TryCast(e.Item, GridDataItem).EditFormItem, GridEditFormItem)
      Dim VcntnDt As String = CType(edititem.FindControl("txtVcnDte"), TextBox).Text()
      Call UpdateVcnCdDt(ID, VcntnDt, VcnCd)
      'lblMsg.Text = " ItemCommand UpdateCdDt ID = " & ID & " Date= " & VcntnDt & " ID= " & VcnCd
      'This will make the popup close
      grdVcntn.MasterTableView.IsItemInserted = False
      grdVcntn.MasterTableView.Rebind()
    End If
  End If
End Sub
0
Vasil
Telerik team
answered on 12 Apr 2013, 01:51 PM
Hi Mike,

The DataKeyName is used for updating, because in the update statement of the query that grid creates it uses the key name in the Whare clause.
In the insert, this is not used, and even if it is wrong, you will not get exception.
Please check your DataKeyNames and correct them to match existing field in your datasource.

All the best,
Vasil
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
mike
Top achievements
Rank 1
answered on 12 Apr 2013, 01:59 PM
that was the problem, on the detailTable I had set a different DataKeyName than what the main grid was, now they are all the same and that is working now, but ran into another problem I couldn't solve and submitted another issue.
Tags
Grid
Asked by
mike
Top achievements
Rank 1
Answers by
mike
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or