How to include another web file in C# ASP.NET (VS 2010) - c#

I'm trying to include a common navigation page into each aspx page. The code looks something like this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="canada_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
Response.WriteFile("../include/navigation.aspx");
%>
</div>
</form>
Here is the navigation.aspx code:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlSites" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSites_SelectedIndexChanged">
<asp:ListItem Selected="True" Text="Global websites" Value="" />
<asp:ListItem Text="Australia" Value="" />
<asp:ListItem Text="Canada" Value="" />
<asp:ListItem Text="Ireland" Value="" />
<asp:ListItem Text="Japan" Value="" />
<asp:ListItem Text="Latin America and the Caribbean" Value="" />
<asp:ListItem Text="Middle East" Value="" />
<asp:ListItem Text="New Zealand" Value="" />
<asp:ListItem Text="Portugal" Value="" />
<asp:ListItem Text="Singapore" Value="" />
<asp:ListItem Text="Spain" Value="" />
<asp:ListItem Text="United Kingdom" Value="" />
<asp:ListItem Text="United States" Value="" />
</asp:DropDownList>
The dropdownlist is not displaying in the browser. I know one way is to use a Master page (which I plan on doing one day), but for this project, I would like to do something simple like this -- it's crude but functional.
Thanks for your help!

Take a look at using User Controls: ASP.NET User Controls Overview (MSDN).
Quote from the MSDN page:
User controls are substantially easier
to create than custom controls,
because you can reuse existing
controls. They make it particularly
easy to create controls with complex
user interface elements.
Also, take a look at this MSDN link: How to: Include a User Control in an ASP.NET Web Page
You would create your User Control (.ascx) and include it as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="canada_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%# Register TagPrefix="uc" TagName="uc1" Src="~/myUserControl.ascx" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:uc1 id="myUC" runat="server" />
</div>
</form>

what you want is an old-school server-side-include
code:
<html>
<body>
<!-- #Include virtual=".\include\header.inc" -->
Here is the main body of the .aspx file.
<!-- #Include virtual=".\include\footer.inc" -->
</body>
</html>

Perhaps an easy way to achieve this would be to use a User Control.

Related

Class attribute with two double quotes

I am adding list items to a Bulleted List control.
In the list item i wanted to have the attribute class="" coded.
See code below. class="" is converted to class and the right side ="" is truncated.
Its very important to note that this happens when the scriptmanage/updatepanel is used in the code. otherwise, it seems to be fine.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManagerMain" runat="server" />
<asp:UpdatePanel ID="UpdatePanelMain"
runat="server">
<ContentTemplate>
<asp:BulletedList ID="BulletedList1" runat="server" DisplayMode="LinkButton">
<asp:ListItem Text="One" Value="1" class="active"></asp:ListItem>
<asp:ListItem Text="Two" Value="2" class=""></asp:ListItem>
<asp:ListItem Text="Three" Value="3" class=""></asp:ListItem>
</asp:BulletedList>
<asp:PlaceHolder ID="myplaceHolder" runat="server">
<asp:BulletedList ID="BL_Seasons" runat="server" DisplayMode="LinkButton">
</asp:BulletedList>
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Try
class="<%= 1<2?"":"something is wrong"%>"
This should look like class="" in browser.
But some more appropriate thing is to use a different quotation on sides:
class='<%= 1<2?"":"something is wrong"%>'

Can't add button to aspx page

