I want to change the visibility of a textbox, according to the value selected in a dropdownlist.
I have created the function like this:
function ShowGiftCardSource() {
var ddlGiftCardSource = document.getElementById('<%=ddlGiftCardSource.ClientID%>');
var txtGiftCardSource = document.getElementById('<%=txtGiftCardSource.ClientID%>');
if (ddlGiftCardSource.value == "Other") {
txtGiftCardSource.style.visibility = "visible";
txtGiftCardSource.fo开发者_Go百科cus();
}
}
In the CS Page:
ddlGiftCardSource.Attributes.Add("onChange", "OnSelectedIndexChanged();");
and in the control:
<asp:DropDownList ID="ddlGiftCardSource" runat="server" Width="151px" onChange="ShowGiftCardSource();">
But I'm getting the following error:
Microsoft JScript runtime error: Object expected
Could some one please help me to resolve it?
Change the code behind to:
ddlGiftCardSource.Attributes.Add("onChange", "ShowGiftCardSource();");
And remove the onchange
from the tag:
<asp:DropDownList ID="ddlGiftCardOccasion" runat="server" Width="151px">
The onchange
in the tag is the server side method to call.
Edit: in case you already have server side method you must first add AutoPostBack to the drop down then in the server side onchange event show the textbox:
<asp:DropDownList ID="ddlGiftCardOccasion" runat="server" Width="151px" OnChange="ShowGiftCardSource" AutoPostBack="True">
And in your C# code behind:
void ShowGiftCardSource(object sender, EventArgs e) {
//code.....
txtGiftCardSource.Visible = true;
}
And of course, get rid of the ddlGiftCardSource.Attributes.Add
line.
Maybe thats because you are using ShowGiftCardOccasion() method in onChange handler, but you method name is ShowGiftCardSource() ? Then javascript just cannot find method with correct name.
精彩评论