How to access .net classes at design time view in ASP .Net - asp.net

Can you please tell me how to refer to .net classes at design time. For e.g I want to print the current datetime in a label or text box. I tried following code but my page is coming blank.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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>
<label runat="server"><%# DateTime.Now.ToString() %></label>
<asp:TextBox runat="server" ID="litDateNow" Text='<%# DateTime.Now.ToString() %>'></asp:TextBox>
</div>
</form>
</body>
</html>

Consider that you want to use .net classes, in any part of ASP.net page you can use <%= your classes %> in top of any Asp.net page you can see a
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %>
so your Page declerative is using this pattern too

That should be:
<label runat="server"><%= DateTime.Now.ToString() %></label>

<%= DateTime.Now.ToString() %>
You can use the above syntax
The syntax you have defined is used for databinding, not printing text

If you just want to display date from aspx page, write
<div>
<%= DateTime.Now.ToLongTimeString()%>
</div>
No need to use any textbox or label control, if it is of no use in code-behind

Related

Control directive is not allowed in .aspx file

I am trying to follow the Microsoft instructions on how to create a user control
my aim is to convert the .aspx file
<%# Page Language="C#" Title="Customize the queue UI" %>
<%# Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="Form1" runat="server">
<CuteWebUI:Uploader ID="uploader1" runat="server">
</CuteWebUI:Uploader>
</form>
</body>
</html>
I tried the following
<%# Control CodeBehind="kgfileUploader.aspx.cs" Inherits="SampleUserControl" Language="C#" %>
<%# Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader, Version=3.0.0.0, Culture=neutral, PublicKeyToken=bc00d4b0e43ec38d" %>
<h3> <u>User Control</u> </h3>
<CuteWebUI:Uploader ID="uploader1" runat="server">
</CuteWebUI:Uploader>
But I get an error "Control directive is not allowed in .aspx file"
Where have I gone wrong?

i'm trying to understand how asp.net skin works

I've just created a label in default.aspx page and created a skin in skinfile.skin. I've put the skin file in App_Themes folder, i've included the reference to that file in the < link > tag and i've mentioned the skin id for that label.
so according to the code, the label should have background as black and text as orange, right? But nothing's happening.
What i'm doin wrong here?
Here's the code:
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
<link rel="stylesheet" type="text/css" href="App_Themes/SkinFile/SkinFile.skin" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" SkinID="LabelSkin"></asp:Label>
</div>
</form>
</body>
</html>
SkinFile.skin:
<asp:Label runat="server" SkinID="LabelSkin" BackColor="Black" Color="Orange">
</asp:Label>
i got this thing fixed. all i had to do was set the 'theme' attribute in the <%# Page.... Theme= "SkinFile" %> statement as the name of the folder in which the skin file is... ^_^

Do not show image when use User Control In Asp.net

I have a user control in my project (asp.net) that have an image. It has below code:
<%# Control Language="C#" ClassName="Header" %>
<asp:Panel ID="Panel1" runat="server">
<img alt="The Night Owl"
src="../Images/bookshelf.jpg"
width="800" height="110"/>
</asp:Panel>
<asp:Panel id="menuPanel" runat="server">
Home |
Titles |
Authors |
Publishers
</asp:Panel>
I can see image in user control but when i use it in my default.aspx page, it do not show image. my default.aspx code is:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register src="~/controls/Header.ascx" tagname="Header" tagprefix="uc1" %>
<!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>The Night Owl</title>
<link href="styles.css" rel="Stylesheet" type="text/css"/>
</head>
<body>
<form id="form1" runat="server">
<uc1:Header ID="Header1" runat="server" />
<h1><span lang="en-us">Home Page</span></h1>
<asp:Panel runat="server">
Welcome to The Night Owl, where all of
your technical needs are met!
</asp:Panel>
</form>
</body>
</html>
Because of src="../Images/bookshelf.jpg" in the default.aspx page it can not be load, user controls dont reference images well unless you specify the whole path or the page and user control are in the same folder,
Your UC is in a sub folder but Default page not,
Change the src something like ~/subf/subf1/img.jpg.
If that page and UC are in a same folder your src will work.
But if they are not in the same folder you should change src as I mentioned.
There is something about Path.

how can type text in the ajax combo box

i added ajax combo box to my page, but i have 2 problems.
1- i can not type in that.
2- when i type in that , it automatically find matched words.
for example when i type "j" , it finds "jack".
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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"></asp:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend"
DropDownStyle="DropDownList"
style="display: inline;">
<asp:ListItem>masoud</asp:ListItem>
<asp:ListItem>jack</asp:ListItem>
</asp:ComboBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Is your browser Firefox? If so, you may have to change the MaxLength property of the ComboBox control to a value other than the default 0.
This link will show you how to use all of the ajax controls, look at the combo box one. And you can see which one you need then.
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ComboBox/ComboBox.aspx
That is a link to all of the ajax controls with samples.
as #CallumHolden said.. if you will set the dropdownstyle property to dropdown rather than dropdownlist it will work. this solved my problem as well.

how to make a asp.net page without any payload just a string in the sourcecode?

normaly a asp.net page looks like:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
<!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>
<asp:Label ID="Label1" runat="server" Text="my text"></asp:Label>
</div>
</form>
</body>
</html>
and then i get displayed "my text".
the problem is, now i need to make a page and just display the "my text" without the html stuff in the background. so when i look in the sourcecode it should just be my text
of course i need the asp.net in the background to change the label text.
is this possible?
Just delete everything except the page tag, and on Page_Load write my text in the response
In the aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
In the .cs:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("My text")
}
Or you can add a literal to the page (set it's value in the code behind) and remove the html stuff like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
<asp:Literal ID="literal1" runat="server" />

Resources