Null object on loginview.findcontrol

Last post 01-13-2009 1:06 AM by Mazenx. 17 replies.

Sort Posts:

  • Null object on loginview.findcontrol

    01-02-2009, 5:07 PM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    hello....I wanna reach an imagebutton to create an update progress , but it keeps giving me null object

    protected void Page_Load(object sender, EventArgs e)

    {

    ImageButton btnn = (ImageButton)UploadView.FindControl("UploadButton");

    ScriptManager1.RegisterAsyncPostBackControl(btnn);

    }

    I tried the code in the imagebutton event , It didnt show me null object

    protected void UploadButton_Click(object sender, ImageClickEventArgs e)

    {

    // I tried it here 

    UpdatePanel1.Update();

    }

    what shall i do to fix this ?

    and btw , I am using that line ( UploadView.FindControl ) in the page and it's working so great ....but maybe and maybe  , this error happens cause the button is inside the logged in template while when page loads , the anonymous template is shown , but not sure and if that was the reason i dont know how to fix it so...anyone can help?!

  • Re: Null object on loginview.findcontrol

    01-02-2009, 9:04 PM
    • Star
      10,827 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,973

    Mazenx:
    this error happens cause the button is inside the logged in template while when page loads , the anonymous template is shown

     

    Visibility of a control only matters on the client-side.  Finding it on the server is an issue of nested controls, becuase FindControl does not loop through a control's nested/child controls.  Thus, if your UploadButton is inside a <div> or another control element, and not directly in your UploadView, then you won't be able to find it.

    I've put the fix to this in several spots on the forum, but phrasing questions about code is even harder than knowing what to look for when searching, so I'll post it again.  This is in VB, but should be easily translated to C#.

    ''' <summary>
    		''' This function will search for a control with the passed-in ID within all of the 
    		''' subcontrols of the passed-in ParentControl.
    		''' </summary>
    		''' <param name="ControlID">The ID of the control to search for.</param>
    		''' <param name="ParentControl">The highest level control that you want to search through.
    		''' If you don't know where the control is at all with relation to the page, pass in the entire page.
    		''' </param>
    		''' <returns>Returns the instance of the control you searched for, or Nothing if the control does not
    		''' exist within the context of the passed-in ParentControl.
    		''' </returns>
    		''' <remarks>The best practice is to pass in a ParentControl that is relatively close to the
    		''' control being sought.  For example, if you know the control you want is nested within a panel server 
    		''' control called "pnlPanel", pass in the pnlPanel control rather than the entire page.  This will narrow down
    		''' the search time dramatically and improve performance, especially if many calls to this function are made
    		''' by several page requests and/or by a single page.
    		''' </remarks>
    		Public Shared Function FindControlRecursively(ByVal ControlID As String, ByVal ParentControl As Control) As Control
    			Dim c As Control
    
    			For Each c In ParentControl.Controls
    				If IsNothing(c.ID) = False AndAlso c.ID = ControlID Then
    					Return c
    				Else
    					If c.HasControls Then
    						' loop through this control collection as well, until we find what we
    						'	are looking for.
    						Dim foundcontrol As Control = FindControlRecursively(ControlID, c)
    						If IsNothing(foundcontrol) Then
    							Continue For
    						Else
    							Return foundcontrol
    						End If
    					End If
    				End If
    			Next
    
    			' if we didn't find the control by now, we aren't going to.
    			Return Nothing
    
    		End Function
    
    ' To call this in your code, (using VB):
    Dim btnn As ImageButton = CType(FindControlRecursively("UploadButton", UploadView), ImageButton)
    
    If btnn Is Nothing Then
    ' error
    Else
    ' good to go...
    End If
    

     

    If the above doesn't work, try passing in your page as the parentControl.  That should find any control on the page.

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Null object on loginview.findcontrol

    01-03-2009, 5:51 AM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    hmmm...I have similiar function which is not working too , here is it :

    private static Control FindControlRecursive(Control Root, string Id)

    {

    if (Root.ID == Id)

    return Root;

    foreach (Control Ctl in Root.Controls)

    {

    Control FoundCtl = FindControlRecursive(Ctl, Id);

    if (FoundCtl != null)

    return FoundCtl;

    }

    return null;

    }

    hmmm , not sure how to pass my whole page as a control.....

  • Re: Null object on loginview.findcontrol

    01-03-2009, 3:16 PM
    • Star
      10,827 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,973

    In C# and if calling from the page, pass 'this', which refers to the current class (the page).  If that button exists and has been given the ID you specified, then it should be retrievable.

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Null object on loginview.findcontrol

    01-03-2009, 4:22 PM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    hmmm...I tried , same error.....anyway what I am trying to do here is that I want to add an update progress to that button , so....I am writing this :

    ImageButton btnn = (ImageButton)FindControlRecursive(this,"UploadButton");

    ScriptManager1.RegisterAsyncPostBackControl(btnn);

    and it gives me error saying btnn cant be null .

  • Re: Null object on loginview.findcontrol

    01-04-2009, 12:49 AM
    • Star
      10,827 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,973

    can you post your aspx code?

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Null object on loginview.findcontrol

    01-04-2009, 5:16 AM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    yeah sure , here is the aspx code that the button exists in , you want me to write you the full code or this is enough ?!?

    <asp:LoginView ID="UploadView" runat="server">

    <LoggedInTemplate>

     

    <asp:Label ID="Label9" runat="server" Font-Bold="True" ForeColor="#757575"

     

    style="top: 368px; left: 458px; position: absolute; height: 19px; width: 625px"

    Visible="False" Font-Names="Times New Roman"></asp:Label>

    <asp:FileUpload ID="Fup3" runat="server"

    style="top: 256px; left: 261px; position: absolute; height: 22px; width: 217px" />

    <asp:FileUpload ID="Fup4" runat="server"

    style="top: 286px; left: 261px; position: absolute; height: 22px; width: 217px" />

    <asp:FileUpload ID="Fup2" runat="server"

    style="top: 226px; left: 261px; position: absolute; height: 22px; width: 217px" />

    <asp:FileUpload ID="Fup1" runat="server"

     

     

    style="top: 196px; left: 261px; position: absolute; height: 22px; width: 217px; bottom: 234px" />

    <asp:Label ID="TitleLabel" runat="server" Font-Bold="True" Font-Names="Times New Roman"

    Font-Size="Medium" ForeColor="#757575"

    style="top: 165px; left: 585px; position: absolute; height: 19px; width: 74px"

    Text="Book Title"></asp:Label>

    <asp:TextBox ID="S1Title" runat="server"

     

     

     

    style="top: 198px; left: 520px; position: absolute; height: 15px; width: 247px"

    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>

    <asp:Label ID="LF1" runat="server" Font-Bold="False" Font-Size="Medium"

    ForeColor="#FF3300"

    style="top: 401px; left: 310px; position: absolute; height: 19px; width: 560px"

    Visible="False" Font-Names="Times New Roman"></asp:Label>

    <asp:Label ID="LF2" runat="server" Font-Size="Medium" ForeColor="#FF3300"

     

    style="top: 424px; left: 310px; position: absolute; height: 19px; width: 679px"

    Visible="False" Font-Names="Times New Roman"></asp:Label>

    <asp:Label ID="LF3" runat="server" Font-Size="Medium" ForeColor="#FF3300"

     

    style="top: 448px; left: 311px; position: absolute; height: 19px; width: 700px"

    Visible="False" Font-Names="Times New Roman"></asp:Label>

    <asp:Label ID="LF4" runat="server" Font-Size="Medium" ForeColor="#FF3300"

     

     

     

    style="top: 472px; left: 310px; position: absolute; height: 19px; width: 563px"

    Font-Names="Times New Roman"></asp:Label>

    <asp:TextBox ID="S2Title" runat="server"

     

     

     

    style="top: 228px; left: 520px; position: absolute; height: 15px; width: 247px; right: 199px;"

    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>

    <asp:TextBox ID="S3Title" runat="server"

     

    style="top: 258px; left: 520px; position: absolute; height: 15px; width: 247px"

    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>

    <asp:TextBox ID="S4Title" runat="server"

     

     

     

    style="top: 288px; left: 520px; position: absolute; height: 15px; width: 247px"

    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>

    <asp:CheckBox ID="Samechk" runat="server"

    style="top: 193px; left: 779px; position: absolute; height: 20px; width: 159px"

    AutoPostBack="True" Font-Bold="True" ForeColor="#757575"

    oncheckedchanged="Samechk_CheckedChanged"

    Text="All files...."

    Font-Names="Times New Roman" />

    <asp:Label ID="Label5" runat="server" BorderColor="#FF822F" BorderStyle="Solid"

    BorderWidth="0px" Font-Bold="True" Font-Size="Small" ForeColor="#757575"

    style="top: 246px; left: 780px; position: absolute; height: 34px; width: 184px"

    Text="Now same..." Visible="False"

    Font-Names="Times New Roman"></asp:Label>

    <asp:Label ID="SupportedLabel0" runat="server" Font-Bold="False"

    Font-Size="Small" ForeColor="#EA0000"

    style="top: 556px; left: 20px; position: absolute; height: 19px; width: 531px"

     

     

     

    Text="* Size limit" Font-Names="Times New Roman"></asp:Label>

    <asp:HyperLink ID="HyperLink6" runat="server" Font-Bold="True"

    Font-Size="Small" ForeColor="Red" Font-Underline = "True"

    style="top: 370px; left: 690px; position: absolute; height: 19px; width: 103px"

    Visible="False" NavigateUrl="~/Help.aspx" Font-Names="Times New Roman">( What is this ?? )</asp:HyperLink>

    <asp:Label ID="SupportedLabel" runat="server" Font-Bold="False"

    Font-Size="Small" ForeColor="#EA0000"

    style="top: 538px; left: 20px; position: absolute; height: 19px; width: 531px"

     

     

    Text="*Text"

    Font-Names="Times New Roman"></asp:Label>

     

    <asp:ImageButton ID="UploadButton" runat="server" ImageUrl="~/Uploadfiles.png"

    style="top: 326px; left: 480px; position: absolute; height: 39px; width: 72px"

    onclick="UploadButton_Click" ValidationGroup="upload" />

    </LoggedInTemplate>

  • Re: Null object on loginview.findcontrol

    01-04-2009, 3:25 PM
    • Star
      10,827 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,973

    That was enough code for me to test the base of your issues.  I tried this in both VB with my function and in C# with yours, and I had no issues whatsoever with finding the control.  I will post my code for C#, as that is what you are working in:

     

     

    <!-- File name is testc.aspx, separate C# code page. -->
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testc.aspx.cs" Inherits="testc" %>
    
    
    <!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 id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:ScriptManager ID="sm1" runat="server" ></asp:ScriptManager>
        <asp:LoginView ID="UploadView" runat="server">
    
    <LoggedInTemplate>
    
     
    
    <asp:Label ID="Label9" runat="server" Font-Bold="True" ForeColor="#757575" 
     
    
    style="top: 368px; left: 458px; position: absolute; height: 19px; width: 625px" 
    Visible="False" Font-Names="Times New Roman"></asp:Label>
    
    <asp:FileUpload ID="Fup3" runat="server" 
    style="top: 256px; left: 261px; position: absolute; height: 22px; width: 217px" />
    
    <asp:FileUpload ID="Fup4" runat="server" 
    style="top: 286px; left: 261px; position: absolute; height: 22px; width: 217px" />
    
    <asp:FileUpload ID="Fup2" runat="server" 
    style="top: 226px; left: 261px; position: absolute; height: 22px; width: 217px" />
    
    <asp:FileUpload ID="Fup1" runat="server" 
     
    
     
    
    style="top: 196px; left: 261px; position: absolute; height: 22px; width: 217px; bottom: 234px" />
    
    <asp:Label ID="TitleLabel" runat="server" Font-Bold="True" Font-Names="Times New Roman" 
    Font-Size="Medium" ForeColor="#757575" 
    
    style="top: 165px; left: 585px; position: absolute; height: 19px; width: 74px" 
    Text="Book Title"></asp:Label>
    
    <asp:TextBox ID="S1Title" runat="server" 
     
    
     
    
     
    
    style="top: 198px; left: 520px; position: absolute; height: 15px; width: 247px" 
    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>
    
    <asp:Label ID="LF1" runat="server" Font-Bold="False" Font-Size="Medium" 
    ForeColor="#FF3300" 
    
    style="top: 401px; left: 310px; position: absolute; height: 19px; width: 560px" 
    Visible="False" Font-Names="Times New Roman"></asp:Label>
    
    <asp:Label ID="LF2" runat="server" Font-Size="Medium" ForeColor="#FF3300" 
     
    
    style="top: 424px; left: 310px; position: absolute; height: 19px; width: 679px" 
    Visible="False" Font-Names="Times New Roman"></asp:Label>
    
    <asp:Label ID="LF3" runat="server" Font-Size="Medium" ForeColor="#FF3300" 
     
    
    style="top: 448px; left: 311px; position: absolute; height: 19px; width: 700px" 
    Visible="False" Font-Names="Times New Roman"></asp:Label>
    
    <asp:Label ID="LF4" runat="server" Font-Size="Medium" ForeColor="#FF3300" 
     
    
     
    
     
    
    style="top: 472px; left: 310px; position: absolute; height: 19px; width: 563px" 
    Font-Names="Times New Roman"></asp:Label>
    
    <asp:TextBox ID="S2Title" runat="server" 
     
    
     
    
     
    
    style="top: 228px; left: 520px; position: absolute; height: 15px; width: 247px; right: 199px;" 
    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>
    
    <asp:TextBox ID="S3Title" runat="server" 
     
    
    style="top: 258px; left: 520px; position: absolute; height: 15px; width: 247px" 
    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>
    
    <asp:TextBox ID="S4Title" runat="server" 
     
    
     
    
     
    
    style="top: 288px; left: 520px; position: absolute; height: 15px; width: 247px" 
    BorderColor="#B1B1B1" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>
    
    <asp:CheckBox ID="Samechk" runat="server" 
    style="top: 193px; left: 779px; position: absolute; height: 20px; width: 159px" 
    
    Font-Bold="True" ForeColor="#757575" 
    
    Text="All files...." 
    Font-Names="Times New Roman" />
    
    <asp:Label ID="Label5" runat="server" BorderColor="#FF822F" BorderStyle="Solid" 
    BorderWidth="0px" Font-Bold="True" Font-Size="Small" ForeColor="#757575" 
    
    style="top: 246px; left: 780px; position: absolute; height: 34px; width: 184px" 
    Text="Now same..." Visible="False" 
    
    Font-Names="Times New Roman"></asp:Label>
    
    <asp:Label ID="SupportedLabel0" runat="server" Font-Bold="False" 
    Font-Size="Small" ForeColor="#EA0000" 
    
    style="top: 556px; left: 20px; position: absolute; height: 19px; width: 531px" 
     
    
     
    
     
    
    Text="* Size limit" Font-Names="Times New Roman"></asp:Label>
    
    <asp:HyperLink ID="HyperLink6" runat="server" Font-Bold="True" 
    Font-Size="Small" ForeColor="Red" Font-Underline ="True" 
    
    style="top: 370px; left: 690px; position: absolute; height: 19px; width: 103px" 
    Visible="False" NavigateUrl="~/Help.aspx" Font-Names="Times New Roman">( What is this ?? )</asp:HyperLink>
    
    <asp:Label ID="SupportedLabel" runat="server" Font-Bold="False" 
    Font-Size="Small" ForeColor="#EA0000" 
    
    style="top: 538px; left: 20px; position: absolute; height: 19px; width: 531px" 
     
    
     
    
    Text="*Text" 
    Font-Names="Times New Roman"></asp:Label>
    
     
    
    <asp:ImageButton ID="UploadButton" runat="server" ImageUrl="~/Uploadfiles.png" 
    style="top: 326px; left: 480px; position: absolute; height: 39px; width: 72px" 
    ValidationGroup="upload" OnClick="Upload" />
    
    </LoggedInTemplate>
    </asp:LoginView>
        </div>
        </form>
    </body>
    </html>
    

     

     

    //Make sure you have some form of ajax, either 1.0 release or
    // .NET 3.5
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class testc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        private static Control FindControlRecursive(Control Root, string Id)
        {
    
            if (Root.ID == Id)
                return Root;
    
            foreach (Control Ctl in Root.Controls)
            {
    
                Control FoundCtl = FindControlRecursive(Ctl, Id);
                if (FoundCtl != null)
    
                    return FoundCtl;
            }
    
            return null;
        }
    
        protected void Upload(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)FindControlRecursive(UploadView, "UploadButton");
            String str = btn.ID;
            sm1.RegisterAsyncPostBackControl(btn);
        }
    
    }
    

     

     

    Put a breakpoint at the second code line in the Upload method to see that the image button was found.

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Null object on loginview.findcontrol

    01-05-2009, 8:31 AM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    hmmm...I tried it in another function else the page load and its working , the problem that in the page load it's not working .

  • Re: Null object on loginview.findcontrol

    01-05-2009, 1:58 PM
    • Star
      10,827 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,973

    Ok, I reproduced the problem.  My web.config was set to Windows for authentication (so I was always 'logged in').

    Apparently, the template is an either/or scenario; the button will not exist if the user is not logged in. 

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Null object on loginview.findcontrol

    01-05-2009, 4:03 PM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    hmmm....I was thinking to put the button inside an update panel and put with it the update progress and an image for 'loading' , but....things didnt work that well , the postback isnt happening perfectly like some labels arent appearing and some textbox's arent getting empty....

  • Re: Null object on loginview.findcontrol

    01-11-2009, 1:11 AM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    any ideas guys to solve this issue?!

  • Re: Null object on loginview.findcontrol

    01-11-2009, 1:55 PM
    • Star
      10,827 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,973

    Mazenx:
    things didnt work that well , the postback isnt happening perfectly like some labels arent appearing and some textbox's arent getting empty....

    Were all of the controls considered 'not working' inside the update panel, as well?  In order for them to update, the controls need to be in the update panel, or in an update panel of their own with an <AsyncPostBack> trigger that handles the upload button (either the clicked event specifically or just specify the button id to handle all events).

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Null object on loginview.findcontrol

    01-12-2009, 4:20 AM
    • Member
      37 point Member
    • Mazenx
    • Member since 09-11-2008, 11:41 AM
    • Posts 237

    hmmm I gave it a try and some file uploads uploaded files that shouldnt be uploaded so...I dont feel into entering a huge security risk , anyway here is what I wanna do and which yahoo does in attachments , I just wanna show an activity indicator(some loading picture) with javascript...that's enough and shows the user something is being done and doesnt create risks I think...the problem is , the picture must be invisible at the start , and I wont be able to see it with javascript , so I was thinking to write c# script for it...and write a function on the onclientclick...can you help me with that ??

  • Re: Null object on loginview.findcontrol

    01-12-2009, 5:37 AM
    • Star
      10,827 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,973

    To give a basic example, you will want to create a panel with Visible="True".  If you set that property to False, then the control markup is not sent to the client.  To hide the control, set the style property 'display: none;'.

     

    <asp:Panel ID="pnlUploading" runat="server" style="display:none;">
      <!-- put your image info control in here with an asp:image control -->
    </asp:Panel>

     

    Next, create a simple javascript function that will find the control by id and change that display style we set:

    <script>
    // controlID is the clientID of the asp.net control
    // displayMode is the text to set to the display property,
    //  usually none (hidden), inline (shown inline, like a <span>),
    //  or block (shown as a block element, like a <div>)
    function SetDisplay(controlID, displayMode)
    {
      var control = document.getElementById(controlID);
      
      if (control != null)
      {
        control.style.display = displayMode;
      }
    }
    </script>

     

    Finally, in your page somewhere (e.g., page_load or Page_prerender), set the OnClientClick property to your method with the proper arguments. VB shown:

    Dim js As String = String.Format("SetDisplay('{0}', '{1}');", pnlUploading.ClientID, "block")

    btnUpload.OnClientClick = js

     

    This does not take into account the positioning of the panel.  You may also look into using a ModalPopup if it is available to you (.Net 3.5 SP1 required by the AjaxControlToolkit) because it is easier to set up to display in the center of the page than trying to devise your own custom positioning code.

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
Page 1 of 2 (18 items) 1 2 Next >