FindControl - UserControl

I have a page - which loads a usercontrol - within the usercontrol is a datalist - I have a link button in the datalist which, when clicked, loads another user control - the question that I have is - how can I get the ID of the button from the link button and reference it within the last usercontrol loaded - I know that I have to do a find control but how...?
tbaseflugAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

puterartCommented:
I'm not sure I understand the issue 100% but here it goes. If the button fires a SelectedIndexChanged event on the GridView you can get the button by doing:
LinkButton lnk = (LinkButton)gvGridView.Rows[gvGridView.SelectedIndex].FindControl("lnkLinkButton");

This should give you the actual link button. From there when you load in your next user control you can pass in the LinkButton to the user control. I usually create a LoadControl() function on the user control so in this case I would create LoadControl(LinkButton lnk) and just pass in the Link Button.

Does this accomplish what your looking for?
0
tbaseflugAuthor Commented:
I will give it a try - the only issue is that I am using a datalist - not a gridview - essentially, I load the "home page" - which loads a usercontrol that I have setup as the site navigation - via a series of linkbuttons in a datalist - when the user clicks the linkbuttons - each of the linkbuttons loads a different usercontrol specific to the content of each linkbutton- all of this works great, except the last usercontrol loaded (content) - I am unable to get the ID of the linkbutton being clicked from within the datalist - which would then filter a data query on the content page, etc...
0
puterartCommented:
Gotcha, sorry for not seeing it was a DataList and not a GridView. Obtaining the LinkButton from the DataList should be essentially the same.

LinkButton lnk = (LinkButton)dlDataList.Items[dlDataList.SelectedIndex].FindControl("lnkLinkButton")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

tbaseflugAuthor Commented:
I am using the code below and it is not finding the button
    Sub Item_Command(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
 
        ' Set the SelectedIndex property to select an item in the DataList.
        'Me.DataList1.SelectedIndex = e.Item.ItemIndex
 
        Dim lnk As LinkButton = DirectCast(DataList1.Items _
        (DataList1.SelectedIndex).FindControl("lbSubNav"), LinkButton)
 
        'Dim t As UserControl = AddControl(lnk.ID.ToString())
        'Me.nav.Value = e.Item.ItemIndex.ToString()
    End Sub

Open in new window

0
puterartCommented:
I don't think you can set the SelectedIndex, the SelectedIndex gets set when you click on a button in an ItemTemplate that has the CommandName of "Select" on it. I think that's your problem.
0
tbaseflugAuthor Commented:
well, I have that commented out
0
puterartCommented:
Sorry about that, I didn't notice the comments because I am not used to VB syntax.

I believe you need to find the button on the item not the DataList. e.Item.FindControl("lbSubNav")
0
tbaseflugAuthor Commented:
Ok - even when I use the below code - it throws - object reference not set, etc.
    Sub Item_Command(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
 
        Dim lnk As LinkButton = DirectCast(e.Item.FindControl("lbSubNav"), LinkButton)
 
        Dim strTest As String = CStr(e.CommandName)
        Me.test1.Text = lnk.Text.ToString()
 
    End Sub

Open in new window

0
puterartCommented:
You want this code to be in OnSelectedIndexChanged not OnItemCommand
0
tbaseflugAuthor Commented:
it is just weird - even when I do that I cannot find it - even when I use the below;
For i As Integer = 0 To dlNames.Items.Count - 1       
    Dim name As String = dlNames.Items(i).FindControl("MemberName").ToString() 
Next 

Open in new window

0
puterartCommented:
Try posting your DataList markup, maybe then I could identify why this isn't working for you.
0
tbaseflugAuthor Commented:
OK - I am an idiot - all of this time - and I forgot to comment out where I was setting the ID of the linkbutton dynamically in the datalist load event - doh!
0
puterartCommented:
Haha, been there a million times. Glad you got it working!
0
tbaseflugAuthor Commented:
I apprecite all of the assistance on seeing this through = thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.