Take the 2-minute tour ×

When coding an Asp.Net page, you generally add a runat attribute in the aspx:


Is it possible to tell in the code behind if the user hasn't done this i.e. they only did this:

<form id="form1">

Here the form has the id "form1" but in my case I don't know this. Code behind such as this is what I am looking for:

share|improve this question
What? In the code behind? Yeah, you can easily tell. If you have access to an object called 'form1' :) I presume, though, you mean in the rendered HTML? Can you clarify? –  Noon Silk Sep 25 '09 at 2:05
You might not know the form id. See updated question. –  Petras Sep 25 '09 at 2:25
Who is writing the app? Are different people responsible for the mark up than the code-behind? Is it not possible to inspect the markup to check for the name of the form? This question, as it stands, makes no sense to me. –  tvanfosson Sep 25 '09 at 2:47

4 Answers 4

up vote 2 down vote accepted

You can only ever have one form tag with "runat=server" on it per .aspx page. All you have to do is to check to see if Page.Form is null or not. If it's null, then there's no form that has been marked to runat server.

if (Page.Form != null)


It's the runat="server" part that makes the .aspx page process an element and create a corresponding object on the server side. If a component is not running on the server, then it's not added to the page's control hierarchy.

share|improve this answer
Are you sure you have a Page as well...lol Couldn't help myself. –  rick schott Sep 25 '09 at 3:14
As much ribbing as the OP is getting, it's possible to have a null form. Consider Page classes that are constructed entirely via code, and then have an ASPX inherit from them. The base class might have virtual functionality that would rely on child classes having a form. –  womp Sep 25 '09 at 3:29

var v = this.Form.TagName; //gets the name of the form that is maked as runat.

Of course if its not maked as runat then your code behind won't run anyway...

share|improve this answer

When you code in C# or Visual Basic in the code page, you will not have access to the object that do not have the runat=server option set.

You can easily access all the controls from a page using the me.controls page or something of the sort (I don't know the exact code but it's close to this) and check the type of the control to get the form.

Why do you need to know that? If a page does not have a runat=server form, it can't really be used as a server page.

share|improve this answer

You'd be able to access the form from the codebehind:


Without the runat="server", you'd just get a compiler error.

share|improve this answer

Your Answer


