I have created a drop down list in my user control, see source code below,
<asp:DropDownList ID="ddlInd" runat="server" DataSourceID="indXmlDS" DataTextField="text" DataValueField="text"></asp:DropDownList>
In Page_load I have done this:
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "myScript", "<script language='javascript' src='../Scripts/myJS.js'></script>");
ddlInd.Attributes["onchange"] = "showTextbox()";
}
So what code should I be using, if I would like to refer to this control in my external javascript file, myJS.js?
I have tried to use document.getElementById("<%=ddlInd.ClientID %>") but it would return NULL.
Can anyone help? Thanks
EDIT:
Not sure if attaching that myJS.js file would be helpful here
function showTextbox() {
var sid = <%=ddlInd.ClientID %>;
//alert(sid);
var s = document.getElementById('<%=ddlInd.ClientID %>'); // <-- problem here
alert(s);
if (s.options[s.selectedIndex].value == "Other") {
myDiv.style.display = "inline";
alert("display");
}
else {
myDiv.style.display = "none";
alert("none");
}
}
EDIT2:
I kinda found the workaround which is to embed scripts in the user control page instead of using external script file. Thanks for the suggestions everyone. They were all very helpful.
Also the modified js script is as follows, and it works:
function showTextbox(objID) {
var s = document.getElementById(objID);
var div = document.getElementById("myDiv");
if (s.options[s.selectedIndex].value == "Other") {
div.style.display = "inline";
}
else {
div.style.display = "none";
}
}
document.getElementById("<%=ddlInd.ClientID %>"), as parameter forshowTextbox? It could also be helpful to see the relevant part of your Usercontrol whereddlIndand the TextBox are. Doesn'tshowTextboxalso needs the ID of the TextBox?