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.
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.