none
OnSelectedIndexChanged e OnChange do DropDown RRS feed

  • Pergunta

  • Pessoa boa tarde,

    Estou com um problema simples, mas não sei mais o que fazer.

    Tenho esse  dropdown, que chama o OnSelectedIndexChanged e também o onChange para que chame um js para verificar algo.

    o js esta possui os return false e true, quando retorna true o OnSelectedIndexChanged  não é ativado, ou seja não funciona e deveria funcionar já que o js retornou true.



    <asp:DropDownList ID="dpHoraFim" runat="server" Width="60px" OnSelectedIndexChanged="dpHoraFim_OnSelectedIndexChanged" AutoPostBack="true" onChange="return VerificaData();">

    Como faço para que funcione corretamente, chamar o js retorna true e executar o OnSelectedIndexChanged???

    Obrigado.
    Leandro
    quinta-feira, 11 de março de 2010 20:07

Respostas

  • Esse link fala exatamente sobre isso:


       http://forums.asp.net/p/1534480/3723272.aspx

       Uma das soluções apresentadas foi executar o "__DoPostBack()" dentro da própria função javascript.




    Veja o link abaixo:
    http://msdn.microsoft.com/en-us/library/aa479302.aspx

    "Copiei o trecho específico do seu caso" (abaixo)

    Confirmation with AutoPostBack DropDownLists

    While confirm dialog boxes are typically used when a button is clicked, they can also be used when a drop-down list is changed. For example, you might have a Web page that automatically posts back when a particular DropDownList Web control is changed. (The DropDownList Web control has an AutoPostBack property that, if set to True, causes the form to postback whenever the DropDownList's selected item is changed.)

    Intuitively you might think adding a confirm dialog box for a DropDownList is identical to adding such a dialog box for a Button Web control. That is, simply set the DropDownList's client-side onchange attribute to something like: return confirm(...);. using:

    DropDownListID.Attributes("onchange") = "return confirm(...);"
    

    Unfortunately, this won't work as desired because an AutoPostBack DropDownList's onchange attribute will be set to a bit of JavaScript that causes a postback, namely a call to the client-side __doPostBack function. When setting the onchange attribute programmatically yourself, the end result is that the rendered client-side onchange event handler has both your code and the call to __doPostBack:

    <select onchange="return confirm(...);__doPostBack(...);">
       ...
    </select>
    

    Noting this, what we really want to happen is have the __doPostBack function called if confirm returns true, because then the page will be posted back. We can accomplish this by setting the onchange event by the Attributes collection to: if (confirm(...)), which will render the following markup, which is what we are after:

    <select onchange="if (confirm(...)) __doPostBack(...);">
       ...
    </select>
    

    At first glance this will seem to have the desired effect. If a user selects a different item from the drop-down list, a confirm box appears. If the user clicks OK, the form will postback; if the user clicks Cancel, the form's postback is halted. The problem, though, is that the drop-down list retains the item the user selected to initiate the drop-down list's onchange event. For example, imagine the drop-down list loads with item x being selected, and then the user chooses item y. This will trigger the drop-down list's client-side onchange event, which will display the confirm dialog box. Now, imagine that the user hits Cancel—the drop-down list will still be selected on item y. What we want is to have the selection reverted back to item x.

    To accomplish this we need to do two things:

    1. Write a JavaScript function that "remembers" the selected drop-down list item.
    2. In the drop-down list's client-side onchange event, if the user clicks Cancel, you need to revert the drop-down list back to the "remembered" value.

    Step 1 entails creating a global script variable for the drop-down list and a function that runs when the page loads that will record the drop-down list's value. Step 2 requires changing the client-side onchange attribute for the drop-down list to look like:

    if (!confirm(...)) resetDDLIndex(); else __doPostBack();
    

    Where resetDDLIndex is a JavaScript function that reverts the drop-down list's selected value back to the "remembered" value. The client-side script for this needs to look like the following:

    <select id="ddlID" 
      onchange="if (!confirm(...)) resetDDLIndex(); else __doPostBack(...);">
       ...
    </select>
    
    <script language="JavaScript">
      var savedDDLID = document.getElementById("ddlID").value;
    
      function resetDDLIndex() {
         document.getElementById("ddlID").value = savedDDLID;
      }
    </script>
    

    This necessary script can be easily generated by creating a helper method in the ClientSidePage class.

    Public Sub ConfirmOnChange(ByVal ddl As DropDownList, ByVal message As String)
        'Register the script block
        If Not IsStartupScriptRegistered("CSP-ddl-onchange") Then
            RegisterStartupScript("CSP-ddl-onchange", _
                "<script language=""JavaScript"">" & _
                "var CSP_savedDDLID = " & _
                 document.getElementById('" & _
                 ddl.ClientID & "').value;" & vbCrLf & _
                "function resetDDLIndex() {" & vbCrLf & _
                "   document.getElementById('" & " & _
                " ddl.ClientID & "').value = CSP_savedDDLID;" & 
                vbCrLf & _
                "}" & vbCrLf & _
                "</script>")
        End If
    
        ddl.Attributes("onchange") = _
        "if (!confirm('" & message.Replace("'", "\'") & _
        "')) resetDDLIndex(); else "
    End Sub
    

    To use this, simply call this method for each AutoPostBack DropDownList on the Web page that you want to display a confirm dialog box for when its selected item changes.

    • Marcado como Resposta Leandro Molina quinta-feira, 11 de março de 2010 20:38
    quinta-feira, 11 de março de 2010 20:11

Todas as Respostas

  • Esse link fala exatamente sobre isso:


       http://forums.asp.net/p/1534480/3723272.aspx

       Uma das soluções apresentadas foi executar o "__DoPostBack()" dentro da própria função javascript.




    Veja o link abaixo:
    http://msdn.microsoft.com/en-us/library/aa479302.aspx

    "Copiei o trecho específico do seu caso" (abaixo)

    Confirmation with AutoPostBack DropDownLists

    While confirm dialog boxes are typically used when a button is clicked, they can also be used when a drop-down list is changed. For example, you might have a Web page that automatically posts back when a particular DropDownList Web control is changed. (The DropDownList Web control has an AutoPostBack property that, if set to True, causes the form to postback whenever the DropDownList's selected item is changed.)

    Intuitively you might think adding a confirm dialog box for a DropDownList is identical to adding such a dialog box for a Button Web control. That is, simply set the DropDownList's client-side onchange attribute to something like: return confirm(...);. using:

    DropDownListID.Attributes("onchange") = "return confirm(...);"
    

    Unfortunately, this won't work as desired because an AutoPostBack DropDownList's onchange attribute will be set to a bit of JavaScript that causes a postback, namely a call to the client-side __doPostBack function. When setting the onchange attribute programmatically yourself, the end result is that the rendered client-side onchange event handler has both your code and the call to __doPostBack:

    <select onchange="return confirm(...);__doPostBack(...);">
       ...
    </select>
    

    Noting this, what we really want to happen is have the __doPostBack function called if confirm returns true, because then the page will be posted back. We can accomplish this by setting the onchange event by the Attributes collection to: if (confirm(...)), which will render the following markup, which is what we are after:

    <select onchange="if (confirm(...)) __doPostBack(...);">
       ...
    </select>
    

    At first glance this will seem to have the desired effect. If a user selects a different item from the drop-down list, a confirm box appears. If the user clicks OK, the form will postback; if the user clicks Cancel, the form's postback is halted. The problem, though, is that the drop-down list retains the item the user selected to initiate the drop-down list's onchange event. For example, imagine the drop-down list loads with item x being selected, and then the user chooses item y. This will trigger the drop-down list's client-side onchange event, which will display the confirm dialog box. Now, imagine that the user hits Cancel—the drop-down list will still be selected on item y. What we want is to have the selection reverted back to item x.

    To accomplish this we need to do two things:

    1. Write a JavaScript function that "remembers" the selected drop-down list item.
    2. In the drop-down list's client-side onchange event, if the user clicks Cancel, you need to revert the drop-down list back to the "remembered" value.

    Step 1 entails creating a global script variable for the drop-down list and a function that runs when the page loads that will record the drop-down list's value. Step 2 requires changing the client-side onchange attribute for the drop-down list to look like:

    if (!confirm(...)) resetDDLIndex(); else __doPostBack();
    

    Where resetDDLIndex is a JavaScript function that reverts the drop-down list's selected value back to the "remembered" value. The client-side script for this needs to look like the following:

    <select id="ddlID" 
      onchange="if (!confirm(...)) resetDDLIndex(); else __doPostBack(...);">
       ...
    </select>
    
    <script language="JavaScript">
      var savedDDLID = document.getElementById("ddlID").value;
    
      function resetDDLIndex() {
         document.getElementById("ddlID").value = savedDDLID;
      }
    </script>
    

    This necessary script can be easily generated by creating a helper method in the ClientSidePage class.

    Public Sub ConfirmOnChange(ByVal ddl As DropDownList, ByVal message As String)
        'Register the script block
        If Not IsStartupScriptRegistered("CSP-ddl-onchange") Then
            RegisterStartupScript("CSP-ddl-onchange", _
                "<script language=""JavaScript"">" & _
                "var CSP_savedDDLID = " & _
                 document.getElementById('" & _
                 ddl.ClientID & "').value;" & vbCrLf & _
                "function resetDDLIndex() {" & vbCrLf & _
                "   document.getElementById('" & " & _
                " ddl.ClientID & "').value = CSP_savedDDLID;" & 
                vbCrLf & _
                "}" & vbCrLf & _
                "</script>")
        End If
    
        ddl.Attributes("onchange") = _
        "if (!confirm('" & message.Replace("'", "\'") & _
        "')) resetDDLIndex(); else "
    End Sub
    

    To use this, simply call this method for each AutoPostBack DropDownList on the Web page that you want to display a confirm dialog box for when its selected item changes.

    • Marcado como Resposta Leandro Molina quinta-feira, 11 de março de 2010 20:38
    quinta-feira, 11 de março de 2010 20:11
  • Kaue muito obrigado pelo retorno, perfeito esse link e o que posto, é exatamente isso.

    Obrigado.
    Leandro
    quinta-feira, 11 de março de 2010 20:38