Solved

accessing a div tag from the code behind

Posted on 2004-08-26
8
2,172 Views
Last Modified: 2008-02-01
Is there anyway to access one of my DIV tags from the code behind page. On a certain event I want to hide a div section.

thanks for any assistance!
0
Comment
Question by:jrmcdona
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11906424
This prints a div, you should be able to modify for your needs:

<div id="printReady">
<p>Hello World! Your Printable Page Content Goes Here</p>
</div>

<script language="JavaScript">
var gAutoPrint = true; // Flag for whether or not to automatically call the print function

function printSpecial()
{
      if (document.getElementById != null)
      {
            var html = '<HTML>\n<HEAD>\n';

            if (document.getElementsByTagName != null)
            {
                  var headTags = document.getElementsByTagName("head");
                  if (headTags.length > 0)
                        html += headTags[0].innerHTML;
            }
            
            html += '\n</HE>\n<BODY>\n';
            
            var printReadyElem = document.getElementById("printReady");
            
            if (printReadyElem != null)
            {
                        html += printReadyElem.innerHTML;
            }
            else
            {
                  alert("Could not find the printReady section in the HTML");
                  return;
            }
                  
            html += '\n</BO>\n</HT>';
            
            var printWin = window.open("","printSpecial");
            printWin.document.open();
            printWin.document.write(html);
            printWin.document.close();
            if (gAutoPrint)
                  printWin.print();
      }
      else
      {
            alert("Sorry, the print ready feature is only available in modern browsers.");
      }
}

</script>

Test Link:
<a href="javascript:void(printSpecial())">Print this Page</a>  

Test Button
<form id="printMe" name="printMe">
  <input type="button" name="printMe" onClick="printSpecial()" value="Print this Page">
</form>  
 
Regards,

Aeros
0
 
LVL 8

Accepted Solution

by:
shovavnik earned 125 total points
ID: 11906578
There are two ways to hide the div.  In both cases you need to give the div an id:

1. With javascript on the client.
document.all[ 'myDiv' ].style.display = 'none';

2. In your code behind file.
Your file needs to have the runat="server" tag in your aspx file and in the code behind file you need to have a member declaration for it.

In aspx: <div id="myId" runat="server">content</div>
In Code Behind file: Dim myDiv as GenericHtmlControl

Then in your code behind file, you can set it to invisible:
myDiv.Visible = false

Note that if you set it to invisible in your code behind file it won't be accessible at all on the client-side, even from javascript.  Asp.net simply won't render it.  If you want it to be accessible, but just invisible so that you can access it with javascript on the client-side, instead of setting Visible to false, try:
myDiv.Style.Add("block", "none")
This is essentially the same as the option 1, only it's done from the server.
0
 
LVL 33

Expert Comment

by:raterus
ID: 11906845
Well that is a lot of fun looking javascript, but the author wanted to do this from codebehind. I'd use an <asp:panel> control (which will render as a <div>) and set the visible property of it to true/false.  I'm not sure why you'd go through all the fuss to make a HtmlGenericControl when you can just use a Panel.
0
 

Author Comment

by:jrmcdona
ID: 11907051
raterus, i just noticed your solution as well. thats looks like a good option to. I will try it sometime, in the meantime i already had the other route setup!

thanks all
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:shovavnik
ID: 11907147
HtmlControls use fewer resources than do WebControls.  So if the developer doesn't need all the functionality of the WebControl, then HtmlControl can be easier to work with sometimes.

Not to mention that the relationship between the presentation and the code behind is more intuitive.  The developer doesn't need to guess what the asp:Panel parses as.  Obviously, there are advantages to using the Panel WebControl, but it doesn't seem like they apply in this case.

In addition, the author initially asked how to hide the div's already present in the code.  The easiest way to use existing code is to simply add the runat="server" attribute and add the declaration in the code behind (which vs.net may do automatically anyway).  This requires the least modification to existing code.
0
 

Author Comment

by:jrmcdona
ID: 11907173
Things are heating up! :)
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11907236
No, no offense is meant.  I'm just pointing out the reasons for recommending the HtmlGenericControl instead of a Panel.
0
 
LVL 33

Expert Comment

by:raterus
ID: 11907705
An intellectual argument is always good, because the "loser" always learns something new.  I guess I've always known an HtmlGenericControl is faster to render than a Panel, even though I don't really use the HtmlGenericControl.  I like my computer to sweat it out, mwhahaha
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now