Link to home
Start Free TrialLog in
Avatar of jrmcdona
jrmcdona

asked on

accessing a div tag from the code behind

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!
Avatar of AerosSaga
AerosSaga

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
ASKER CERTIFIED SOLUTION
Avatar of shovavnik
shovavnik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of jrmcdona

ASKER

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
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.
Things are heating up! :)
No, no offense is meant.  I'm just pointing out the reasons for recommending the HtmlGenericControl instead of a Panel.
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