在rowdrop事件telerik上访问详细信息表datakeynames

时间:2014-04-07 04:16:15

标签: c#-4.0 telerik telerik-grid radgrid

我正在使用telerik嵌套的rad网格。请看一下代码。

<telerik:RadGrid ID="radGridRoutes" runat="server" AutoGenerateColumns="false" ShowStatusBar="True"
        AllowSorting="True" AllowPaging="True" ClientSettings-Scrolling-EnableVirtualScrollPaging="false"
        ClientSettings-Scrolling-AllowScroll="false"
        OnNeedDataSource="Routes_NeedDataSource" OnDetailTableDataBind="radGridRoutes_DetailTableDataBind" AllowFilteringByColumn="True"
        EnableLinqExpressions="false" OnInsertCommand="Routes_InsertCommand"
        OnUpdateCommand="radGridRoutes_UpdateCommand"
        OnItemCommand="radGridRoutes_ItemCommand"
        OnPreRender="radGridRoutes_PreRender" ClientSettings-AllowRowsDragDrop="true"
        OnRowDrop="radGridRoutes_RowDrop">
        <ClientSettings AllowDragToGroup="True">
            <Selecting AllowRowSelect="True" />
        </ClientSettings>
        <GroupingSettings CaseSensitive="false" />
        <SelectedItemStyle CssClass="SelectedItem" />
        <PagerStyle Mode="NextPrevNumericAndAdvanced"></PagerStyle>
        <ValidationSettings CommandsToValidate="PerformInsert,Update" EnableModelValidation="true"
            EnableValidation="true" />
        <FilterItemStyle BackColor="#F8F8F8" HorizontalAlign="Left" />
        <FilterMenu EnableImageSprites="false">
        </FilterMenu>
        <MasterTableView DataKeyNames="ID" AllowMultiColumnSorting="false"
            Width="100%" CommandItemDisplay="Top" Name="Routes">
            <AlternatingItemStyle BackColor="WhiteSmoke" />

            <DetailTables>
                <telerik:GridTableView OnRowDrop="radGridRoutes_RowDrop" ClientSettings-AllowRowsDragDrop="true" 
                    DataKeyNames="ID,DisplayOrder,RouteID" Width="100%" runat="server"
                    CommandItemDisplay="Top" Name="Stops" AllowFilteringByColumn="false">
                    <CommandItemSettings AddNewRecordText="Add New Stops" ShowAddNewRecordButton="true" />
                    <ParentTableRelation>
                        <%-- <telerik:GridRelationFields DetailKeyField="ID" MasterKeyField="ID" />--%>
                    </ParentTableRelation>
                    <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    </ExpandCollapseColumn>
                    <Columns>
                        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditTask" EditText="Edit"
                            UpdateText="Update" CancelText="Cancel">
                            <HeaderStyle Width="20px" />
                        </telerik:GridEditCommandColumn>

                        <telerik:GridTemplateColumn SortExpression="Name" HeaderText="Name" UniqueName="Name"
                            HeaderButtonType="TextButton" HeaderStyle-Width="15%" ItemStyle-Width="15%" DataField="tName">
                            <ItemTemplate>
                                <%# Eval("Name")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtName" runat="server" Width="250px" Text='<%# Eval("Name")%>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="requiredName" ControlToValidate="txtName"
                                    CssClass="validationMessage" ErrorMessage="Name is required."
                                    Text="* required" runat="server" Display="Dynamic"></asp:RequiredFieldValidator>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn SortExpression="Comment" HeaderText="Comment" UniqueName="Comment"
                            HeaderButtonType="TextButton" DataField="Comment">
                            <ItemTemplate>
                                <%# Eval("Comment")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtComment" TextMode="MultiLine" Rows="4" Width="250px" runat="server" Text='<%# Eval("Comment")%>'></asp:TextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="Address" HeaderText="Address"
                            DataField="Address" UniqueName="Address" HeaderButtonType="TextButton">
                            <ItemTemplate>
                                <%# Eval("Address")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtAddress" Width="250px" TextMode="MultiLine" Text='<%# Bind("Address") %>' runat="server"></asp:TextBox>

                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="Phone" HeaderText="Phone" UniqueName="Phone"
                            HeaderButtonType="TextButton" DataField="Phone">
                            <ItemTemplate>
                                <%# Eval("Phone")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtPhone" Rows="4" Width="250px" runat="server" Text='<%# Eval("Phone")%>'></asp:TextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="Email" HeaderText="Email" UniqueName="Email"
                            HeaderButtonType="TextButton" DataField="Email">
                            <ItemTemplate>
                                <%# Eval("Email")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtEmail" Rows="4" Width="250px" runat="server" Text='<%# Eval("Email")%>'></asp:TextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="ManagerName" HeaderText="ManagerName" UniqueName="ManagerName"
                            HeaderButtonType="TextButton" DataField="ManagerName">
                            <ItemTemplate>
                                <%# Eval("ManagerName")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtManagerName" Rows="4" Width="250px" runat="server" Text='<%# Eval("ManagerName")%>'></asp:TextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="NumberOfRooms" HeaderText="#Rooms" UniqueName="NumberOfRooms"
                            HeaderButtonType="TextButton" DataField="NumberOfRooms">
                            <ItemTemplate>
                                <%# Eval("NumberOfRooms")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtNumberOfRooms" Width="250px" Rows="4" runat="server" Text='<%# Eval("NumberOfRooms")%>'></asp:TextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="Merchandising" HeaderText="Merchandising" UniqueName="Merchandising"
                            HeaderButtonType="TextButton" DataField="Merchandising">
                            <ItemTemplate>
                                <%# Eval("Merchandising")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtMerchandising" Width="250px" Rows="4" runat="server" Text='<%# Eval("Merchandising")%>'></asp:TextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="Frequency" HeaderText="Frequency"
                            DataField="Frequency" UniqueName="Frequency" HeaderButtonType="TextButton">
                            <ItemTemplate>
                                <%# GetFrequencyName(Convert.ToInt32(Eval("FrequencyID"))) %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="drpFreq" runat="server" DataTextField="Name" DataValueField="ID"
                                    AppendDataBoundItems="true">
                                    <asp:ListItem Text="--Select--" Value="-1">
                                    </asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn SortExpression="DisplayOrder" HeaderText="Display Order" UniqueName="DisplayOrder"
                            HeaderButtonType="TextButton" HeaderStyle-Width="15%" ItemStyle-Width="15%" DataField="DisplayOrder">
                            <ItemTemplate>
                                <%# Eval("DisplayOrder")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtDisplayOrder" runat="server" Width="250px" Text='<%# Eval("DisplayOrder")%>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="requiredDisplayOrder" ControlToValidate="txtDisplayOrder"
                                    CssClass="validationMessage" ErrorMessage="Display Order is required."
                                    Text="* required" runat="server" Display="Dynamic"></asp:RequiredFieldValidator>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>

                        <telerik:GridTemplateColumn HeaderText="Delete" AllowFiltering="false">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkDelete" CommandName="DeleteStops" CommandArgument=' <%# Eval("ID") %>' runat="server" OnClientClick="javascript:if (!confirm('Are you sure you want to delete Stop?')) {
                                         return false;
                                     }">Delete</asp:LinkButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>

                    </Columns>
                    <EditFormSettings>
                        <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                        </EditColumn>
                    </EditFormSettings>
                </telerik:GridTableView>
            </DetailTables>
            <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
            </RowIndicatorColumn>
            <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" CancelText="Cancel" EditText="Edit"
                    UpdateText="Update" UniqueName="EditRoutes" ItemStyle-Width="5%">
                    <HeaderStyle Width="5%" />
                </telerik:GridEditCommandColumn>
                <telerik:GridTemplateColumn SortExpression="Name" HeaderText="Name" UniqueName="RouteName"
                    HeaderButtonType="TextButton" HeaderStyle-Width="20%" ItemStyle-Width="20%" DataField="Name">
                    <ItemTemplate>
                        <%# Eval("Name")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtRouteName" runat="server" Text='<%# Eval("Name")%>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="requiredRouteName" ControlToValidate="txtRouteName"
                            CssClass="validationMessage" ErrorMessage="Name is required."
                            Text="* required" runat="server" Display="Dynamic"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn SortExpression="Comment" HeaderText="Comment" UniqueName="Comment"
                    HeaderButtonType="TextButton" DataField="Comment">
                    <ItemTemplate>
                        <%# Eval("Comment")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtComment" TextMode="MultiLine" Rows="4" Width="250px" runat="server" Text='<%# Eval("Comment")%>'></asp:TextBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>

                <telerik:GridTemplateColumn HeaderText="Delete" AllowFiltering="false">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkDelete" CommandName="Delete" CommandArgument=' <%# Eval("ID") %>' runat="server" OnClientClick="javascript:if (!confirm('Are you sure you want to delete route?')) {
                                         return false;
                                     }">Delete</asp:LinkButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
            <SortExpressions>
                <telerik:GridSortExpression FieldName="Name"></telerik:GridSortExpression>
            </SortExpressions>
            <CommandItemSettings AddNewRecordText="Add New Route" ShowAddNewRecordButton="true" />
            <EditFormSettings>
                <EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

