0

I have an asp dropdownlist with a few selections and when I select a certain item from the list I want a javascript modal to open up. I have successfully been able to open the modal by using an html button after selecting the specific item from the drop down, but I want one less step.

Here is my code for the index changed event on the Drop down:

protected void ShipTo_Changed(object sender, EventArgs e)
{
    foreach (DataListItem dli in cart.Items)
    {
        DropDownList drpShipto = (DropDownList)dli.FindControl("drpShipto");
        if (drpShipto.SelectedItem.Text == "-Add New ShipTo-")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "function", "showDialog('newShipTo')", true);
        }
    }
}

and here is what I have for the js :

<script>
$(document).ready(function () {
    $('#newShipTo').dialog({
        autoOpen: false,
        draggable: true,
        title: "Add New ShipTo",
        open: function (type, data) {
            $(this).parent().appendTo("form");
        }
    });

    $('#editShipTo').dialog({
        autoOpen: false,
        draggable: true,
        title: "Edit ShipTo",
        open: function (type, data) {
            $(this).parent().appendTo("form");
        }
    });
});

function showDialog(id) {
    $('#' + id).dialog("open");
}

function closeDialog(id) {
    $('#' + id).dialog("close");
}
</script>

I know the function works, as I can swap out the call to the function to making a button visible that with the onclick set to showDialog('newShipTo')

Am I just not calling it correctly using the RegistarStartupScript

2
  • Could you make a fiddle of this? Or otherwise show the HTML. My guess is you need to have an on change event in the HTML drop down to trigger your function. Commented Oct 16, 2014 at 14:10
  • use the onchange javascript call function handle opening your dialog based on criteria of the selected dropdown see stackoverflow.com/a/12517284/941398 Commented Oct 16, 2014 at 14:11

2 Answers 2

2

In your drop down, add an onchange event calling showDialog('newShipTo').

Sign up to request clarification or add additional context in comments.

2 Comments

while this works, it also calls the function on every item, not the specific item in the list. That is why I am trying to call the function from the codebehind so I can easily determine what item is being selected.
Then you need to add the logic to the showDialog function. Hard to solve without a working fiddle. But what does console.log('type',type) and console.log('data',data) show you? Probably something in there that you can say "if not the value I want, do nothing"
0

Please refer the below URL

http://dotnetgallery.com/forum/thread127-Calling-Javascript-function-from-an-asp-dropdownlist.aspx

Regards.

2 Comments

Link only answers are generally discouraged. Consider adding information about the solution into the answer here.
Also, where you have //showDialog('newShipTo'); //Call your modal popup window here thats what this question is about, the rest of my code is working. My question is more about why Page.ClientScript.RegisterStartupScript(this.GetType(), "function", "showDialog('newShipTo')", true); isn't working

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.