nav100
nav100

Reputation: 3133

Javascript getElementByID for asp:Image returns null

I am trying to find the control and display set to "block" or "none" on onclientselectedindexchanged event of RadCombobox. It returns always null. The script and controls are in User Control of Content page. There is also Master page for this Content page. I debugged the code with Debugger statement but the control has this tag. "ctl00_content2_ucControl1_imgTest". How can show and hide image? Please let me know. Thanks for your help. Also I tried to use document.getElementById("<%=imgTest.ClientID"); and $find(("<%=imgTest.ClientID") ; but none of these working.

<asp:Image ID="imgTest" ImageUrl="../../../images/test.gif" AlternateText="test"
                            runat="server" style="display:none"></asp:Image>

<telerik:RadComboBox ID="Combobox1" runat="server" DataTextField="test1"
                            DataValueField="test_id" NoWrap="true" Width="250" onclientselectedindexchanged="OnClientSelectedIndexChanged">                        </telerik:RadComboBox>


<script type="text/javascript">
function OnClientSelectedIndexChanged(sender, eventArgs) { 
     {
      var item = eventArgs.get_item(); 
          if(item.get_value() == "8") 
             {
            var imageControl = document.getElementById('imgTest');
            imageControl.style.display = "block"; 
            }
        }
</script>

Upvotes: 2

Views: 1466

Answers (2)

lucuma
lucuma

Reputation: 18339

If you are on .net 4.0 you can set the ClientIDMode='Static' and then your code should work fine as intended as long as you aren't in a repeatable element.

Here is some more info on how to use the ClientIDMode: http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

Upvotes: 1

Chris Gessler
Chris Gessler

Reputation: 23113

imgTest is a server control, so the client id will be automatically generated by the server.

Change this line:

var imageControl = document.getElementById('imgTest'); 

to:

var imageControl = document.getElementById('<%=imgTest.ClientId%>'); 

The issue with your previous attempts was the missing end tag %>

Upvotes: 2

Related Questions