Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-18
3
Medium Priority
?
342 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

Hire Technology Freelancers with Gigs

Work with 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

610 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