Why can I not Find The Control

1 posts, 0 answers
  1. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 11 Jun 2018 Link to this post

    I have a simple RadGrid with 2 columns. I have set AllowGenerateColumns to false but I am using the NeedData Source to populate the Grid.   One column is a Text box and the other is a dropdown list.  I have nor problems showing the Grid with the data and I can add new records.
    Here is my ASPX code to for this Grid:

    <telerik:RadGrid ID="gvOrgContacts" runat="server"
                                    AutoGenerateColumns="false"
                                    OnNeedDataSource="gvOrgContacts_NeedDataSource"
                                    AllowSorting="true"
                                    AllowPaging="false"
                                    AllowAutomaticDeletes="true"
                                    AllowFilteringByColumn="false"
                                    OnItemCreated="gvOrgContacts_ItemCreated"
                                    OnItemInserted="gvOrgContacts_ItemInserted"
                                    OnPreRender="gvOrgContacts_PreRender"
                                    OnInsertCommand="gvOrgContacts_InsertCommand"
                                    OnItemDataBound="gvOrgContacts_ItemDataBound"
                                    OnUpdateCommand="gvOrgContacts_UpdateCommand"
                                    OnDeleteCommand="gvOrgContacts_DeleteCommand"
                                    OnItemCommand="gvOrgContacts_ItemCommand"
                                    Skin="Default"
                                    Visible="false">
                                    <HeaderStyle CssClass="GridHeader" />
                                    <PagerStyle Mode="NextPrevNumericAndAdvanced" />
                                    <MasterTableView AutoGenerateColumns="false" DataKeyNames="ContactID" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
                                        <Columns>
                                            <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                                            <telerik:GridBoundColumn DataField="ContactID" HeaderText="ContactID" ReadOnly="true" UniqueName="ContactID" AllowFiltering="false"
                                                DataType="System.Int16" DefaultInsertValue="" Exportable="false" Visible="false">
                                            </telerik:GridBoundColumn>
                                            <telerik:GridBoundColumn DataField="Contact" HeaderText="Contact" SortExpression="Contact" UniqueName="Contact"></telerik:GridBoundColumn>
                                            <telerik:GridTemplateColumn UniqueName="ContactTemplateColumn" HeaderText="Contact Type">
                                                <ItemTemplate>
                                                    <asp:Label ID="Contact" runat="server"
                                                        Text='<%# DataBinder.Eval(Container.DataItem, "Contact") %>'>
                                                    </asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtContact" runat="server"></asp:TextBox>
                                                </EditItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:DropDownList ID="ddlContacts" runat="server" DataTextField="ContactType" DataValueField="ContactTypeID"></asp:DropDownList>
                                                </EditItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridButtonColumn ConfirmText="Delete this contact?" ConfirmDialogType="RadWindow"
                                                ConfirmTitle="Delete" ButtonType="ImageButton" ImageUrl="~/Images/filterCancel.gif" Text="Delete" CommandName="Delete" Exportable="false" />
                                        </Columns>
                                    </MasterTableView>
                                </telerik:RadGrid>

     

    And Here is my Code behind

    protected void gvOrgContacts_UpdateCommand(object sender, GridCommandEventArgs e)
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    string contactID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ContactID"].ToString();
                    int intContactID = Convert.ToInt16(contactID);
     
                    using (BackgroundInvEntities db = new BackgroundInvEntities())
                    {
                        var results = db.T_Contacts.Where(a => a.ContactID == intContactID).ToList();
                        DropDownList ddlContact = (DropDownList)editedItem.FindControl("ddlContacts") as DropDownList;
                        int intContactType = Convert.ToInt32(ddlContact.SelectedValue);
                        TextBox txtContacts = (TextBox)editedItem.FindControl("txtContact") as TextBox;
                        string strContact = txtContacts.Text;
                         
                        results[0].ContactTypeID = intContactType;
                        results[0].Contact = strContact;
                        DbContext.SaveChanges();
                        gvOrgContacts.MasterTableView.ClearEditItems();
                        gvOrgContacts.Rebind();
     
                    }
                }
     
                 
            }

     

    I can retrieve the selected value of the DropDownList but when the code line to set the value of the strContact runs I get an unhandled exception. "JavaScript runtime error: ObjectReference not set to an instance of an object.
    When I inspect the TextBox txtContacts it does not show a web control it shows as null.

    Any one have any ideas as to why this web control is not being found?

     

     

     

Back to Top