Solved

RE: Having difficulty in getting FindControl to work in Page load Event......

Posted on 2011-02-18
3
335 Views
Last Modified: 2012-06-27
Hi,

I am having a problem in getting a "FindControl" method to work in my PageLoad event. I need to find the control that raise an event in the PageLoad event.

The FindControl is not finding anthing. I just get "Object reference not set to an instance of an object." Any ideas on what is wrong? BTW this PL event is operating within a User Control on a Page with also has a Master Page.

Please see the attached code. "cntControl" seems to stay at nothing although a value is in "ctrlname"

 ie "ctl00$MainPanelContent$ctl00$_lbtnDeleteRCe8a9bf61-f593-4be5-9085-bc74f382c52f"

Thanks in advance,

Sam
Dim ctrlname As String = Page.Request.Params.Get("__EVENTTARGET")
        Dim cntControl As Control = Nothing
        Dim strID As String = ""
        Dim lbtn As LinkButton = Nothing

        If Not (ctrlname Is Nothing) And (ctrlname <> String.Empty) Then
            cntControl = Me.Page.FindControl(ctrlname)
            lbtn = DirectCast(cntControl, LinkButton)
            If lbtn.CommandName = "AddRow" Then
                response.write("Add clicked")
            ElseIf lbtn.CommandName = "Delete" Then
                response.write("Delete clicked")
            Else
                response.write("Something Else triggered")
            End If

        Else
            response.write("No control was triggered")
        End If

Open in new window

0
Comment
Question by:SamJolly
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
agarwalrahul earned 500 total points
ID: 34924732
Write this code in LoadComplete event where you can find FindControl method will work according to me.

Dim ctrlname As String = Page.Request.Params.Get("__EVENTTARGET")
        Dim cntControl As Control = Nothing
        Dim strID As String = ""
        Dim lbtn As LinkButton = Nothing

        If Not (ctrlname Is Nothing) And (ctrlname <> String.Empty) Then
            cntControl = Me.Page.FindControl(ctrlname)
            lbtn = DirectCast(cntControl, LinkButton)
            If lbtn.CommandName = "AddRow" Then
                response.write("Add clicked")
            ElseIf lbtn.CommandName = "Delete" Then
                response.write("Delete clicked")
            Else
                response.write("Something Else triggered")
            End If

        Else
            response.write("No control was triggered")
        End If
0
 

Author Comment

by:SamJolly
ID: 34924962
Hi,

Thanks for your answer. I will not be able to use your approach since I am using a User Control which does not support the LoadComplete event as far as I can see.

Thoughts?

Thanks,

Sam
0
 

Author Closing Comment

by:SamJolly
ID: 35076401
thks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASCX file or a newer alternative? 1 34
ASP.net Have sliders change in order 2 22
C#, asp.net MVC, sql server, LINQ 3 32
cannot connect to sqlserver 8 27
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question