2
votes

I have a dropdown list whose auto postback is set to true and selectedindexchanged event is used for some logic. But the condition is when other data is changed on the page i need to warn user about the changes by showing a confirmation dialogue. I am using javascript to do this but the problem is post back is not happening even when the function returns true. I have no idea whats wrong. Can some body help regarding this.

function ValidateDataChange() {
        var isDataChanged = $('#<%=hdnDataIsChanged.ClientID %>').val();
        if (isDataChanged == "True") {
            alert(isDataChanged);
            return false;
        }

        return true;
    }

  <asp:DropDownList ID="ddlHierarchy" runat="server" AppendDataBoundItems="true" AutoPostBack="true"
                        onchange="return ValidateDataChange();" Enabled="false" OnSelectedIndexChanged="ddlHierarchy_SelectedIndexChanged">
                        <asp:ListItem Value="" Text="--Select Hierarchy--"></asp:ListItem>
                    </asp:DropDownList>
  <asp:HiddenField ID="hdnDataIsChanged" runat="server" />


 protected void ddlHierarchy_SelectedIndexChanged(object sender, EventArgs e)
    {
      //mycode
    }
1
why u use Enabled="false" here? is it hit on the alert? - felix Antony
it will be enabled when a search is performed on the page. - Sravan Kumar

1 Answers

3
votes

selectedIndexChanged is bound to the onchange event of your DDL client-side.

By writing

onchange="return ValidateDataChange();"

you are in fact doing :

onchange="return ValidateDataChange(); __doPostBack(this.id,'');"

Because of the return, __doPostBack will never be hit.

You should try :

onchange="if(!ValidateDataChange()) return false;"