Solved

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

697 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