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

[Solved] 500 error when using custom template

4 Answers 214 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt Travis
Top achievements
Rank 1
Matt Travis asked on 23 Oct 2008, 05:59 PM
Hello,

I'm getting the following javascript Ajax related error when I set up a custom iTemplate instead of using the Telerik GridTemplateColumn.  The error shows up when I click on the custom link I create instead of using the aspx GridTemplateColumn's link.

Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

The error details say it is a 500 error.


If I use the GridTemplateColumn shown below, it works fine.

<Columns> 
     <telerik:GridTemplateColumn UniqueName="Delete" AllowFiltering='false'> 
          <HeaderStyle Width="45px" /> 
          <ItemStyle CssClass="c" /> 
          <ItemTemplate> 
               <asp:HyperLink ID="DeleteLink" runat="server" Text="Delete" /> 
          </ItemTemplate> 
     </telerik:GridTemplateColumn> 
</Columns> 

But when I convert it to an iTemplate using the following code, I receive the error.

Dim type As String = "Delete" 
Dim column As New GridTemplateColumn 
column.AllowFiltering = False 
column.HeaderStyle.Width = 45 
column.ItemStyle.CssClass = "c" 
column.ItemTemplate = New HyperlinkTemplate(type) 
column.UniqueName = type 
RadGrid1.MasterTableView.Columns.Add(column) 

My HyperlinkTemplate is the following class.

Public Class HyperlinkTemplate 
    Implements ITemplate 
    Private _type As Object 
 
    Public Sub New(ByVal type As String
        MyBase.New() 
        _type = type 
    End Sub 
 
    Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn 
        Dim hl As New HyperLink 
        hl.EnableViewState = False 
        hl.ID = _type & "Link" 
        hl.Text = _type 
        hl.Attributes("href") = "#" 
        container.Controls.Add(hl) 
    End Sub 
End Class 

I have an OnItemCreated event that runs the following code which adds an onclick event which I will show next and customizes the ID to make it unique:

Dim deleteLink As HyperLink = DirectCast(e.Item.FindControl("DeleteLink"), HyperLink) 
deleteLink.Attributes("onclick") = [String].Format("return Delete('{0}');", e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)(IDcolumnIsHere)) 
deleteLink.ID &= e.Item.ItemIndex 
 

The onclick code calls the RadAjaxManager I have set up.

function Delete(id) { $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Grid,Delete," + customValue + "," + id); } 


The custom column is added during the Page_Load event.

It never reaches the Ajax Handler I have set up unless I use the aspx GridTemplateColumn instead of my custom template.

Am I doing something wrong with the iTemplate or am I not initializing something I should be that integrates it with the RadGrid?  I've tried going through the various properties available to me for the column and the iTemplate, but I haven't had any luck making the error go away so I know I'm missing something... just not sure what.


4 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 27 Oct 2008, 02:24 PM
Hello Matt Travis,

Can you please try to disable the ajax functionality, as this way you will be able to see the full stacktrace of the exception and locate more precisely where it has occurred.

Sincerely yours,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Matt Travis
Top achievements
Rank 1
answered on 27 Oct 2008, 06:52 PM
I changed the Ajax request to a postback, and it turns out that it is dieing because the Hyperlink Template isn't added to the grid before the logic attempts to set the link for it (i.e. the ItemCreated event is firing before the Page Load event).

I'm in the process of adding a work around that involves storing the related IDs during the Item Creation Event, but if you can recommend an event that works similar to ItemCreated (gives me access to each grid item row and their child controls) but is handled after the load, it might save me some time.
0
Matt Travis
Top achievements
Rank 1
answered on 27 Oct 2008, 07:55 PM
I've completed my work around so I don't need the event any more.  Marking the initial reply you left as the answer.
0
Ivan Stoilkov
Top achievements
Rank 1
answered on 08 Oct 2009, 03:55 PM
Thank you Rosen! Your simple suggestion helped me to locate the root of the server error in my case. I am using a rad grid control to populate data from a database and a user web control to edit/create the data on the grid. In my case the exception was not being raised on my development environment, it also was updating the data, but on the test server it gave me the server error message. Disabling the ajax functionality of the grid showed me the error on the stack trace. I was implicitly converting a rad textbox to an asp textbox and the server did not like that. However, the IDE of VS 2008 allowed the implicit conversion of the two types, which did not raise the excepton on the dev.
But, this is a very good startup point: to temporarily disable the ajax functionality
Tags
Grid
Asked by
Matt Travis
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Matt Travis
Top achievements
Rank 1
Ivan Stoilkov
Top achievements
Rank 1
Share this question
or