0

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.focus();
        }
    }

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

onChange event of the ddlGiftCardSource.
@Knv You can't have both the client side event and server side event, it's meaningless. See my edit.
In the server side, there is no events of it. I told about the 'onChange', which is registered while loading the page. Actually, I have put a breakpoint on 'ShowGiftCardSource()'[JavaScript] method and executed it but nothing happened, when I selected a item from the dropdown. I'm not getting, why this is happening?, if you know any alternatives please suggest me. Thank you for your response.
Sorry I completely lost you. I provided you with all the options, can't really explain any better.
1

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.

1 Comment

<asp:DropDownList ID="ddlGiftCardSource" runat="server" Width="151px" onChange="ShowGiftCardSource();">

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.