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

[Solved] Cannot edit radgrid in webusercontrol

3 Answers 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 25 Jul 2013, 04:14 AM
I have a radgrid which has user control edit form (webusercontrol), inside which is another radgrid.  In short, both grids have a master/detail relationship.  At the moment, I use dataset as the data source, but bind it in the appropriate NeedDataSource event.

I want to commit changes to master/detail records in a single transaction, if there's a way to do this with a hierachy, I'm all ears.

When I edit a row in the master grid, webusercontrol appears, showing columns from selected row.  Grid inside webusercontrol correctly shows related detail rows.  But as soon as I click Edit, all rows disappear and grid says there are no records to display.  This happens when I click Refresh as well.

I put a breakpoint in detail grid's NeedDataSource event, which vs hits.  I checked there are data in the dataset.

I'm not loading user control dynamically.  Same webusercontrol declared in the grid is used for insert and edit.  I temporarily removed ajax manager to focus on the problem.

I already spent a day reading Telerik's extensive docs, demos and forums.  I googled, yahooed, even binged, but search results point back to Telerik site anyway. 

Any help is much appreciated.

Master
<telerik:RadGrid ID="rgHead" runat="server" ShowStatusBar="True" AutoGenerateColumns="False"
    AllowSorting="True"  AllowPaging="True"
    GridLines="None" OnPreRender="rgHead_PreRender" OnNeedDataSource="rgHead_NeedDataSource"
    OnUpdateCommand="rgHead_UpdateCommand" OnInsertCommand="rgHead_InsertCommand"
    OnDeleteCommand="rgHead_DeleteCommand"
    OnItemCommand="rgHead_ItemCommand" onitemdatabound="rgHead_ItemDataBound">
    <MasterTableView AllowSorting="True" DataKeyNames="RefNo" CommandItemDisplay="Top" Name="Head">
        <Columns>
            <telerik:GridButtonColumn Text="Delete" CommandName="Delete"
                UniqueName="DeleteCommandColumn">
            </telerik:GridButtonColumn>
            <telerik:GridTemplateColumn DataField="RefNo" SortExpression="RefNo" UniqueName="RefNo">
                <EditItemTemplate>
                    <asp:TextBox ID="Textbox1" runat="server" Text='<%# Bind("RefNo") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("RefNo") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="Description"
                ReadOnly="True" UniqueName="Description" HeaderText="Description">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Rate"
                ReadOnly="True" UniqueName="Rate" HeaderText="Rate">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Active"
                ReadOnly="True" UniqueName="Active" HeaderText="Active"
                DataType="System.Boolean">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="LastUpdateDate"
                UniqueName="LastUpdateDate" HeaderText="Last Updated">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
        </Columns>
        <EditFormSettings UserControlName="~/Controls/DetailControl.ascx" EditFormType="WebUserControl">
            <EditColumn UniqueName="EditCommandColumn1">
            </EditColumn>
        </EditFormSettings>
        <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
</telerik:RadGrid>

protected void rgHead_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    rgHead.DataSource = ds.Tables["tblHead"];
}

Detail

<telerik:RadGrid ID="rgItems" runat="server" AllowSorting="True" AutoGenerateColumns="False"
    CellSpacing="0" GridLines="None" OnNeedDataSource="rgItems_NeedDataSource" Width="70%"
    OnDeleteCommand="rgItems_DeleteCommand" OnInsertCommand="rgItems_InsertCommand"
    OnItemCommand="rgItems_ItemCommand" OnUpdateCommand="rgItems_UpdateCommand"
    OnItemDataBound="rgItems_ItemDataBound">
    <MasterTableView CommandItemDisplay="Top" ClientDataKeyNames="ItemNo" DataKeyNames="ItemNo" EditMode="EditForms">
        <RowIndicatorColumn Visible="True">
        </RowIndicatorColumn>
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <ExpandCollapseColumn Visible="True">
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridButtonColumn CommandName="DeleteItem"
                Text="Delete" UniqueName="DeleteColumn">
            </telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="ItemNo"
                HeaderText="Item No" UniqueName="ItemNo">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="DiscountRate"
                HeaderText="Discount Rate (%)" UniqueName="DiscountRate">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn2">
            </telerik:GridEditCommandColumn>
        </Columns>
        <EditFormSettings>
            <EditColumn UniqueName="EditCommandColumn2">
            </EditColumn>
        </EditFormSettings>
        <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
    </MasterTableView>
    <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
</telerik:RadGrid>

protected void rgItems_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    rgItems.DataSource = ds.Tables["tblDetails"].Select("RefNo='" + (string)DataBinder.Eval(DataItem, "RefNo") + "'");
}
        

3 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 29 Jul 2013, 10:31 AM
Hello,

If you want to save the changes from both RadGrid controls in a single transaction you should do this manually because RadGrid does not provide built-in support for transactions. You could check this Code-Project article for more information on using transactions with data-bound controls.

About your problem, could you share your full page source code along with the code-behind file content? Thus all the people who want to help you will have better understanding of your case.

Regards,
Andrey
Telerik
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 the blog feed now.
0
Jon
Top achievements
Rank 1
answered on 29 Jul 2013, 12:22 PM
Andrey, thanks for taking the time to reply.

protected void rgItems_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    rgItems.DataSource = ds.Tables["tblDetails"].Select("RefNo='" + (string)DataBinder.Eval(DataItem, "RefNo") + "'");
}

The problem was DataItem.  This is passed by radgrid that contains the webusercontrol, but as I eventually discovered, DataItem is null on subsequent postbacks, like if I click Add or Edit in the inner grid!  I hate wasting time on nitty gritty.  I wonder if using mvc has obstacles like this, or has it got it's own problems.

I spent a long day trying to fix this error.  Then the next morning, it just came to me, sigh.
0
Andrey
Telerik team
answered on 31 Jul 2013, 01:55 PM
Hi,

Yes, the DataItem is initialized only during the DataBind stage of the control. On a postback if there was no change to the data RadGrid restores its data from the ViewState and that is why the DataItem is null.

ASP.NET MVC is a different architecture and some things are done easier than the Web Forms approach, however, almost each time a new design is required and a new set of control, since Web Forms controls does not work as expected in MVC scenario.

Regards,
Andrey
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Jon
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Jon
Top achievements
Rank 1
Share this question
or