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

Passing the DataKeyValue to a link button in the CommandItemTemplate

2 Answers 198 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 2
Veteran
Jon asked on 31 Mar 2011, 08:44 PM
I have a grid with three nested levels

the tables are as follows:

Forms
Form Sections
Form Items

You can have many sections within a form and many items within a form section. I am using a radwindow to edit and add items from this grid. In the command Item Template I have a Link buttons that allow me to enter new records, but i am having trouble getting the datakeyvalue when it comes to the add new buttons.

Heres my grid code:

<telerik:RadGrid ID="RadGrid1" DataSourceID="DSForms" runat="server" ShowStatusBar="true" AutoGenerateColumns="False" PageSize="30" AllowSorting="True" AllowMultiRowSelection="False"
            AllowPaging="True" GridLines="None" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" OnDeleteCommand="RadGrid1_DeleteCommand">           
            <PagerStyle Mode="NumericPages"></PagerStyle>
            <MasterTableView DataSourceID="DSForms" DataKeyNames="Form_ID" AllowMultiColumnSorting="True" Width="100%" CommandItemDisplay="Top"  CommandItemSettings-RefreshImageUrl="~/images/refresh.gif" CommandItemSettings-RefreshText="Refresh" CommandItemSettings-AddNewRecordText="Add Form" Name="Forms">
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="FormSection_ID, Form_ID" DataSourceID="DSFormSections" Width="100%" runat="server" CommandItemDisplay="Top"  Name="FormItems">
                        <CommandItemTemplate>
                            <asp:HyperLink ID="AddSectionLink" runat="server" Text="Add Section"></asp:HyperLink>
                        </CommandItemTemplate>
                        <ParentTableRelation>
                            <telerik:GridRelationFields DetailKeyField="Form_ID" MasterKeyField="Form_ID" />
                        </ParentTableRelation>
                        <DetailTables>
                            <telerik:GridTableView DataKeyNames="FormSection_ID,FormItem_ID" DataSourceID="DSFormItems" CommandItemDisplay="Top" Width="100%" runat="server">
                                <ParentTableRelation>
                                    <telerik:GridRelationFields DetailKeyField="FormSection_ID" MasterKeyField="FormSection_ID" />
                                </ParentTableRelation>
                                <AlternatingItemStyle BackColor="LightCyan" />
                                <CommandItemTemplate>
                                    <asp:HyperLink ID="AddFormItemLink" runat="server" Text="Add Item"></asp:HyperLink>
                                </CommandItemTemplate>
                                <Columns>
                                    <telerik:GridBoundColumn SortExpression="FormItem_Question" HeaderText="Question" HeaderButtonType="TextButton"
                                        DataField="FormItem_Question" UniqueName="FormItem_Question">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn SortExpression="FormItem_QuestionValues" HeaderText="FormItem_QuestionValues" HeaderButtonType="TextButton"
                                        DataField="FormItem_QuestionValues" Visible="false" UniqueName="FormItem_QuestionValues">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn SortExpression="FormItem_Type" HeaderText="FormItem_Type" HeaderButtonType="TextButton"
                                        DataField="FormItem_Type" Visible="false" UniqueName="FormItem_Type">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn SortExpression="FormItem_Grid" HeaderText="FormItem_Grid" HeaderButtonType="TextButton"
                                        DataField="FormItem_Grid" Visible="false" UniqueName="FormItem_Grid">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridButtonColumn ConfirmText="Delete this Form Item?" ButtonType="ImageButton"
                                        CommandName="Delete" Text="Delete" UniqueName="DeleteColumn2">
                                        <HeaderStyle Width="20px" />
                                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                                    </telerik:GridButtonColumn>
                                    <telerik:GridTemplateColumn UniqueName="TemplateEditColumn" ItemStyle-Width="20px">
                                        <ItemTemplate>
                                            <asp:HyperLink ID="EditItemLink" runat="server"><img src="/images/edit.gif" alt="Edit" border="0" /></asp:HyperLink>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                </Columns>
                                <SortExpressions>
                                    <telerik:GridSortExpression FieldName="FormItem_Sort" SortOrder="Descending"></telerik:GridSortExpression>
                                </SortExpressions>
                            </telerik:GridTableView>
                        </DetailTables>
                        <AlternatingItemStyle BackColor="AliceBlue" />
                        <Columns>
                            <telerik:GridBoundColumn SortExpression="FormSection_ID" HeaderText="ID" HeaderButtonType="TextButton"
                                DataField="FormSection_ID" UniqueName="FormSection_ID" ReadOnly="true" Visible="false">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="FormSection_Title" ReadOnly="true" HeaderText="Section" Visible="true"  HeaderButtonType="TextButton"
                                DataField="FormSection_Title" UniqueName="FormSection_Title">
                            </telerik:GridBoundColumn>
                            <telerik:GridButtonColumn ConfirmText="Delete this Form Section Item?" ButtonType="ImageButton"
                                CommandName="Delete" Text="Delete" UniqueName="DeleteColumn2">
                                <HeaderStyle Width="20px" />
                                <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                            </telerik:GridButtonColumn>
                            <telerik:GridTemplateColumn UniqueName="TemplateEditColumn" ItemStyle-Width="20px">
                                <ItemTemplate>                                   
                                    <asp:HyperLink ID="EditSectionLink" runat="server"><img src="/images/edit.gif" alt="Edit" border="0" /></asp:HyperLink>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
 
                        </Columns>
                        <SortExpressions>
                            <telerik:GridSortExpression FieldName="FormSection_Sort"></telerik:GridSortExpression>
                        </SortExpressions>
                        <EditFormSettings EditFormType="Template">
                        <FormTemplate>
                        <table id="tblitems" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                            style="border-collapse: collapse; background: white;">
                            <tr>
                                <td>
                                    <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
                                        <tr><td colspan="2"></td></tr>
 
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
                                        runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'>
                                    </asp:Button> 
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
                                </td>
                            </tr>
                        </table>                       
                        </FormTemplate>
                        </EditFormSettings>
                    </telerik:GridTableView>                   
                </DetailTables>
                <Columns>               
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"><HeaderStyle Width="20px" /></telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn SortExpression="Form_ID" HeaderText="Form_ID" ItemStyle-Width="30px" HeaderButtonType="TextButton" DataField="Form_ID" UniqueName="Form_ID" Visible="false" ReadOnly="true" />
                    <telerik:GridBoundColumn SortExpression="Form_Title" HeaderText="Form" ItemStyle-Width="300px" HeaderButtonType="TextButton" DataField="Form_Title" UniqueName="Form_Title" />
                    <telerik:GridButtonColumn ConfirmText="Delete this Form?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"><HeaderStyle Width="20px" /><ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /></telerik:GridButtonColumn>
                </Columns>
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="Form_Title" SortOrder="Descending"></telerik:GridSortExpression>
                </SortExpressions>
                <EditFormSettings EditFormType="Template">               
                    <FormTemplate>
                        <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none" style="border-collapse: collapse; background: white;">
                            <tr>
                                <td>
                                    <table id="Table3" cellspacing="1" cellpadding="1" border="0">
                                        <tr><td colspan="2"></td></tr>
                                        <tr>
                                            <td>Form Name: </td>
                                            <td><asp:TextBox ID="TextBox7" Width="350px" runat="server" Text='<%# Bind( "Form_Title" ) %>'></asp:TextBox></td>
                                        </tr>
                                        <tr><td colspan="2"></td></tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
                                        runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'>
                                    </asp:Button> 
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
                                </td>
                            </tr>
                        </table>
                    </FormTemplate>
                </EditFormSettings>
 
            </MasterTableView>
        </telerik:RadGrid>


