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

hierarchical Radgrid demo with CRUD operation from codebehind

6 Answers 175 Views
Grid
This is a migrated thread and some comments may be shown as answers.
VIJAY
Top achievements
Rank 1
VIJAY asked on 15 May 2012, 01:32 PM
Need a demo of hierarchical  radgrid having CRUD operation from code behind.
Have used imagebutton as buttotype for edid and delete but it is giving error:

Sys.WebForms.PageRequestManagerServerErrorException: Unable to cast object of type 'Telerik.Web.UI.GridLinkButton' to type 'System.Web.UI.WebControls.ImageButton'.

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 May 2012, 01:40 PM
Hello Vijay,

Can you please provide your line of codes from where this error raised?

Note : You get this error because there is miss match in Casting. So Replace ImageButton to LinkButton.

Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 15 May 2012, 01:43 PM
Hello Vijay,

Here is the sample demo for CRUD operation in hierarchy.
For the second question, such errors when there is a mismatch in the type casting. Since you are using ImageButton, you need to access it as shown below.
C#:
ImageButton imgbtn = (ImageButton)item["UniqueName"].Controls[0];
Please provide your code if it doesn't help.
Thanks,
Shinu.
0
VIJAY
Top achievements
Rank 1
answered on 15 May 2012, 02:58 PM
Folowing is my itemdatabound code:

 protected void rgdLabTest_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.OwnerTableView.Name == "Detail")
            {

                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                                        GridEditFormItem editItem = (GridEditFormItem)e.Item;
                                  ImageButton updateBtn = (ImageButton)editItem.FindControl("UpdateButton");
                    ImageButton cancelBtn = (ImageButton)editItem.FindControl("CancelButton");

                    if (updateBtn != null)
                    {
                        updateBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_025", BOL.Classes.EHRSessions.Culture);
                        updateBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        updateBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        updateBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/apply_ico.png");

                        cancelBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_006", BOL.Classes.EHRSessions.Culture);
                        cancelBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        cancelBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        cancelBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/close_pop_ico.png");

                        //updateBtn.Attributes.Add("OnClick", "return ValidateGroup('" + t1.ClientID + "','" + t2.ClientID + "');");
                    }

                    ImageButton insertBtn = (ImageButton)editItem.FindControl("PerformInsertButton");
                    if (insertBtn != null)
                    {
                        insertBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_005", BOL.Classes.EHRSessions.Culture);
                        insertBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        insertBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        insertBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/apply_ico.png");

                        cancelBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_006", BOL.Classes.EHRSessions.Culture);
                        cancelBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        cancelBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        cancelBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/close_pop_ico.png");

                        // insertBtn.Attributes.Add("OnClick", "return ValidateGroup('" + t1.ClientID + "','" + t2.ClientID + "');");
                    }
                }

            }
            else
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditableItem editItem = (GridEditableItem)e.Item;
                    TextBox t1 = (TextBox)editItem["AttributeName"].Controls[0];

                    ImageButton updateBtn = (ImageButton)editItem.FindControl("UpdateButton");
                    ImageButton cancelBtn = (ImageButton)editItem.FindControl("CancelButton");

                    if (updateBtn != null)
                    {
                        updateBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_025", BOL.Classes.EHRSessions.Culture);
                        updateBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        updateBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        updateBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/apply_ico.png");

                        cancelBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_006", BOL.Classes.EHRSessions.Culture);
                        cancelBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        cancelBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        cancelBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/close_pop_ico.png");

                        //updateBtn.Attributes.Add("OnClick", "return ValidateGroup('" + t1.ClientID + "','" + t2.ClientID + "');");
                    }

                    ImageButton insertBtn = (ImageButton)editItem.FindControl("PerformInsertButton");
                    if (insertBtn != null)
                    {
                        insertBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_005", BOL.Classes.EHRSessions.Culture);
                        insertBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        insertBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        insertBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/apply_ico.png");

                        cancelBtn.ToolTip = Utility.ContentFetcher.GetResourceContent("RES_006", BOL.Classes.EHRSessions.Culture);
                        cancelBtn.Attributes.Add("onmouseover", "visibilityOfToolTip(this, 1);");
                        cancelBtn.Attributes.Add("onmouseout", "visibilityOfToolTip(this);");
                        cancelBtn.ImageUrl = ResolveUrl("~/App_Themes/Bari_Default/images/close_pop_ico.png");

                        // insertBtn.Attributes.Add("OnClick", "return ValidateGroup('" + t1.ClientID + "','" + t2.ClientID + "');");
                    }
                }

                if (e.Item is GridPagerItem && !(e.Item as GridPagerItem).IsTopPager)
                {

                    rgdLabTest.PagerStyle.AlwaysVisible = (rgdLabTest.MasterTableView != null && rgdLabTest.MasterTableView.DataSourceCount > 5);

                }
            }

        }

