0

I have a working dropdown box that gives the answers yes & no. When "yes" is selected I need to create a message box that displays a simple message and allows the user to click ok, to get back to the survey.

I have been working with it, and tried several things but no luck. What would the code look like, and where exactly would I place it to fire at the right time. I am working in VB, with an aspx & aspx.vb page. Thanks in advance.

3 Answers 3

1

one sort of classic way to do this is add the onchange attribute to the DropDownList

Dim message As String = "Custom Message"
DropDownList1.Attributes.Add("onchange", "if (this.value === 'yes') alert('" + message + "');")
Sign up to request clarification or add additional context in comments.

Comments

0

Remember that message boxes on the web are a client technology, so you'll have to do this via javascript, using the alert() function.

6 Comments

ok, but how do I get it to trigger when the user chooses "yes" in the drop down? Also, this is an aspx page, what will the code look like to implement javascript?
mdmullinax has probably the most straightforward answer to accomplish this.
Yes most straight forward but inline javascript, just like inline CSS, should be avoided. Check out this link for reasons why. robertnyman.com/2008/11/20/…
@anothershrubery: You're right - personally, I'd hook up the change event to the function in the document.ready, using jQuery, like $("#<%: ddlQuestion.ClientID %>").change(questionChanged);, then a function like: function questionChanged() { if ($(this).val() == "yes") { alert("Some Message"); } }. But the idea is pretty much the same.
Why use jQuery, a javascript library, when you can do it in javascript in a couple of lines? jQuery is great for a lot of things but would be complete overkill for something of this nature.
|
0

The below code should help you get something similar for your page.

<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">  

    </style>
    <script type="text/javascript">
        function ShowAlert()
        {
            var dropDownList = document.getElementById("DropDownList1");
            var dropDownListValue = dropDownList.value;
            if (dropDownListValue == "1")
            {
                alert('This is your message box!');
            }
        };
    </script> 
</head>
<body>
<form runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server" onchange="ShowAlert();">
        <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
    </asp:DropDownList>
</form>    
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.