rowDrop事件背后的代码是

protected void radGridRoutes_RowDrop(object sender, GridDragDropEventArgs e)
    {
        GridDataItem RowDragged = e.DraggedItems.FirstOrDefault();
        e.DraggedItems[0].GetDataKeyValue("DisplayOrder");
        int OriginalID = Convert.ToInt32(RowDragged.GetDataKeyValue("ID"));
        int OriginalOrder = Convert.ToInt32(RowDragged.GetDataKeyValue("DisplayOrder"));
        int RouteID = Convert.ToInt32(RowDragged.GetDataKeyValue("RouteID"));

        int DestID = Convert.ToInt32(e.DestDataItem.GetDataKeyValue("ID"));
        int DestOrder = Convert.ToInt32(e.DestDataItem.GetDataKeyValue("DisplayOrder"));
        UpdateOrder(OriginalID, OriginalOrder, DestID, RouteID);
    }

现在问题是我想从嵌套的gridTableView访问datakeyNames。我无法理解这一点。请提供有关如何从rowdrop事件中的嵌套gridtableview获取ID,DisplayOrder,RouteID的建议。目前它给我的是空值。

1 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。

<强> ASPX

<telerik:GridTableView Name="Child" OnRowDrop="radGridRoutes_RowDrop" ClientSettings-AllowRowsDragDrop="true" DataKeyNames="ID,DisplayOrder,RouteID" Width="100%" runat="server" CommandItemDisplay="Top" Name="Stops" AllowFilteringByColumn="false">

