Solved

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

Posted on 2011-02-18
3
333 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now