ASP.NET server side access to page <% tags

Posted on 2009-02-13
Last Modified: 2012-05-06

I have used vb with my pages and generally they are setup with a code-behind page (code not on same page as html) (yes we are still using .net v1.1)

However, I received a C# page from another department and need to include it in my project. Problem isn't converting C# to's not hard. Problem is that the one page contains the html and code and they do some things that I'm not familiar with when using a code-behind setup.

For one, they change the page title in the <HEAD> do you do that when all of the tags need to be in the <FORM> tags in a code-behind setup?

Can I have one page like this when all other pages in the project are setup for the code-behind setup?

I know I can use literal controls for most of what they show using the old style <% ... %> tags...but is there a way to continue using the <% ... %> tags and have the variables within be assignable from the server-side code? For example, in the case of:

<% if ( configuredProperly ) { %>
You may logout at <a href="BLAH BLAH">BLAH BLAH</a>.
<% } %>

Is there anyway that the configuredProperly boolean can still be resolvedeven though the code has beenmoved to the code-behind page?



<%@ Page Language="C#" Debug="true" %>

<%@ assembly name="System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>

<%@ import Namespace="System.DirectoryServices" %>

<%@ import Namespace="System.Text.RegularExpressions" %>

<script runat="server">


 Change ldapUsername and ldapPassword to match

 your registered Kerberos principal and password


string ldapUsername = "";

string ldapPassword = "";


bool configuredProperly = false;

string pageTitle;

string pennKey = "UNAVAILABLE";

string pennId = "UNAVAILABLE";

void Page_Load(Object sender, EventArgs args) {

	if ( !isNullOrEmpty(Request.Headers["REMOTE_USER"]) && !isNullOrEmpty(Request.Headers["COSIGN_SERVICE"]) ) {

		configuredProperly = true;

		pageTitle = "IISCosign Is Configured Properly.";

		pennKey = Request.Headers["REMOTE_USER"];

		if ( !Regex.IsMatch(pennKey, @"^[A-Za-z][A-Za-z0-9]{1,7}$") ) {

			pennId = "[Test skipped. PennKey unsafe for use in LDAP query.]";



		if ( isNullOrEmpty (ldapUsername) || isNullOrEmpty(ldapPassword) ) {

			pennId = "[Test skipped. LDAP credentials not set.]";



		try {

			DirectoryEntry entry = new DirectoryEntry("LDAP://BLAH BLAH");

			entry.AuthenticationType = AuthenticationTypes.Encryption;

			entry.Username = ldapUsername;

			entry.Password = ldapPassword;

			DirectorySearcher searcher = new DirectorySearcher(entry);

			searcher.Filter = "pennname=" + Request.Headers["REMOTE_USER"];

			SearchResult result = searcher.FindOne();

			if (result != null ) {

				ResultPropertyCollection coll = result.Properties;

				pennId = result.Properties["pennid"][0].ToString();



		catch (Exception e) {

			pennId = "[Exception occurred during retrieval. LDAP username and password likely unset.]";



	else {

		pageTitle = "IISCosign Is MISCONFIGURED.";



bool isNullOrEmpty ( string str ) {

	return (str == null || str.Length == 0);





<title><%= pageTitle %></title>



<h2><%= pageTitle %></h2>



	Response.Write("PennKey: " + Server.HtmlEncode( pennKey ) + "<br/>");

	Response.Write("PennID: "  + Server.HtmlEncode( pennId  ) );



<% if ( configuredProperly ) { %>

You may logout at <a href="BLAH BLAH">BLAH BLAH</a>.

<% } %>


<table border="1">









foreach ( string headerKey in Request.Headers ) {


	Response.Write("<td>" + Server.HtmlEncode( headerKey                  ) + "</td>");

	Response.Write("<td>" + Server.HtmlEncode( Request.Headers[headerKey] ) + "</td>");









Open in new window

Question by:georgeb3
    LVL 48

    Accepted Solution

    Do you assign a value to that variable in the page load event ? You just need to declare the variable as public in the code-behind.
    LVL 1

    Author Comment

    Interesting...I'll give that a try.
    LVL 1

    Author Comment


    that worked like a charm...can't believe I was so stupid.

    Thanks again!
    LVL 1

    Author Closing Comment

    LVL 48

    Expert Comment

    Glad I could help!

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    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…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    732 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

    20 Experts available now in Live!

    Get 1:1 Help Now