You can use java script function to show the date and time.
<script type="text/javascript">
function displayTime()
{
var localTime = new Date();
var year= localTime.getYear();
var month= localTime.getMonth() +1;
var date = localTime.getDate();
var hours = localTime .getHours();
var minutes = localTime .getMinutes();
var seconds = localTime .getSeconds();
var div=document.getElementById("div1");
div.innerText=year+"-"+month+"-"+date+" "+hours+":"+minutes+":"+seconds;
}
</script>
1.7How to display images with a delay of five seconds [top]
With this script you can see clickable images rotating in real-time without requiring banner rotation software or page reloads/refreshes .You should see a new banner rotating every 5 seconds:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>JavaScript</title>
<script language="javascript" type="text/javascript">
var image="";
var banners=0;
function loadbanners() {
if (banners==1)
{
image="images/AJAX.gif";
}
if (banners==2)
{
image="images/ASPDotNet.gif";
}
if (banners==3)
{
image=" images/MSDN.gif";
}
}
function cycle()
{
if (++banners > 3)
banners=1;
loadbanners();
document.getElementById("banner1").src =image;
window.setTimeout('cycle();',5000);
}
</script>
</head>
<body onload="cycle();">
<form id="form2" runat="server">
<div>
<img alt="" id="banner1" src="images/start.png" />
</div>
</form>
</body>
</html>
private void Button1_Click(object sender, System.EventArgs e)
{
//Drag TextBox1 and TextBox2 onto a web form
Session["name"]=TextBox1.Text;
Session["email"]=TextBox2.Text;
Server.Transfer("anotherwebform.aspx");
}
//Page1.aspx stores value in context before transferring
Context.Items("UserName") = txtName.Text;
Server.Transfer("Page2.aspx");
//Page2.aspx retrieves the value from Page1’s context string sName;
sName = Context.Items("UserName").ToString;
Response.Write("Your name is " + sName);
Please check the following article, in the article, Haissam Abdul Malak will explain how to upload multiple files in an organized way using HtmlInputFile control.
3.3Why upload fails when using an ASP.NET FileUpload control to upload large files [top]
For security reasons, ASP.NET is limited in terms of an uploaded file size. The default maximum file size is 4 MB but this can be changed by modifying the MaxRequestLength attribute of Machine.config's <httpRuntime> element.
4.2How to select multiple non-sequential dates at Code-Behind [top]
Invoke the member function ‘Add’ of the control's SelectedDates collection. You can add dates in any sequence, because the collection will automatically arrange them in order for you.
<script type ="text/javascript" >
function change()
{
var dd=document.getElementById ('<%=DropDownList1.ClientID %>');
var value=dd.options[dd.selectedIndex].value;
if(value!="2") //supose you want to disable the item numbered 2
{
setTimeout("__doPostBack('DropDownList1','')", 0);
}
}
</script>
<%@ Page Language="C#" %>
<%@ Register src="UC.ascx" tagname="UC" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnLoad_Click(object sender, EventArgs e)
{
UC uc = new UC();
uc.LoadControl("~/uc/UC.ascx");
uc.ID = "MyUC";
form1.Controls.Add(uc);
(form1.FindControl("MyUC").FindControl("txtName") as TextBox).Text = "ASP.NET";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>User Control Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnLoad" runat="server" Text="Loading user control ..." onclick="btnLoad_Click" />
</div>
</form>
</body>
</html>
6.3How to access a control inside a UserControl [top]
Assume there is a user control called UC and there is only a TextBox control inside it. Now drag this user control into a web page, you can use the following code to visit the TextBox control.
7.5Why dynamically created controls disappear after a post back [top]
The dynamic button must be re-created on each post back, so remove the if(!Page.IsPostBack). It's probably better if you create the controls at the Page_Init event.
Linked style sheet usually lives in <head> tag, but there is no need to worry if it is put inside <body> tag.As well, <head> tag must have a runat=”server” attribute.
<!--startprint-->
This area will print!
<!--endprint-->
<br />
I will not print?
<input id="btnPrint" type="button" value="Print" onclick="doPrint()" />
9.3How to print a part of a web page with JavaScript (2) [top]
JavaScript CODE:
<script language="javascript" type="text/javascript">
function printdiv(divID)
{
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = document.all.item(divID).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr+footstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>
HTML CODE:
<input name="b_print" type="button" onclick="printdiv('divID');" value=" Print " />
<div id="divID">
<h1 style="color:green">
The Div content which you want to print</h1>
</div>
10.1What classes are needed to send e-mails in ASP.NET [top]
Class MailMessage and SmtpMail are used to send emails from an ASP.NET application. MailMessage and SmtpMail are from System.Web.Mail namespace of .NET Framework 1.1 Class Library. Also, you can use System.Net.Mail instead of System.Web.Mail if you have .NET Framework version 2.0 installed.
10.2How to send emails by using System.Net.Mail [top]
CODE-BEHIND:
MailMessage message = new MailMessage();
message.From = new MailAddress("fromusername@DomainName");
message.To.Add(new MailAddress("tousername@DomainName"));
message.CC.Add(new MailAddress("ccusername@DomainName"));
message.Subject = "Subject";
message.Body = "Content";
SmtpClient client = new SmtpClient();
client.Send(message);
Please read FAQ “How to send email by using System.Net.Mail?” first. Please note that you need to be aware of the following points while configuring
the following settings:
·SMTP server name
·Port number
·SSL authentication
Gmail SMTP server name is “smpt.gmail.com”;
Gmail port is 465, not the default port 25;
The SSL authentication should be set to true;
So the secret of sending mails successfully with Gmail is: port 465, server name “smtp.gmail.com” and SSL = true.
hi men/women actually when I design the page in asp.net (in c#) I arrange the contents in specific way , but when make "browse in web page" the contents overlapping and some of them get bigger and another get smaller and some move from original position ....etc.
I tried to use tables to make the content fixed , I also tried to use the percent in size of tables, td and tr but with no useful can any one help me and tell me what is the suitable way to make the content the same in the design and browsing mode
Allen Chen –...
All-Star
40943 Points
4949 Posts
FAQ in Web Forms
Dec 13, 2008 01:15 PM|LINK
1 JavaScript
1.1 How to get client date and time
1.2 How to access a control by using JavaScript
1.3 How to invoke a server-side function with JavaScript
1.4 How to retrieve server side variables using JavaScript code
1.5 How to assign a value to a hidden field using JavaScript in ASP.NET
1.6 How to register the JavaScript function at Code-Behind
1.7 How to display images with a delay of five seconds
1.8 How to get browser screen settings and apply it to page controls
1.9 How to clear the session when the user closes a window
2 Ways to pass data between pages
2.1 How to use cookies
2.2 How to use QueryString
2.3 How to use Session
2.4 How to use Context
2.5 How to use PreviousPage
2.6 How to use Submit Form
2.7 How to use Server.Transfer
3 File Upload
3.1 How to upload a file
3.2 How to upload multiple files at once
3.3 Why upload fails when using an ASP.NET FileUpload control to upload large files
3.4 How to upload an image files only
3.5 How to get a File Upload control work with an UpdatePanel
4 Calendar
4.1 How to change the culture settings for a Calendar
4.2 How to select multiple non-sequential dates at Code-Behind
4.3 How to disable some dates in Calendar control
4.4 How to extend Calendar control for Server-Side validation
4.5 How to set ToolTips and links in Calendar control’s DayRender event
4.6 How to give some dates different appearances
5 List controls
5.1 How to enable ASP.NET DropDownList with OptionGroup support
5.2 How to disable an item in DropDownList
5.3 How to hold the selected value for a DropDownList
6 User control
6.1 How to add a new property to UserControl
6.2 How to access a dynamically created UserControl
6.3 How to access a control inside a UserControl
7 Dynamic controls
7.1 How to create a dynamic control
7.2 How to access a user entered value in a dynamic created TextBox control
7.3 Dynamic controls accessed by JavaScript
7.4 How to retain all added server controls dynamically after post back
7.5 Why dynamically created controls disappear after a post back
8 Style
8.1 How to use with Code-Behind
8.2 How to use with JavaScript
8.3 How to remove a space
8.4 How to use with html
8.5 How to set an image as Button’s background
8.6 How to color items in ListBox
9 Print
9.1 How to print a part of a web page with CSS
9.2 How to print a part of a web page with JavaScript (1)
9.3 How to print a part of a web page with JavaScript (2)
10 Mail
10.1 What classes are needed to send e-mails in ASP.NET
10.2 How to send emails by using System.Net.Mail
10.3 How to configure a SMTP Server
10.4 How to send an email with Gmail server
<div style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: medium none; PADDING-BOTTOM: 1pt; PADDING-LEFT: 0pt; PADDING-RIGHT: 0pt; BORDER-TOP: medium none; BORDER-RIGHT: medium none; PADDING-TOP: 0pt; mso-element: para-border-div; mso-border-bottom-alt: solid windowtext .75pt" mce_style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0pt; BORDER-TOP: medium none; PADDING-LEFT: 0pt; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0pt; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-border-bottom-alt: solid windowtext .75pt">
</div>1. JavaScript
1.1 How to get client date and time [top]
You can use java script function to show the date and time.
Then you can call it at web page.
Related posts:
http://forums.asp.net/p/1247758/2303034.aspx
1.2 How to access a control by using JavaScript [top]
Reference the ClientID property (or UniqueID) of the control in the JavaScript.
Or you can use the following method:
Related posts:
http://forums.asp.net/p/1239593/2260331.aspx#2260331
1.3 How to invoke a server-side function with JavaScript [top]
Firstly, you can drag a server button and put the server function into the button Click even,
Secondly, you can call the server function at JavaScript by using the following code,
Related posts:
http://forums.asp.net/p/1242420/2274228.aspx
1.4 How to retrieve server side variables using JavaScript code [top]
Then you can access the control HiddenField1 using javascipt:
Related posts:
http://forums.asp.net/p/1000655/1319119.aspx
1.5 How to assign a value to a hidden field using JavaScript in ASP.NET [top]
We can use JavaScript to set the value of a hidden control and get its value at the server after a post back.
Related posts:
http://forums.asp.net/p/1262153/2362090.aspx
1.6 How to register the JavaScript function at Code-Behind [top]
Use ResterStartupScript
Use Literal control,
Related posts:
http://forums.asp.net/p/981603/1257057.aspx#1257057
1.7 How to display images with a delay of five seconds [top]
With this script you can see clickable images rotating in real-time without requiring banner rotation software or page reloads/refreshes .You should see a new banner rotating every 5 seconds:
Related posts:
http://forums.asp.net/p/1213103/2147140.aspx
1.8 How to get browser screen settings and apply it to page controls [top]
You can use the JavaScript, suppose the control type is <image>, see the code below:
Please remove the control style if it is present.
Related posts:
http://forums.asp.net/p/1228180/2212987.aspx
1.9 How to clear the session when the user closes a window [top]
Use the code,
Related posts:
http://forums.asp.net/p/1237752/2255401.aspx
2. Ways to pass data between pages
2.1 How to use cookies [top]
Create a page named page1.aspx, and then drag a button and a TextBox onto the page. Double click the button and then add the following code:
page1.aspx,
On page2.aspx, double click the form and put the following code in it:
Related posts:
http://forums.asp.net/t/1223291.aspx
Asp.Net Cookies Overview
How to: Read a Cookie
How to: Delete a Cookie
ASP.NET State Management Overview
ASP.NET State Management Recommendations
2.2 How to use QueryString [top]
Destination web form
Related posts:
http://forums.asp.net/p/1223291/2191155.aspx
ASP.NET State Management Overview
ASP.NET State Management Recommendations
2.3 How to use Session [top]
Destination web form
Related posts:
http://forums.asp.net/p/1255625/2333723.aspx
How to: Read Values from Session State
How to: Save Values in Session State
HttpSessionState Class
ASP.NET State Management Overview
ASP.NET State Management Recommendations
2.4 How to use Context [top]
Related posts:
http://forums.asp.net/t/1238286.aspx
HttpContext Class
ASP.NET State Management Overview
ASP.NET State Management Recommendations
2.5 How to use PreviousPage [top]
FirstForm.aspx
SecondForm.aspx.cs
Or SecondForm.aspx.cs
As you’ve noticed, this is a simple and clean implementation of passing values between pages.
Related posts:
http://forums.asp.net/p/1048041/1474374.aspx
Page.PreviousPage Property
How to: Pass Values Between ASP.NET Web Pages
2.6 How to use Submit Form [top]
page1.aspx,
page2.aspx,
Related posts:
http://forums.asp.net/p/1257184/2339923.aspx
2.7 How to use Server.Transfer [top]
page1.aspx,
page2.aspx,
Related posts:
http://forums.asp.net/p/1262144/2362078.aspx
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
http://dotnetslackers.com/community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx
Cross-Page Posting in ASP.NET Web Pages
How to: Determine How ASP.NET Web Pages Were Invoked
3. File Upload
3.1 How to upload a file [top]
Related posts:
FileUpload Web Server Control OverviewFileUpload Class
3.2 How to upload multiple files at once [top]
Please check the following article, in the article, Haissam Abdul Malak will explain how to upload multiple files in an organized way using HtmlInputFile control.
Related posts:
http://forums.asp.net/t/1263738.aspx
http://aspalliance.com/1221_CodeSnip_Uploading_Multiple_Files_At_Once.all
3.3 Why upload fails when using an ASP.NET FileUpload control to upload large files [top]
For security reasons, ASP.NET is limited in terms of an uploaded file size. The default maximum file size is 4 MB but this can be changed by modifying the MaxRequestLength attribute of Machine.config's <httpRuntime> element.
Related posts:
http://forums.asp.net/t/1074332.aspx
http://forums.asp.net/p/1106754/1696069.aspx#1696069
3.4 How to upload an image files only [top]
See the code,
Fileupload.aspx:
Fileupload.aspx.cs:
Related posts:
http://forums.asp.net/p/1051895/2171502.aspx
3.5 How to get a File Upload control work with an UpdatePanel [top]
The FileUpload control does not work with asynchronous post backs and therefore does not work from within an AJAX UpdatePanel.
The trick to make the FileUpload to work within an Ajax UpdatePanel is to setup a PostBackTrigger in the UpdatePanel control.
Demo code:
Related posts:
http://forums.asp.net/p/1105208/1689084.aspx
4. Calendar
4.1 How to change the culture settings for a Calendar [top]
In calendar.aspx.cs:
Related posts:
http://forums.asp.net/t/1133896.aspx
4.2 How to select multiple non-sequential dates at Code-Behind [top]
Invoke the member function ‘Add’ of the control's SelectedDates collection. You can add dates in any sequence, because the collection will automatically arrange them in order for you.
Related posts:
http://forums.asp.net/t/1260917.aspx
4.3 How to disable some dates in Calendar control [top]
Related posts:
http://forums.asp.net/t/1230073.aspx
How to: Customize Individual Days in a Calendar Web Server Control
4.4 How to extend Calendar control for Server-Side validation [top]
Refer to:
http://support.microsoft.com/default.aspx?scid=kb;en-us;310145
4.5 How to set ToolTips and links in Calendar control’s DayRender event [top]
Related posts:
http://forums.asp.net/p/1036174/1800067.aspx
4.6 How to give some dates different appearances [top]
We can do this with the following code:
Related posts:
http://forums.asp.net/p/1036174/1800067.aspx
How to: Customize Individual Days in a Calendar Web Server Control
How to: Format Calendar Web Server Control Elements Using Styles
5. List controls
5.1 How to enable ASP.NET DropDownList with OptionGroup support [top]
We can override the function of DropDownList, and add the additional property for it.
Here are some good articles about it.
Refer to:
http://www.codeproject.com/KB/custom-controls/xlist.aspx
http://www.codeproject.com/KB/custom-controls/DropDownListOptionGroup.aspx
5.2 How to disable an item in DropDownList [top]
Related posts:
http://forums.asp.net/p/1041568/1451492.aspx
5.3 How to hold the selected value for a DropDownList [top]
You should place your data binding code for the dropdownlist in the !page.Ispostback block.
the !postback block will ensure it will only be filled once during post backs.
Related posts:
http://forums.asp.net/p/1251081/2312321.aspx
6. User control
6.1 How to add a new property to UserControl [top]
You can setup new properties inside the class definition of the user control at its .ascx.cs file.Example:
ascx file,
ascx.cs file,
aspx file
Related posts:
http://forums.asp.net/t/349580.aspx
6.2 How to access a dynamically created UserControl [top]
You can use FindControl method to get a reference to the target child control of your user control and then use it just like any other controls.
Example:
UC.ascx,
Page.aspx:
6.3 How to access a control inside a UserControl [top]
Assume there is a user control called UC and there is only a TextBox control inside it. Now drag this user control into a web page, you can use the following code to visit the TextBox control.
To known the basic principle, please refer to INamingContainer:
http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx
7. Dynamic controls
7.1 How to create a dynamic control [top]
We can create the dynamic control in the Page_Init() event or Page_Load() event,
Related posts:
http://forums.asp.net/t/1152363.aspx
7.2 How to access a user entered value in a dynamic created TextBox control [top]
a. Get the value from the form's POST data. Here is the code:
b. Get the value by finding the control at the webpage.
See the whole demo code,
Related posts:
http://forums.asp.net/p/1119972/1745762.aspx
7.3 Dynamic controls accessed by JavaScript [top]
Reference the ClientID property (or UniqueID) of the control in the Javascript.
Or,
Use the following method.
Related posts:
http://forums.asp.net/p/1239593/2260331.aspx#2260331
7.4 How to retain all added server controls dynamically after post back [top]
You should recreate these dynamic control at Page_load or Page_init() function everytime.
Related posts:
http://forums.asp.net/p/1242809/2280514.aspx
7.5 Why dynamically created controls disappear after a post back [top]
The dynamic button must be re-created on each post back, so remove the if(!Page.IsPostBack). It's probably better if you create the controls at the Page_Init event.
Related posts:
http://forums.asp.net/p/1080863/1598618.aspx
8. Style
8.1 How to use with Code-Behind [top]
8.2 How to use with JavaScript [top]
8.3 How to remove a space [top]
Add the following code inside of the “head” tag,
8.4 How to use with html [top]
Note: CSS is valuable
Linked style sheet usually lives in <head> tag, but there is no need to worry if it is put inside <body> tag. As well, <head> tag must have a runat=”server” attribute.
Related posts:
http://forums.asp.net/p/1197909/2076464.aspx
8.5 How to set an image as Button’s background [top]
Related posts:
http://forums.asp.net/t/299555.aspx
8.6 How to color items in ListBox [top]
Demo code:
Please refer to:
http://www.codeproject.com/KB/webforms/ColorListBox.aspx
9. Print
9.1 How to print a part of a web page with CSS [top]
CSS CODE:
HTML CODE:
Related posts:
http://forums.asp.net/t/981539.aspx
9.2 How to print a part of a web page with JavaScript (1) [top]
JavaScript CODE:
HTML CODE:
Related posts:
http://forums.asp.net/p/1234564/2256428.aspx
9.3 How to print a part of a web page with JavaScript (2) [top]
JavaScript CODE:
HTML CODE:
Related posts:
http://forums.asp.net/t/1263912.aspx
10. Mail
10.1 What classes are needed to send e-mails in ASP.NET [top]
Class MailMessage and SmtpMail are used to send emails from an ASP.NET application. MailMessage and SmtpMail are from System.Web.Mail namespace of .NET Framework 1.1 Class Library. Also, you can use System.Net.Mail instead of System.Web.Mail if you have .NET Framework version 2.0 installed.
10.2 How to send emails by using System.Net.Mail [top]
CODE-BEHIND:
web.config:
Related posts:
http://forums.asp.net/t/971802.aspx
10.3 How to configure a SMTP Server [top]
Taking the IIS as an example, please review the following links:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/56c94d38-b10f-4a1b-a1cd-3714387a042a.mspx?mfr=true
http://www.codeproject.com/KB/winsdk/ConfigServerSmtp.aspx
10.4 How to send an email with Gmail server [top]
Please read FAQ “How to send email by using System.Net.Mail?” first. Please note that you need to be aware of the following points while configuring the following settings:
· SMTP server name
· Port number
· SSL authentication
Gmail SMTP server name is “smpt.gmail.com”;
Gmail port is 465, not the default port 25;
The SSL authentication should be set to true;
So the secret of sending mails successfully with Gmail is: port 465, server name “smtp.gmail.com” and SSL = true.
Related posts:
http://forums.asp.net/p/1167140/1944223.aspx
http://forums.asp.net/p/1234241/2235990.aspx
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Joesef_2010
Member
2 Points
2 Posts
Re: FAQ in Web Forms
Mar 26, 2011 03:21 PM|LINK