0

I'm working on an old aspx project with Code Behind in VB, with a UserControl that is used on two .aspx sites.

Inside that Control, in Code Behind, is a function called RegisterScript, which creates a JS function and, you guessed it, registers this. This function then gets called when clicking radio buttons in a popup, The code is below.

My problem is on one site the function works properly, the button gets clicked and textboxes show/don't show. On the other site, clicking the radio buttons does nothing and Browser Console shows SetSearchField, the JS function, is not defined.

The UserControl is defined exactly the same way in both .aspx files, Register at the top, defined at the bottom. There's literally no difference in this aspect. I don't know what the mistake is.

The Sub RegisterScript, which creates the function:

Private Sub RegisterScript()

    Dim csm As ClientScriptManager = Page.ClientScript
    Dim cstype = Me.GetType()
    Dim csname = "SetSearchField"

    If Not csm.IsClientScriptBlockRegistered(cstype, csname) Then
        Dim script As String = ""
        script &= "<script type='text/javascript'>"
        script &= "function SetSearchField(selTextBoxId) {"
        script &= "var itTextbox;"
        script &= "itTextbox = document.getElementById('" & Me.Panel_Amount.ClientID & "');"
        script &= "itTextbox.style.display = 'none';"
        script &= "itTextbox = document.getElementById('" & Me.TextBox_SearchStringBelegNr.ClientID & "');"
        script &= "itTextbox.style.display = 'none';"
        script &= "itTextbox = document.getElementById('" & Me.TextBox_SearchStringFirm.ClientID & "');"
        script &= "itTextbox.style.display = 'none';"
        script &= "itTextbox = document.getElementById('" & Me.TextBox_SearchStringOtherAccount.ClientID & "');"
        script &= "itTextbox.style.display = 'none';"
        script &= "itTextbox = document.getElementById('" & Me.TextBox_SearchStringUsage.ClientID & "');"
        script &= "itTextbox.style.display = 'none';"
        script &= "var selElem = document.getElementById(selTextBoxId);"
        script &= "selElem.style.display = 'block';"
        script &= "selElem.focus();"

        'script &= "if (selTextBoxId=='" & Me.TextBox_SearchStringAmountFrom.ClientID & "') {"
        'script &= "document.getElementById('" & Me.TextBox_SearchStringAmountTo.ClientID & "').style.display = 'block';"
        'script &= "}"

        script &= "}"
        script &= "</script>"
        csm.RegisterClientScriptBlock(cstype, csname, script, False)
    End If

End Sub

Page_Load, where RegisterScript gets called and added to the RadioButtons:

    If Not Me.IsPostBack Then

        Me.RegisterScript()
        Me.RadioButton_Amount.Attributes.Add("onClick", "javascript:SetSearchField('" & Me.Panel_Amount.ClientID & "');")
        Me.RadioButton_BelegNr.Attributes.Add("onClick", "javascript:SetSearchField('" & Me.TextBox_SearchStringBelegNr.ClientID & "');")
        Me.RadioButton_Firm.Attributes.Add("onClick", "javascript:SetSearchField('" & Me.TextBox_SearchStringFirm.ClientID & "');")
        Me.RadioButton_OtherAccount.Attributes.Add("onClick", "javascript:SetSearchField('" & Me.TextBox_SearchStringOtherAccount.ClientID & "');")
        Me.RadioButton_Usage.Attributes.Add("onClick", "javascript:SetSearchField('" & Me.TextBox_SearchStringUsage.ClientID & "');")

Register and Definition of the UserControl on the aspx site where it's working:

<%@ Register Src="../UserControls/UCSearch.ascx" TagName="UCSearch" TagPrefix="uc4" %>
<uc4:UCSearch ID="UCSearch_Sent" runat="server" Title="Gesendete Auftträge suchen"
    EnableSearchSuborders="true" EnableSearchBelegNr="true" EnableSearchPeriode="false" />

Register and Definition on the site where it's not working:

    <%@ Register Src="../UserControls/UCSearch.ascx" TagName="UCSearch" TagPrefix="uc3" %>

I've been searching thoroughly, but haven't found anything that fits my case of working on one site, but not working on the other.

karel
  • 5,489
  • 46
  • 45
  • 50
Guddo
  • 1
  • 3
  • 1-is the any message in the browser debug ? 2-better to show HTML / JS generated into a browser (F12) – Mister Jojo Feb 20 '19 at 08:34
  • @MrJ Inspector The Inspector shows the the function with the correct parameter, Console shows ReferenceError: SetSearchField is not defined, Debugger shows this line: javascript:SetSearchField('ctl00_ctl00_ContentPlaceHolder_Body_ContentBody_UCSearch_TextBox_SearchStringFirm'); while showing undefined when hovering over both javascript and SetSearchfield. – Guddo Feb 20 '19 at 08:55
  • Why don't you want to show your generated code ? + maybe this just about browser loading sequence. – Mister Jojo Feb 20 '19 at 08:59
  • Sorry, I don't know, which generated code specifically. This is for one of the radio buttons. – Guddo Feb 20 '19 at 09:02
  • I suspect your names or id are too long ? – Mister Jojo Feb 20 '19 at 09:09
  • That doesn't explain why it's working on one site, but not on the other. IDs and names are both defined in the UserControl, which is exactly the same in both files. – Guddo Feb 20 '19 at 09:16
  • there is necessarily a difference, without seeing the code, it's impossible to guess – Mister Jojo Feb 20 '19 at 09:27

1 Answers1

0

I found the answer.

The generated JavaScript was enclosed by an If Else Statement for IsPostBack. Since one caller was an imagebutton and the other a normal button, one caused postback where the other didn't, which led to the function not being defined and registered on one of the sites. Changed the button type and it works like a charm.

Guddo
  • 1
  • 3