I tried the following code to the Webform.aspx page:
<asp:Button ID="btnShowAssignLecturer" runat="server"
onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />
It says: System.Web.HttpException: Control 'ctl00_head_btnShowAssignLecturer' of type 'Button' must be placed inside a form tag with runat=server.
When I do that, I get another error. Not quite sure what to do.
Master page code:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="AllsWellHospital.Front_End.MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<h1>All Wells Hospital</h1>
<p>
<asp:Label ID="DateDisplay" runat="server"></asp:Label>
</p>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
<link id="Link1" href="/Styles/StyleSheet2.css" runat="server" rel="stylesheet" type="text/css"/>
<style>
body
{
background-color:#d0e4fe;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="topContent">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="WebForm1.aspx" Text="Web form.aspx" Value="Upload SP10"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
</form>
</body>
</html>
Webform.aspx code:
<%# Page Title="" Language="C#" MasterPageFile="~/Front_End/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AllsWellHospital.Front_End.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium"
Text="Course Report"></asp:Label>
<asp:Button ID="btnShowAssignLecturer" runat="server"
onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />
</asp:Content>
Your button needs to be within a form tag and currently it isn't. Do you really want to place the button within the head portion of your form? Why not just add another content area for your main content. Try doing something like this:
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="AllsWellHospital.Front_End.MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<h1>All Wells Hospital</h1>
<p>
<asp:Label ID="DateDisplay" runat="server"></asp:Label>
</p>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
<link id="Link1" href="/Styles/StyleSheet2.css" runat="server" rel="stylesheet" type="text/css"/>
<style>
body
{
background-color:#d0e4fe;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="topContent">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="WebForm1.aspx" Text="Web form.aspx" Value="Upload SP10"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</form>
Your aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Front_End/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AllsWellHospital.Front_End.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium"
Text="Course Report"></asp:Label>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server"
onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />
</asp:Content>
A button must be placed inside of a server managed <form> tag in order for the post to actually do anything in the ASP pipeline.
However, your content place holder Content1 is inside the <head> tag of the master page, where <form> is invalid. You need to rethink your master page so that this content does not live inside of the <head> element, and include a <form runat='server'> surrounding your form.
You may have meant to use the HTML 5 <header> element, which is distintly different than the normal <head> element.

Download a Word editable document from an aspx page

I currently have an aspx page returning a html form (with images and css). I would like get a download Word editable version of this form without rewrite all the code. Is it possible ?
An example : I Have a page 'WebForm2.aspx' with a simple content like this.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="WebApplication15.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Title</h1>
<asp:CheckBox runat="server" ID="myTest" Text="Doc 1" /><br />
<asp:CheckBox runat="server" ID="myTest2" Text="Doc 2" /><br />
<asp:CheckBox runat="server" ID="myTest3" Text="Doc 3" /><br />
<asp:CheckBox runat="server" ID="myTest4" Text="Doc 4" /><br />
</div>
</form>
</body>
</html>
I would like that from an other page for example a button that lets you download the contents of 'WebForm2.aspx' in an editable Word document.
I Add this in Page_PreRender method
Response.AddHeader("Content-Type", "application/msword");
It's partially work. If I save the document css and images aren't loaded but if I only open the downloadable content css and images are loaded.
MS Word can edit html document so by adding Content-Type=application/msword record in header will make browser open your page in browser unfortunately without css and images.
Add following code in prerender event on your page
Response.AddHeader("Content-Type", "application/msword");

Can some one help me regarding progress bar

I used this example from here
http://www.asp.net/ajaxlibrary/jqueryui_progressbar.ashx
Every thing works fine except the progress
This is my design
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default15.aspx.cs" Inherits="Default15" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css"
rel="Stylesheet" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.min.js"></script>
<script type="text/javascript">
$("#progress").progressbar({
value: document.getElementById('<%=litprogress.ClientID%>').value
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Wizard progress</h1>
<div id="progress">
<asp:Literal ID="litprogress" runat="server"></asp:Literal>
</div>
<asp:Panel ID="step1" runat="server">
<h1>
Step 1</h1>
<asp:Button ID="GoToStep2" Text="Next" runat="server" OnClick="GoToStep2_Click" />
</asp:Panel>
<asp:Panel ID="step2" runat="server">
<h1>
Step 2</h1>
<asp:Button ID="GoToStep3" Text="Next" runat="server" OnClick="GoToStep3_Click" />
</asp:Panel>
<asp:Panel ID="step3" runat="server">
<h1>
Step 3</h1>
<asp:Button ID="GoToStep4" Text="Next" runat="server" OnClick="GoToStep4_Click" />
</asp:Panel>
<asp:Panel ID="step4" runat="server">
<h1>
Completed</h1>
</asp:Panel>
</div>
</form>
</body>
</html>
Instead of required I am getting this
So can any one tell where I went wrong
Getting some warnings regarding CSS and the error is as follows
Error: document.getElementById("litprogress") is null
I think that the progress bar init is happening before the DOM is fully loaded, put the code inside $(document).ready.

ASP.NET and AJAX library - Extremely slow !

So, i'm new to ASP.NET and AJAX.
I am trying out the Beta library.
I setup a page and was using the Editor.
The loading of this page is well, slow.
There is nothing more than then, you can see it here
http://eski.internet.is/default.aspx but it will take a min to load.
Whats is the reason for the slow load, is it the AJAX library ?
Its about 7 mb, the .dll's. Is it downloading it everytime you load the page ?
No, it doesn't load the whole 7mb worth of .dlls - that's the code that generates the output.
For that site I'm getting this from YSlow:
HTTP Requests - 46
Total Weight - 304.9K
1 HTML/Text 121.9K
4 JavaScript File 161.7K
3 Stylesheet File 6.4K
38 Image 14.7K
Which isn't that much. It did seem to take a LONG time for the host to respond, however. What are the specs on your server and its internet connection?
In your web.config do you have debug="true"? If so, take that out as it can cause pages to take longer as it then generates debug information.
It doesn't seem to be hanging on any one component being served to the client. It seems to be either server load or something in your code. Can you provide the code that you are using to better help diagnose this?
This is the code that website http://eski.internet.is/default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ScriptMode="Release"></asp:ToolkitScriptManager>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropShadowExtender ID="TextBox1_DropShadowExtender" runat="server"
Enabled="True" TargetControlID="TextBox1">
</asp:DropShadowExtender>
<asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server"
Enabled="True" TargetControlID="TextBox1">
</asp:CalendarExtender>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="To Editor" />
<br />
<br />
<cc1:Editor ID="Editor1" runat="server" Width="500" />
<br />
<asp:Button ID="Button2" runat="server" Text="To Textbox"
onclick="Button2_Click" />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server" Height="161px" TextMode="MultiLine"
Width="600px"></asp:TextBox>
</form>
</body>
</html>

Categories

Resources