And Below id the Grid:
  <telerik:RadGrid ID="rgdLabTest" runat="server" GridLines="None" AllowAutomaticDeletes="false"
                        AllowAutomaticInserts="false" PageSize="5" AllowAutomaticUpdates="false" AllowPaging="True"
                        AutoGenerateColumns="False" OnDetailTableDataBind="rgdLabTest_DetailTableDataBind"
                        Skin="BariRC" EnableEmbeddedSkins="false" OnInsertCommand="rgdLabTest_InsertCommand"
                        OnItemCommand="rgdLabTest_ItemCommand" OnItemDataBound="rgdLabTest_ItemDataBound"
                        OnNeedDataSource="rgdLabTest_NeedDataSource" OnUpdateCommand="rgdLabTest_UpdateCommand">
                        <PagerStyle Mode="NumericPages"></PagerStyle>
                        <MasterTableView Width="100%" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage"
                            DataKeyNames="Id" HorizontalAlign="NotSet" EditMode="EditForms" AutoGenerateColumns="False"
                            CommandItemSettings-ShowRefreshButton="false" CommandItemSettings-AddNewRecordImageUrl="../../App_Themes/Bari_Default/images/add_ico.png">
                            <Columns>
                                <telerik:GridBoundColumn DataField="AttributeName" HeaderText="AttributeName" SortExpression="AttributeName"
                                    UniqueName="AttributeName" ColumnEditorID=" GridTextBoxColumnEditor1" MaxLength="200">
                                </telerik:GridBoundColumn>
                                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"
                                    HeaderText="Edit" EditImageUrl="~/App_Themes/Bari_Default/images/edit_ico.png"
                                    ItemStyle-Width="200px" ItemStyle-Wrap="false">
                                </telerik:GridEditCommandColumn>
                                <telerik:GridButtonColumn ConfirmText="" ConfirmDialogType="RadWindow" ConfirmTitle="Delete"
                                    ButtonType="ImageButton" UniqueName="DeleteColumn" HeaderText="Delete" ImageUrl="~/App_Themes/Bari_Default/images/delete_ico.png">
                                </telerik:GridButtonColumn>
                            </Columns>
                            <DetailTables>
                                <telerik:GridTableView Name="Detail" Width="100%" runat="server" CommandItemDisplay="Top"
                                    DataKeyNames="Id,GenderLookupDisplayValue" CommandItemSettings-ShowRefreshButton="false">
                                    <Columns>
                                        <telerik:GridTemplateColumn HeaderText="Gender" UniqueName="GenderLookupDisplayValue">
                                            <EditItemTemplate>
                                                <telerik:RadComboBox ID="rcbGenderLookupDisplayValue" runat="server" AutoPostBack="false"
                                                    DataValueField="GenderLookupDisplayValue" DataTextField="GenderLookupDisplayValue"
                                                    Skin="BariRC" EnableEmbeddedSkins="false">
                                                </telerik:RadComboBox>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="lblGenderLookupDisplayValue" runat="server" Text='<%#Eval("GenderLookupDisplayValue")%>'></asp:Label>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridNumericColumn DataField="StartAge" HeaderText="Start Age" SortExpression="StartAge"
                                            UniqueName="StartAge" ColumnEditorID="GridNumericColumnEditor1" MaxLength="6">
                                        </telerik:GridNumericColumn>
                                        <telerik:GridNumericColumn DataField="EndAge" HeaderText="End Age" SortExpression="EndAge"
                                            UniqueName="EndAge" ColumnEditorID="GridNumericColumnEditor2" MaxLength="6">
                                        </telerik:GridNumericColumn>
                                        <telerik:GridBoundColumn DataField="NormalStartRange" HeaderText="Normal StartRange"
                                            SortExpression="NormalStartRange" UniqueName="NormalStartRange" ColumnEditorID="GridTextBoxColumnEditor2">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="NormalEndRange" HeaderText="Normal EndRange"
                                            SortExpression="NormalEndRange" UniqueName="NormalEndRange" ColumnEditorID="GridTextBoxColumnEditor3">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridBoundColumn DataField="Unit" HeaderText="Unit" SortExpression="Unit"
                                            UniqueName="" ColumnEditorID="GridTextBoxColumnEditor4">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"
                                            HeaderText="Edit" EditImageUrl="~/App_Themes/Bari_Default/images/edit_ico.png"
                                            ItemStyle-Width="200px" ItemStyle-Wrap="false">
                                        </telerik:GridEditCommandColumn>
                                        <telerik:GridButtonColumn ConfirmText="" ConfirmDialogType="RadWindow" ConfirmTitle="Delete"
                                            ButtonType="ImageButton" UniqueName="DeleteColumn" HeaderText="Delete" ImageUrl="~/App_Themes/Bari_Default/images/delete_ico.png">
                                        </telerik:GridButtonColumn>
                                    </Columns>
                                </telerik:GridTableView>
                            </DetailTables>
                        </MasterTableView>
                    </telerik:RadGrid>

