Foreign Key is not Filtered Automatically!

Jun 5, 2009 at 8:57 PM

I saw this problem in a couple discussions but since I couldn't easily find them again, I figured I'd start a new discussion with a different Discussion Name with the hopes that it can be found easier.

Either way, since there was no solution posted and I shoe-horned a workaround, I thought I would post my solution.

Problem: When you click on the foreign key link on a primary key page, the new filter does not detect the query string and pre-filter the page.

Solution:

1) On the ForeignKey.ascx.vb add code to read the query string and select the item in the list if it exists.

2) On the Page Load of the foreign key page
    a. Check to see if there is a query string
    b. Programmatically "Click" the "Search" button

To accomplish this, put the following code in the code behind of your ForeignKey.ascx.vb:

 Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
     PopulateListControl(DropDownList1)
     For Each s As String In Request.QueryString.AllKeys
         
Dim li As New ListItem
          li = DropDownList1.Items.FindByValue(Request.QueryString(s))
         
If li IsNot Nothing Then
              
li.Selected = True
              
Exit For
          End If
     
Next
End Sub

 On my Foreign Key page I added the following three functions (to be neat):

Private ReturnBtn As LinkButton
 'This is the function that should be called should be called
Public Function getSearchButton(ByVal contr As Catalyst.Web.DynamicData.DynamicFilterRepeater) As LinkButton
     ReturnBtn =
Nothing
     actuallyGetSearchButton(contr)
     Return ReturnBtn
End Function

 

'This code iterates through every control and its child to find the Search button
Private Sub actuallyGetSearchButton(ByVal contr As Control)
  For Each ctl As Control In contr.Controls
    
If TypeOf ctl Is LinkButton Then
         
Dim btn As LinkButton = DirectCast(ctl, LinkButton)
         
If btn.CommandName = "Search" Then
                
ReturnBtn = btn
          End If
     End If
    
If ctl.Controls.Count > 0 Then
         
actuallyGetSearchButton(ctl)
    
End If
  Next
End Sub

'This will return JavaScript that will "click" the link button on the Page Load
Public Function getJSForBtnClick(ByVal ctlClientID As String) As String
    
Return <script type='text/javascript'>
                     
var btn = document.getElementById('<%= ctlClientID %>');
                      
if(btn)
                           
btn.click();
                
</script>.ToString()
End Function

Then I add the following to the Page Load of my foreign key page. This calls the function to get the button and then hides the gridview (so it's not flashed to the user before the postback). It also sets the Javascript to click the button right after the page loads.

GridView1.Visible = True 'This will make sure it's visible if there is not a query string
If Request.QueryString.Count > 1 And Not IsPostBack Then 'There will also be 1 query string for the table
     Dim btn As LinkButton = getSearchButton(DynamicFilterRepeater1)
    
If Not btn Is Nothing Then
         
ClientScript.RegisterStartupScript(Me.GetType(), "js", getJSForBtnClick(btn.ClientID))
    
End If
    
GridView1.Visible = False
End If

Jun 16, 2009 at 8:45 PM

Thanks for the steps to get this to work.  Major pain to have to do this.

Just wanted to note that the line "btn.click();" doesn't work in firefox.  I got around it by using "document.location = btn.href;" instead.  That works in IE and Firefox, which fits my needs.