[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 989
  • Last Modified:

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...?
0
tbaseflug
Asked:
tbaseflug
  • 7
  • 7
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now