By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

Show/hide divs according to server side variable

P: n/a
How would I show or hide a div that is using client side Javascript
based upon a server side variable?

Here are my divs :

<div id="idButton5" class="otherLeftBarLink" onmouseover="javascript:
changeStylesMouseOver('5');" onmouseout="javascript:
changeStylesMouseOut('5');" onclick="location='/AddProject.aspx'">
<div class="leftBarLinkText">
Add Project
</div>
</div>
<div id="idButton7" class="otherLeftBarLink"
onmouseover="javascript: changeStylesMouseOver('7');"
onmouseout="javascript: changeStylesMouseOut('7');"
onclick="location='/AddTask.aspx'">
<div class="leftBarLinkText">
Add Task
</div>
</div>

I want to use a variable that is being set in the Page_Load event to
determine whether I show or hide each of these divs.

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Put your variable's value in a hidden input ....
access the hidden input from javascript ...
and perform your operation

Best of luck

Munna

www.munna.shatkotha.com
www.munna.shatkotha.com/blog
www.shatkotha.com
Jun 27 '08 #2

P: n/a
How exactly would I do that?
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #3

P: n/a
In the page load event register some custom javascript to include on
the page, e.g.

protected void Page_Load(object sender, EventArgs e)
{
// build custom javascript string based on server parameter
string myJavascript = "var myParameter = '" + this.MyParameter +
"';";
// register script on page
Page.ClientScript.RegisterClientScriptBlock(
Page.GetType(), "MyScript", myJavascript, true);
// now myParameter can be used in javascript code on the page
}
private string MyParameter
{
get { ... }
}
Alternatively you could put runat="server" on the divs and set the
Visible property as you would on a server control, but that's entirly
server side, not javascript.

Terry.

On Jun 20, 12:02*pm, Mike P <mike.p...@gmail.comwrote:
How would I show or hide a div that is using client side Javascript
based upon a server side variable?

Here are my divs :

<div id="idButton5" class="otherLeftBarLink" onmouseover="javascript:
changeStylesMouseOver('5');" onmouseout="javascript:
changeStylesMouseOut('5');" onclick="location='/AddProject.aspx'">
* * * * <div class="leftBarLinkText">
* * * * * * Add Project
* * * * </div>
* * </div>
* * <div id="idButton7" class="otherLeftBarLink"
onmouseover="javascript: changeStylesMouseOver('7');"
onmouseout="javascript: changeStylesMouseOut('7');"
onclick="location='/AddTask.aspx'">
* * * * <div class="leftBarLinkText">
* * * * * * Add Task
* * * * </div>
* * </div>

I want to use a variable that is being set in the Page_Load event to
determine whether I show or hide each of these divs.

*** Sent via Developersdexhttp://www.developersdex.com***
Jun 27 '08 #4

P: n/a
I am setting runat="server" on the divs and set the
Visible property as you would on a server control, but whenever my code
calls one of my Javascript functions I get the error 'object required'
when I am passing the div id to the function changeStylesMouseOver(id).
But this works fine when I don't set runat="server".

Any ideas why?

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #5

P: n/a
On Jun 20, 2:50*pm, Mike P <mike.p...@gmail.comwrote:
I am setting runat="server" on the divs and set the
Visible property as you would on a server control, but whenever my code
calls one of my Javascript functions I get the error 'object required'
when I am passing the div id to the function changeStylesMouseOver(id).
But this works fine when I don't set runat="server".

Any ideas why?

*** Sent via Developersdexhttp://www.developersdex.com***
The server side Visible property will cause the control not to be
rendered out to the browser.

To have it there but hidden change the CSS style properties of the
control rather than the Visible property.

Terry.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.