ClientID problem in Asp.Net 2.0

  • Thread starter Thread starter Miguel Dias Moura
  • Start date Start date
M

Miguel Dias Moura

Hello,

I am having problems in accessing controls in my Asp.Net 2.0 pages.
I know Asp.Net 2.0 generates a ClientId.

In a Javascript function I tried to hide a panel as follows:

function ChangePanel()
{
document.getElementById("<%=Panel1.ClientID %>").style.display =
"block";
}

It is not working. I tried to check the code as follows:

function ChangePanel()
{
var PanelClientID = document.getElementById("<%=Panel1.ClientID
%>").style.display = "block";
alert("Client ID:" + PanelClientID)
}

I get Null.

If I place the code <%=Panel1.ClientID %> in my HTML code I get:

ctl00_MyContentPlaceHolder_Panel1

This was what I was expecting.

I think I am using "<%=Panel1.ClientID %>" wrong in my Javascript
function.

This is driving me crazy.

Could someone, please, help me out?

Thank You,
Miguel
 
when you view source what does your javascript look like? also looking at
the source, what is the panels actual id tag?

-- bruce (sqlwork.com)
 
Hi,

The panel id tag is ctl00_MyContentPlaceHolder_Panel1.

When I view the source I see the include line that includes the
javascript file. I am using the following line to load the javascript
file at runtime in the Page_Init:

' Define and include javascript file
Dim name As String = "Inscricao"
Dim url As String = "Assets/Javascript/Subs.js"
Dim type As Type = Me.GetType()

' Get a ClientScriptManager reference from the Page class
Dim reference As ClientScriptManager = Page.ClientScript

' Check if the include script is already registered
If (Not reference.IsClientScriptIncludeRegistered(type, name)) Then
' Include javascript file
reference.RegisterClientScriptInclude(type, name, url)
End If

However I call the javascript function only when a dropdownlist is
changed.
I can't figure what the problem is.

Thanks,
Miguel
 
The panel id tag is ctl00_MyContentPlaceHolder_Panel1.
When I view the source I see the include line that includes the
javascript file. I am using the following line to load the javascript
file at runtime in the Page_Init...

If the Javascript file is just included like that, then ASP.NET won't
be preprocessing it for server tags (<%...%>), as it isn't part of a
..aspx file. You probably need to include the JS code directly in the
aspx file, or set up a global variable in there that is accessed by the
JS file when it needs the clientID.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top