How to access server control property via JavaScript?

Hi guys,'

 I need to make a Panel control visible, when user clicks on HTML button:

When I click on the button I get a JavaScript error: "Null" is null or not an object.

function showPanel()
 {
   var panel = document.getElementById("pnlAddRecord");
   panel.visible = true;
 }

<input id="Button1" style="width: 80px" type="button" value="Add Record" onclick ="showPanel()" />

How can I access Visible property of the Panel control via JavaScript?

Thank you !

But it produces an error:
alik13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChetOS82Commented:
Is "pnlAddRecord" the ID attribute on the Panel control?  If so, this ID is only for the server-side components.  The ID put in the generated HTML is totally different.  You need to pass the ClientID attribute to the Javascript function.  You could do something like this:
function showPanel(panelId)
 {
   var panel = document.getElementById(panelId);
   panel.visible = true; 
 }
 
<input id="Button1" style="width: 80px" type="button" value="Add Record" onclick ="showPanel('"& pnlAddRecord.ClientID &"');" />

Open in new window

0
Michel PlungjanIT ExpertCommented:
you mean

 panel.style.visibility = 'visible'; // opposite of 'hidden'

or

 panel.style.display = 'block'; // opposite of 'none'
0
alik13Author Commented:
I mean, that panel become visible on the form
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

alik13Author Commented:
function showPanel(panelID)
 {
   var panel = document.getElementById(panelID);
   panel.visible = true;
 }

onclick ="showPanel('& pnlAddRecord.ClientID &');" />

Still does not work.  The same error: "null" is null or not an object.

This suggestion also does not work :   panel.style.visibility = 'visible';

0
ChetOS82Commented:
I totally missed the point that mplungjan brought up.

Modify the showPanel function do help us debug this.  Let us know what it says.
function showPanel(panelID) {
     alert(panelID);
}

Open in new window

0
alik13Author Commented:
Hi Chet0S82

 the alert produces the following :  & pnlAddRecord.ClientID &

Thanks!
0
ChetOS82Commented:
Looks like you forgot to include the quotes in my original sample.  Notice that it is a single quote, then a double quote, then an ampersand.

onclick="showPanel('"& pnlAddRecord.ClientID &"');"
0
Michel PlungjanIT ExpertCommented:
Can you perhaps show the line where you have


response.write 'onclick ="showPanel('& pnlAddRecord.ClientID &');" />'

or something since that is where it for sure gets messed up.
You are mixing server and client side script in an unhealthy way
0
Michel PlungjanIT ExpertCommented:
perhaps you mean


<input id="Button1" style="width: 80px" type="button" value="Add Record"
onclick="showPanel('<%= pnlAddRecord.ClientID%>')" />
0
ChetOS82Commented:
Sorry, you are right, I thought it was a server control.
0
alik13Author Commented:
<input id="Button1" style="width: 80px" type="button"  value="Add Record"  onclick = "showPanel('"& pnlAddRecord.ClientID &"');" />

Guys, I event cannot switch to Design mode due to the following errors:

1. Cannot switch views: The attribute name and value must be separated by an equals(=) sign.

2. Validation(XHTML 1.0 Transitional): This name contains uppercase characters which is not allowed.

3. Validation(XHTML 1.0 Transitional):  Attribute 'pnlAddRecord; is not a valid attribute of element 'input'
0
Michel PlungjanIT ExpertCommented:
That is because that is not correct syntax
Did you try
<input id="Button1" style="width: 80px" type="button" value="Add Record"
onclick="showPanel('<%= pnlAddRecord.ClientID%>')" />

or whatever is similar in your version of asp instead
0
alik13Author Commented:
I just tried your code and alert produced the following output:

 "ctl00_ContentPlaceHolder1_pnlAddRecord" .

I use ASP.NET 2.0 with AJAX
0
ChetOS82Commented:
I forgot about this question.  Pretty much there now... just modify your function accordingly:
function showPanel(panelId)
 {
   var panel = document.getElementById(panelId);
   if (panel) {
       panel.style.display = 'block';
   } else {
       alert('Panel DIV not found.');
   }
 }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alik13Author Commented:
Thank you guys!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.