0
Shinu
Top achievements
Rank 2
answered on 16 May 2012, 06:23 AM
Hello Vijay,

Since you are using the EditMode as EditForms, access it using GridEditFormItem.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   if (e.Item is GridEditFormItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "Detail")
   {
     GridEditFormItem editItem = (GridEditFormItem)e.Item;
     LinkButton updateBtn = (LinkButton)editItem.FindControl("UpdateButton");
   }
}

Thanks,
Shinu.
0
VIJAY
Top achievements
Rank 1
answered on 16 May 2012, 11:10 AM
But i have to use Imagebutton.

In the code behind as well as designer i have used iamgebutton still the error is coming.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 May 2012, 12:03 PM
Hello VIJAY,

Please check below code snippet.

Note : Please check "EditFormSettings" in below code snippet.


<telerik:RadGrid ID="RadGrid1" runat="server">
  <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="false">
    <EditFormSettings>
      <EditColumn UniqueName="EditCommandColumn" ButtonType="ImageButton" CancelImageUrl="~/RadControls/Grid/Skins/Default/Cancel.gif"
        UpdateImageUrl="~/RadControls/Grid/Skins/Default/Update.gif" InsertImageUrl="~/RadControls/Grid/Skins/Default/Insert.gif">
      </EditColumn>
    </EditFormSettings>
    <Columns>
      <telerik:GridBoundColumn UniqueName="ContactName" HeaderText="Contact name" DataField="ContactName" />
      <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="ImageButton"
        EditImageUrl="~/RadControls/Grid/Skins/Default/Edit.gif">
      </telerik:GridEditCommandColumn>
      <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"
        ButtonType="ImageButton" ImageUrl="~/RadControls/Grid/Skins/Default/Delete.gif">
      </telerik:GridButtonColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
VIJAY
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
VIJAY
Top achievements
Rank 1
Share this question
or