Results 1 to 10 of 10

Thread: FindControl + Casting Results In NullReferenceException

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    FindControl + Casting Results In NullReferenceException

    I'm using a third party control called an Accordion from Juice UI. Within that control, I have a table with labels.

    VB.NET Code:
    1. <juice:Accordion ID="accSwrlHddRepl" runat="server">
    2.                 <juice:AccordionPanel runat="server" Title="HDD Information">
    3.                     <PanelContent>
    4.                         <p>
    5.                             <table style="width: 99%;">
    6.                                 <tr>
    7.                                     <td class="style2">
    8.                                         <asp:Label ID="Label2111111" runat="server" Text="Original HDD:"></asp:Label>
    9.                                     </td>
    10.                                     <td>
    11.                                         <asp:Label ID="lblOriginalHDD" runat="server" Text="N/A"></asp:Label>
    12.                                     </td>
    13.                                 </tr>
    14.                                 <tr>
    15.                                     <td class="style2">
    16.                                         <asp:Label ID="Label16666666" runat="server" Text="First HDD Replacement:"></asp:Label>
    17.                                     </td>
    18.                                     <td>
    19.                                         <asp:Label ID="lblFirstHddReplacement" runat="server" Text="N/A"></asp:Label>
    20.                                     </td>
    21.                                 </tr>
    22.                                 <tr>
    23.                                     <td class="style2">
    24.                                         <asp:Label ID="Label39876" runat="server" Text="Second HDD Replacement:"></asp:Label>
    25.                                     </td>
    26.                                     <td>
    27.                                         <asp:Label ID="lblSecondHddReplacement" runat="server" Text="N/A"></asp:Label>
    28.                                     </td>
    29.                                 </tr>
    30.                             </table>
    31.                         </p>
    32.                     </PanelContent>
    33.                 </juice:AccordionPanel>
    34.                 <juice:AccordionPanel runat="server" Title="Replace HDD(s)">
    35.                     <PanelContent>
    36.                        
    37.                     </PanelContent>
    38.                 </juice:AccordionPanel>
    39.                 <juice:AccordionPanel runat="server" Title="SWRL Letter">
    40.                     <PanelContent>
    41.                        
    42.                     </PanelContent>
    43.                 </juice:AccordionPanel>
    44.             </juice:Accordion>

    I can't access the label directly, so I'm trying to use the FindControl method to get it and set it's text property.

    VB.NET Code:
    1. CType(Me.accSwrlHddRepl.FindControl("lblFirstHddReplacement"), System.Web.UI.WebControls.Label).Text = "string"

    But I get a NullReferenceException. So I moved the label outside of the table and put it just inside the accordion. The same behavior occurs. Then I gave the accordion panel an ID and did FindControl there, but the same issue occurs.

    So I'm a little confused here.

    Anyone have any ideas?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: FindControl + Casting Results In NullReferenceException

    you might need to use find control, within the control, like this:

    Code:
    dim  myL as label = Me.accSwrlHddRepl.FindControl("accSwrlHddRepl").FindControl("lblFirstHddReplacement").
    if this doesnt work, first try:
    Code:
     dim  myL as label = Me.accSwrlHddRepl.FindControl("accSwrlHddRepl")
    to see if you can get a handle on the control.
    if i was able to help, rate my post!

  3. #3

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: FindControl + Casting Results In NullReferenceException

    Quote Originally Posted by jasonwucinski View Post
    you might need to use find control, within the control, like this:

    Code:
    dim  myL as label = Me.accSwrlHddRepl.FindControl("accSwrlHddRepl").FindControl("lblFirstHddReplacement").
    if this doesnt work, first try:
    Code:
     dim  myL as label = Me.accSwrlHddRepl.FindControl("accSwrlHddRepl")
    to see if you can get a handle on the control.
    So using FindControl on the Accordion and then on the AccordionPanel, still produced the error. I cast the Accordion control to a variable (as an accordion, of course, not a label) and that worked. So I'm able to get a handle to that control.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: FindControl + Casting Results In NullReferenceException

    can you add an id to

    Code:
    <juice:AccordionPanel runat="server" Title="HDD Information" id="someId">
    and then try using find control on that, then on the label? (ie
    Code:
     Me.accSwrlHddRepl.FindControl("accSwrlHddRepl").FindControl("someId").FindControl("lblFirstHddReplacement")
    if i was able to help, rate my post!

  5. #5

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: FindControl + Casting Results In NullReferenceException

    Quote Originally Posted by jasonwucinski View Post
    can you add an id to

    Code:
    <juice:AccordionPanel runat="server" Title="HDD Information" id="someId">
    and then try using find control on that, then on the label? (ie
    Code:
     Me.accSwrlHddRepl.FindControl("accSwrlHddRepl").FindControl("someId").FindControl("lblFirstHddReplacement")
    I did that. Your first example:

    Code:
    Me.accSwrlHddRepl.FindControl("accSwrlHddRepl").FindControl("lblFirstHddReplacement")
    Didn't make sense. You wanted me to call the main accordion's FinControl method to find itself and then find the label. I thought it was just a typo. So I put the ID of the panel in there. I mentioned in my first post that I gave it an ID.

    Quote Originally Posted by weirddemon
    hen I gave the accordion panel an ID and did FindControl there, but the same issue occurs.
    Code:
    <juice:AccordionPanel runat="server" Title="HDD Information" ID="pnlHddInfo">
    Code:
    CType(Me.accSwrlHddRepl.FindControl("pnlHddInfo").FindControl("lblFirstHddReplacement"), Label).Text = "eeeeeeeeeee"
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: FindControl + Casting Results In NullReferenceException

    Hi.Can say that i've used that control but assuming that it has a namespace so you can reference it i made you an example of an ajax accordion.Your problem is that you do not take the <PanelContent> into consideration.Your label resides inside the <PanelContent> so here is what you can do in Ajax accordion and i think it will work if you can reference the juice AccordionPanel to expose it's objects:
    Code:
     Dim acp As AjaxControlToolkit.AccordionPane
            acp = Me.FindControl("AccordionPane1")
            Dim lbl1 As Label = acp.ContentContainer.FindControl("label1")
            Dim str As String = lbl1.Text
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: FindControl + Casting Results In NullReferenceException

    Quote Originally Posted by sapator View Post
    Hi.Can say that i've used that control but assuming that it has a namespace so you can reference it i made you an example of an ajax accordion.Your problem is that you do not take the <PanelContent> into consideration.Your label resides inside the <PanelContent> so here is what you can do in Ajax accordion and i think it will work if you can reference the juice AccordionPanel to expose it's objects:
    Code:
     Dim acp As AjaxControlToolkit.AccordionPane
            acp = Me.FindControl("AccordionPane1")
            Dim lbl1 As Label = acp.ContentContainer.FindControl("label1")
            Dim str As String = lbl1.Text
    The Juice Accordion panel doesn't have a ContentContainer class. But I understand what you're doing there, it just doesn't seem like the Juice control has that capability. Unless I'm confused.

    If you want to test the Juice controls, it's really simple if you have NuGet for VS.

    Install-Package JuiceUI
    http://docs.nuget.org/docs/start-her...anager-console
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: FindControl + Casting Results In NullReferenceException

    Sorry i don't want to push my registry more but i could try to find out with the jquery accordion when i have the time and if i can make it work.
    Also it's not obligatory for the AccordionPanel to have the ContentContainer object it may be a different name.You said you can reference it so what do you get when you press the dot on the AccordionPanel?Does anything in there seems to be useful?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: FindControl + Casting Results In NullReferenceException

    Quote Originally Posted by sapator View Post
    Sorry i don't want to push my registry more but i could try to find out with the jquery accordion when i have the time and if i can make it work.
    Also it's not obligatory for the AccordionPanel to have the ContentContainer object it may be a different name.You said you can reference it so what do you get when you press the dot on the AccordionPanel?Does anything in there seems to be useful?
    No need to apologize. I wasn't asking you to do anything. Only mentioned that you could if you wanted.

    As for the intellisense, I forgot to mention that I did check it out.

    I asked the same question at SO, but added details I neglected here.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: FindControl + Casting Results In NullReferenceException

    Ok i feel better now :P
    Hey can you tell us what exactly you need to do with the label?I was thinking that if what you try is on the client side then you porbably could work something with javascript instead of trying to hit the server.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width