<% if (Session["desig"].ToString() == "Developer"){%>
<td>
<select name='zone' id="zone" onchange="showLoc(this.value,'mlodg_loc')">
<option value="Select Zone">Select Zone</option>
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South1">South1</option>
<option value="South2">South2</option>
<option value="South3">South3</option>
</select>
</td>
<%}
else
{%>
<td>
<select name='zone' id="Select1" onchange="showLoc(this.value,'mlodg_loc')">
<option value="Select Zone">Select Zone</option>
<option value="<%#Session["zone"]%>"><%# Session["zone"].ToString() %></option>
</select>
</td>
<%}%>
the code above is working fine if I am writing it directly, I tried to write this code in other file and in main file i did Response.WriteFile("zone.aspx")
How can i include it is there any way to include and also would like to know a better way to write the above statements.
Thankyou
UserControls
are there in asp.net for this purpose, make a .ascx file for this piece of code u have, and use it as a control anywhere.
Response.WriteFile
just writes the filecontents into the Http response stream, without parsing it.
Although it is possible to use Server Side includes in asp.net (<!--#include file="xxx.ext" -->
), this has an asp-classic code smell to it IMO.
The better way to get re-use of .aspx
components is by using a User Control (.ascx).
Similar question here
Update
Response.WriteFile
is fine for plain Html, .css or .js, but not for code containing c# or referencing .Net objects (you've used Session
in your code), e.g.
Response.WriteFile("HtmlSnippet.html")
where HtmlSnippet.html
is
<p>In breaking news, scientists have discovered a planet in
<a href='http://en.wikipedia.org/wiki/Alpha_Centauri_Bb'>Alpha Centauri</a>
</p>
Using a Server Side Include, e.g. <!--#include file="file.inc" -->
would allow you to do put something like this in file.inc:
<% if (Session["desig"].ToString() == "Developer"){ %>
You are a Developer!!
<% } %>
However, the recommendation is to use a User Control (.ascx
), which allows you to do much the same as the server side include, but just in a much more reusable, object oriented, and testable fashion. Your reusable Control will be a first class object, with properties, methods and be able to raise events. You'll need to do some research here.
Response.WriteFile does not process the code on the server side. It simply takes the contents of html and pipes it to the browser.
If your code contains code blocks that must be processed by the server you can use this handy feature:
<div ID="menuContent" runat="server">
<!-- #Include virtual="/menu.aspx" -->
</div>
In my menu.aspx file I have raw html and some C# codeblocks and ASP will resolve those after inserting the content into the page. Great huh?