javascriptasp.netvb.netpostbackclientscript

Issue with Postback and ScriptManager.RegisterStartupScript not working


I am using ASP.net with VB.net code behind.

The page is successfully using ScriptManager.RegisterStartupScript to execute a Javascript function from multiple asp:DropdownList OnSelectedIndexChanged events. Each dropdown has the property AutoPostBack="True". In these cases, the code behind and the ScriptManager both execute flawlessly.

I needed to add (3) asp:RadioButtons. Each using OnCheckedChanged and with AutoPostBack="True" like the Dropdowns. Unlike the DropDowns, all the code in the code behind executes as expected except the ScriptManager. It appears to be the only thing that consistently fails and it always fails on the combination of RadioButton and OnCheckChanged.

I tried changing the position of the ScriptManager from last element of code to the first. Tried removing all the code but the ScriptManager and it still fails.

I tied my javascript function to a button so after the page rendered, I could confirm my function works. It does.

I tried adding an onclick event. Let it call the function directly with return. It works running the javascript but doesn't run the OnCheckedChange code behind. Looking like this: <asp:RadioButton runat="server" ID="radApptN" value="A" GroupName="radAppt" text=" Add New" AutoPostBack="true" checked="true" onclick="return gotoCalView();" OnCheckedChanged="radAppt_OnChanged" />

If I put the script call into the Body Onload it works but, I don't want it to fire all the time.

Thought it might have something to do with the PostBack not processing the same as OnSelectedIndexChanged. I tried the following. Didn't work.

Page.ClientScript.RegisterStartupScript(me, me.GetType(),"CallView","window.onload = function(){ gotoCalView(); }",true)

Should this not work like putting into the Body Onload call?

After reading sample posts there were a lot of questions about using an Update Panel. I am not in this page.

Any suggestions to try that maybe I missed? Feel like i hit a brick wall.


Solution

  • You have to post more code.

    ScriptManger should ALWAYS be placed right after the form tag.

    And it not clear why you using 3 RadioButtons as opposed to using a RadioButton list?

    So, as a simple markup, I have this:

    <form id="form1" runat="server">
    
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    
        <div style="padding:35px">
    
            <asp:DropDownList ID="cboHotels" runat="server"
                DataValueField="ID"
                DataTextField="HotelName"
                AutoPostBack="true"
                OnSelectedIndexChanged="cboHotels_SelectedIndexChanged"
                >
            </asp:DropDownList>
    
            <asp:RadioButton ID="RB1" runat="server" 
                Text="RB1" GroupName="BT" AutoPostBack="true"
                OnCheckedChanged="RB1_CheckedChanged"
                />
            <asp:RadioButton ID="RB2" runat="server" 
                Text="RB2" GroupName="BT" AutoPostBack="true"
                OnCheckedChanged="RB2_CheckedChanged"
                />
    
            <asp:RadioButton ID="RB3" runat="server" 
                Text="RB3" GroupName="BT" AutoPostBack="true"
                OnCheckedChanged="RB3_CheckedChanged"
                />
    
            <script>
    
                function myfun(sValue) {
                    alert('this is the client side script from ' + sValue)
                }
    
            </script>
        </div>
    </form>
    

    Keep in mind that page load always runs fire before the event code runs. This is even the case when using an update panel.

    So, as always, any setup code, combo box loading code, loading of controls etc.?

    Well, such code can be placed in the load event, but that load event runs each time on each post back.

    Thus, such setup and loading code has to be placed in a "if not postback" code stub. If I let say the code to load the combo box up run each time in page load, then I will lose the user's selection in the combo box each time, since I am reloading the values each time. So, keep in mind that you can't actually building a working web page by omitting the use of the if not IsPostBack code stub in the forms load event.

    So, code behind is this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
            cboHotels.DataSource =
                MyRst("SELECT ID, HotelName FROM tblHotelsA ORDER BY HotelName")
            cboHotels.DataBind()
            cboHotels.Items.Insert(0, New ListItem("Select Hotel", "0"))
        End If
    
    End Sub
    
    Protected Sub RB1_CheckedChanged(sender As Object, e As EventArgs)
    
        Dim sJava As String = "myfun('RB1')"
        ScriptManager.RegisterStartupScript(Page, Page.GetType, "myftest", sJava, True)
    
    End Sub
    
    Protected Sub RB2_CheckedChanged(sender As Object, e As EventArgs)
    
        Dim sJava As String = "myfun('RB2')"
        ScriptManager.RegisterStartupScript(Page, Page.GetType, "myftest", sJava, True)
    
    End Sub
    
    Protected Sub RB3_CheckedChanged(sender As Object, e As EventArgs)
    
        Dim sJava As String = "myfun('RB3')"
        ScriptManager.RegisterStartupScript(Page, Page.GetType, "myftest", sJava, True)
    
    End Sub
    
    Protected Sub cboHotels_SelectedIndexChanged(sender As Object, e As EventArgs)
    
        Dim sJava As String = "myfun('Combo box')"
        ScriptManager.RegisterStartupScript(Page, Page.GetType, "myftest", sJava, True)
    
    End Sub
    

    And the result is now this:

    enter image description here

    So far, based on what you provided, as the above shows, it works just fine.

    I thus have to assume we are missing some additional details here, or perhaps an update panel is being used here?

    However, I don't see any need to use the window.onload event, since when you register a startup script, then you only need to have the JavaScript code, and no window.on load is required for the injected script code to run after the page is sent back to the client side and rendered.