Home > DeveloperSection > Forums > Show Confirmation Message Yes or No in Asp.Net
Maria Susan

Total Post:29

Points:107
Posted on    December-21-2014 10:25 PM

 ASP.Net C# 
Ratings:


 2 Reply(s)
 595  View(s)
Rate this:

I have drop-down box where users can select Yes or No, if user selects Yes from the drop-down then i want to show a confirmation box that shows Yes or No option. If only user selects Yes in the confirmation box then i want to proceed calling my other function which makes an update to the backend database. If user selects No in the confirmation box then i don't want to proceed and cancel the operation. Here is my drop-down code:

 

OnSelectedIndexChanged="CheckDropDownSelection"

                        runat="server" AppendDataBoundItems="true" Height="16px">

                        <asp:ListItem Text="-- Please Selet --" Value="-- Please Selet --"></asp:ListItem>

                        <asp:ListItem Text="YES" Value="YES"></asp:ListItem>

                        <asp:ListItem Text="NO" Value="NO"></asp:ListItem>

                    </asp:DropDownList>

here is my code behind:

 

protected void CheckDropDownSelection(Object sender, EventArgs e)

    {

        if (ddl_CloseTask.SelectedValue == "YES")

        {

            CloseTask();

        }

        else

        {

        }

    }

 

    protected void CloseTask()

    {

        // here is where i close the task

    }



sandy taylor
sandy taylor

Total Post:46

Points:324
Posted on    December-22-2014 5:07 AM

The code has too many Yes/No. I hope it won't confuse to user -

 

If a user selects YES in DropDownList, a Confirmation Message will be prompted.

 

If the user selects YES in Confirmation Message, DropDownList will post back to server.

 

<asp:DropDownList ID="ddl_CloseTask" Width="157px"

   AutoPostBack="true"

   OnSelectedIndexChanged="CheckDropDownSelection"

   runat="server"

   AppendDataBoundItems="true" Height="16px">

   <asp:ListItem Text="-- Please Selet --" Value="-- Please Selet --"></asp:ListItem>

   <asp:ListItem Text="YES" Value="YES"></asp:ListItem>

   <asp:ListItem Text="NO" Value="NO"></asp:ListItem>

</asp:DropDownList>

 

<script type="text/javascript">

    var selectlistId = '<%= ddl_CloseTask.ClientID %>',

        selectlist = document.getElementById(selectlistId);

 

    selectlist.onchange = function() {

        if (selectlist.options[selectlist.selectedIndex].value == "YES") {

            if (confirm("Are you sure you want to do this?")) {

                __doPostBack(selectlistId, '');

            }

        }

    };

</script>


Ankit Singh

Total Post:341

Points:2389
Posted on    December-22-2014 6:44 AM

This is how you can achieve with sever and client side code

 

attach event of client code mostly in page_load

 

yourDropDownList.Attributes["onChange"] = "jsFunction(this);";

Client script

 

function jsFunction(mlist){

  var myselect = mlist;

  if(myselect.options[myselect.selectedIndex].value == "YES")

  {

    if(confirm("Delete item!"))

    {

      $.ajax({

       type:"POST",

         url: window.location.pathname + "/CloseTask";,

           data: dataString,

          contentType:"application/json; charset=utf-8",

            dataType:"json",

         error:

                  function(XMLHttpRequest, textStatus, errorThrown) {

                         $(errorlableid).show();

                         $(errorlableid).html("Error");

                  },

         success:

             function(result) {

 

                }

         }

         }

      });

    }

  }

}

serverside code

 

[WebMethod]

protected void CloseTask()

{

    //code to close task

 

}


Don't want to miss updates? Please click the below button!

Follow MindStick