<强> ASPX.CS

protected void radGridRoutes_RowDrop(object sender, GridDragDropEventArgs e)
{
        GridDataItem RowDragged = e.DraggedItems.FirstOrDefault();

    if (RowDragged.OwnerTableView.Name == "Child")
    {
        // Row Dragged from child Grid

            e.DraggedItems[0].GetDataKeyValue("DisplayOrder");
            int OriginalID = Convert.ToInt32(RowDragged.GetDataKeyValue("ID"));
            int OriginalOrder = Convert.ToInt32(RowDragged.GetDataKeyValue("DisplayOrder"));
            int RouteID = Convert.ToInt32(RowDragged.GetDataKeyValue("RouteID"));

             // You can also check same thing for Drop Item
            int DestID = Convert.ToInt32(e.DestDataItem.GetDataKeyValue("ID"));                 int DestOrder = Convert.ToInt32(e.DestDataItem.GetDataKeyValue("DisplayOrder"));
            UpdateOrder(OriginalID, OriginalOrder, DestID, RouteID);
    }
}

更新1:

如果上述代码无效,请尝试使用以下代码段。

方法1:

<强> ASPX

<telerik:GridTableView Name="Child" ClientDataKeyNames="ID,DisplayOrder,RouteID" OnRowDrop="radGridRoutes_RowDrop" ClientSettings-AllowRowsDragDrop="true"  DataKeyNames="ID,DisplayOrder,RouteID" Width="100%" runat="server" CommandItemDisplay="Top" Name="Stops" AllowFilteringByColumn="false">
......
......
<ClientSettings>
    <ClientEvents OnRowDragStarted="RowDragStarted" />
</ClientSettings>

JS代码

    function RowDragStarted(sender, eventArgs) {
        var tblvew = eventArgs.get_tableView().get_name();
        if(tblvew == null || tblvew == undefined || tblvew != 'Child')
        {
            eventArgs.set_cancel(true);
        }
    }
OR

function RowDragStarted(sender, eventArgs) {
    var route = eventArgs.getDataKeyValue("RouteID");
    if(route == null || route  == '')
    {
        eventArgs.set_cancel(true);
    }
}

如果有任何疑虑,请告诉我。