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

Object reference not set to an instance of an object.

1 Answer 54 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dingen
Top achievements
Rank 1
dingen asked on 02 Jan 2010, 06:28 AM
Hi

I have build a grid with a formtemplate. I want to set the label text through a button click. The problem is that it cant find the reference to the label. What to do?

reg
D

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Itemcommand.aspx.vb" Inherits="Itemcommand" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title></title
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <telerik:RadScriptManager runat="server"
        </telerik:RadScriptManager> 
        <telerik:RadGrid ID="RadGrid1" runat="server"  GridLines="None" 
             AutoGenerateColumns="False" 
            ShowStatusBar="True" DataSourceID="SqlDataSource1"
            <MasterTableView CommandItemDisplay="TopAndBottom" GridLines="None" DataKeyNames="EmployeeID" 
                AllowFilteringByColumn="False" AllowSorting="True" DataSourceID="SqlDataSource1"
                <Columns> 
                    <telerik:GridEditCommandColumn UniqueName="EditCommandColumn"
                    </telerik:GridEditCommandColumn> 
                    <telerik:GridBoundColumn UniqueName="EmployeeID" HeaderText="EmployeeID" DataField="EmployeeID" 
                        DataType="System.Int32" ReadOnly="True" SortExpression="EmployeeID"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City" 
                        UniqueName="City"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Country" HeaderText="Country" SortExpression="Country" 
                        UniqueName="Country"
                    </telerik:GridBoundColumn> 
                </Columns> 
                <EditFormSettings EditFormType="Template"
                    <EditColumn UniqueName="EditCommandColumn1"
                    </EditColumn> 
                    <FormTemplate> 
                        <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module"
                            <tr> 
                                <td> 
                                </td> 
                                <td> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    Country: 
                                </td> 
                                <td> 
                                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind( "Country"  ) %>'
                                    </asp:TextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    City: 
                                </td> 
                                <td> 
                                    <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind( "City") %>' TabIndex="1"
                                    </asp:TextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    <asp:Button ID="btnTest" CommandName="test" text="go" runat="server" /> 
                                    <asp:Label ID="lb1" runat="server"></asp:Label> 
                                </td> 
                            </tr> 
                        </table> 
                        </table> 
                    </FormTemplate> 
                </EditFormSettings> 
            </MasterTableView> 
        </telerik:RadGrid> 
    </div> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:nwindConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:nwindConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Employees]"
    </asp:SqlDataSource> 
    </form> 
</body> 
</html> 
 

Imports Telerik.Web.UI 
 
Partial Class Itemcommand 
    Inherits System.Web.UI.Page 
 
    Protected Sub RadGrid1_ItemCreated1(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated 
        If e.Item.ItemType = GridItemType.EditFormItem And e.Item.IsInEditMode Then 
 
            Dim testbut As Button = CType(e.Item.FindControl("btnTest"), Button) 
 
            Dim lbref As Label = CType(e.Item.FindControl("lb1"), Label) 
 
            AddHandler testbut.Click, AddressOf testbutclicked 
 
        End If 
    End Sub 
 
    Protected Sub testbutclicked(ByVal sender As ObjectByVal e As EventArgs) 
        Dim lb As Label = RadGrid1.FindControl("lb1"
        lb.Text = "hi" 
    End Sub 
End Class 
 

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 04 Jan 2010, 06:05 AM
Hi,

You can try the following code in order to set the Label text when clicking the button in FormTemplate.

VB:
 
Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As GridItemEventArgs) 
    If e.Item.ItemType = GridItemType.EditFormItem And e.Item.IsInEditMode Then 
        Dim testbut As Button = DirectCast(e.Item.FindControl("btnTest"), Button) 
        AddHandler testbut.Click, AddressOf testbut_Click 
    End If 
End Sub 
Private Sub testbut_Click(ByVal sender As ObjectByVal e As EventArgs) 
    Dim btn As Button = DirectCast(sender, Button) 
    Dim editItem As GridEditFormItem = DirectCast(btn.NamingContainer, GridEditFormItem) 
    Dim lbref As Label = DirectCast(editItem.FindControl("lb1"), Label) 
    lbref.Text = "hi" 
End Sub 

Also you can try the following code in ItemCommand event of grid in order to perform same functionality.
VB:
 
Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs)  
    If e.CommandName = "test" Then  
        Dim edititem As GridEditFormItem = DirectCast(TryCast(e.Item, GridDataItem).EditFormItem, GridEditFormItem)  
        Dim lbl As Label = DirectCast(edititem.FindControl("lb1"), Label)  
        lbl.Text = "hi"  
    End If  
End Sub  

-Shinu.
Tags
Grid
Asked by
dingen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Share this question
or