0

I am using an external JavaScript file which will give me a message when a Dropdown list value is not selected. It is set 'Select' by default. I have tried a lot of things I found while searching, but it is not running on my site. I am using Visual studio 2013 with 4.5 frame work.

Below is the code I have tried.

function unicollege()
{

    var ddlObject = document.getElementById("<%=ddlType.ClientID%>"); 
    var selectedValue = ddlObject.options[ddlObject.selectedIndex].value; 

    var e = document.getElementById("ddlLocation");  
    var selectedLocation = e.options[e.selectedIndex].value; 

    // .option and .value property are not available in my program.

}

2 Answers 2

0

Here is what needs to be in your page to use the external javascript file:

     protected void Page_Load(object sender, EventArgs e)
     {
        ScriptManager.RegisterClientScriptInclude(
        this,
        typeof(Page),
        "",
        ResolveClientUrl("JavaScript1.js"));
     }

In your aspx markup use a variable for your ddl to reference in your external javascript file:

    <script type="text/javascript">
       var ddl = "<%=ddlList.ClientID%>";
    </script>

and in your external javascript file:

    /// <reference path="WebForm1.aspx" />

    function unicollege() {

    var ddlObject = document.getElementById(ddl);
    var selectedValue = ddlObject.options[ddlObject.selectedIndex].value;
    }
1
  • I have tried but it is not working. One more thing i would like to tell you. My Java script works if I put it inside asp.net file but when i put it in extenal JavaScript file it does not work. I hope this may help you understanding my problem.
    – Muryali
    Jun 1, 2015 at 17:22
0

you dont really need to think this much complex. i do these kind of work every day a thousand times.

function unicollege()
{
   var ddlSelectedValue = document.getElementById("ddlType").value; 
}

this i just sent u after testing..it is going to work perfectly fine and i am doing it for a long time.however be careful about the postback problems if they happen.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.