And here is some of my code behind where I am trying to assign the rad window code to the buttons, the edit code works fine just not the add new buttons:

Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
 
    If e.Item.OwnerTableView.DataSourceID = "DSFormSections" Then
        If TypeOf e.Item Is GridDataItem Then
            Dim EditSectionLink As HyperLink = DirectCast(e.Item.FindControl("EditSectionLink"), HyperLink)
            EditSectionLink.Attributes("href") = "#"
            EditSectionLink.Attributes("onclick") = [String].Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("FormSection_ID"), e.Item.ItemIndex)
        End If
    End If
 
    If e.Item.OwnerTableView.DataSourceID = "DSFormItems" Then
        If TypeOf e.Item Is GridDataItem Then
            Dim EditItemLink As HyperLink = DirectCast(e.Item.FindControl("EditItemLink"), HyperLink)
            EditItemLink.Attributes("href") = "#"
            EditItemLink.Attributes("onclick") = [String].Format("return ShowEditItemForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("FormItem_ID"), e.Item.ItemIndex)
        End If
    End If
 
 
    If e.Item.OwnerTableView.DataSourceID = "DSFormSections" Then
        If TypeOf e.Item Is GridCommandItem Then
            Dim AddSectionLink As HyperLink = DirectCast(e.Item.FindControl("AddSectionLink"), HyperLink)
            AddSectionLink.Attributes("href") = "#"
            AddSectionLink.Attributes("onclick") = "return ShowInsertForm();"
        End If
    End If
 
    If e.Item.OwnerTableView.DataSourceID = "DSFormItems" Then
        If TypeOf e.Item Is GridCommandItem Then
            Dim AddFormItemLink As HyperLink = DirectCast(e.Item.FindControl("AddFormItemLink"), HyperLink)
            AddFormItemLink.Attributes("href") = "#"
            AddFormItemLink.Attributes("onclick") = "return ShowInsertItemForm();"
        End If
    End If
 
End Sub


Any ideas what i am doing wrong in the RadGrid1_ItemCreated as the e.item.itemindex is -1 for the add new items.

2 Answers, 1 is accepted

Sort by
0
Jon
Top achievements
Rank 2
Veteran
answered on 04 Apr 2011, 03:24 PM
anyone?
0
Iana Tsolova
Telerik team
answered on 06 Apr 2011, 09:12 AM
Hi Jon,

I assume that the problematic code is this:
If e.Item.OwnerTableView.DataSourceID = "DSFormItems" Then
    If TypeOf e.Item Is GridCommandItem Then
        Dim AddFormItemLink As HyperLink = DirectCast(e.Item.FindControl("AddFormItemLink"), HyperLink)
        AddFormItemLink.Attributes("href") = "#"
        AddFormItemLink.Attributes("onclick") = "return ShowInsertItemForm();"
    End If
End If

And here you what to get the data key value of the parent item, right? You can try modifying the code as below:
If e.Item.OwnerTableView.DataSourceID = "DSFormItems" Then
    If TypeOf e.Item Is GridCommandItem Then
        Dim AddFormItemLink As HyperLink = DirectCast(e.Item.FindControl("AddFormItemLink"), HyperLink)
        AddFormItemLink.Attributes("href") = "#"
        AddFormItemLink.Attributes("onclick") = "return ShowInsertItemForm();"
        Dim parentItem as GridDataItem = e.Item.OwnerTableView.ParentItem
        dim keyValue as String = parentItem.GetDataKeyValue("key_field").ToString()
    End If
End If

For more information on accessing grid rows, find the below article:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

Best wishes,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Jon
Top achievements
Rank 2
Veteran
Answers by
Jon
Top achievements
Rank 2
Veteran
Iana Tsolova
Telerik team
Share this question
or