Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Javascript functions in an asp.net page

Status
Not open for further replies.

timothy

Programmer
Mar 20, 2000
39
US
Pulling my hair out on this one. I want to run a simple js function when a user selects an item from a drop down (I'm sure this is an ASP.NET issue).

This is my Javascript function:
Code:
function SetupUserType(UserType){
    if(UserType=='Customer'){
        lstNPCs.Enable=false;
        lstRoles.Enable=true;
    }else{
        lstNPCs.Enable=true;
        lstRoles.Enable=false;
    }
}

My call to the (js) file that contains the function
Code:
<script language="javascript" src="Local.js"></script>

My drop down listbox that contains the event:
Code:
<asp:dropdownlist OnSelectedIndexChanged="SetupUserType(this)" id="ddlUserType" runat="server">
<asp:ListItem Value="Customer" Selected="True">Customer</asp:ListItem>
<asp:ListItem Value="Employee">Employee</asp:ListItem>
</asp:dropdownlist>

This is the error (right at compile):
Code:
Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0117: 'ASP.Administrator_aspx' does not contain a definition for 'SetupUserType'

Source Error:
Line 30:
<asp:dropdownlist OnSelectedIndexChanged="SetupUserType(this)" id="ddlUserType" runat="server">
Line 31: <asp:ListItem Value="Customer" Selected="True">Customer</asp:ListItem>
Line 32: <asp:ListItem Value="Employee">Employee</asp:ListItem>

Any ideas? Let me know thanks.
 
Add the js event handler in the code behind:
Code:
in the Page_Load:
if(ddlUserType.Attributes["onchange"] == null)
{
  ddlUserType.Attributes.Add("onchange", "javascript:SetupUserType(this)");
}
page html (remove the OnSelectedIndexChanged):
Code:
<asp:dropdownlist id="ddlUserType" runat="server">
This should do it.
 
That's exactly what I was looking for. Thanks

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top