?
Solved

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

Posted on 2011-02-18
3
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
Rahul Agarwal earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 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