Thursday, May 15, 2014

Javascript Confirmation message on DropDownList Selected Index Changed Event


 Suppose a  user entering records in fields and before save, changed the selected DropDownList suddenly there should be a message . Otherwise the values will be lose those are loaded under   DropDownList selected index change event.I took a another textbox to hold the previous selected index value of  DropDownList .(Also we can take HiddenField)


     protected void DDlPo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDlPo.Attributes.Add("onchange", "return OnSelectedIndexChange();");
            LoadMaterialBatchDetails();//this is load method

         }





   <script language="javascript" type="text/javascript">
            function OnSelectedIndexChange() {
                  var IndexValue = document.getElementById('DDlPo');               
                  var PreviousIndexValue = document.getElementById('Txt PreviousIndexValue ').value;
                  if (IndexValue.selectedIndex != PreviousIndexValue) {
                      if (confirm("Are you sure to select another Order Number, Data will be loss for this Order Number?") == true) {
                          document.getElementById('BtnClose').click();
                          IndexValue.selectedIndex = IndexValue.selectedIndex;
                      }
                      else {
                          IndexValue.selectedIndex = PreviousIndexValue;
                      }
                }
               
            }

        </script>