开发者

"Object expected" error while calling javascript function

开发者 https://www.devze.com 2023-02-13 11:04 出处:网络
I want to change the visibility of a textbox, according to the value selected in a dropdownlist. I have created the function like this:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号