Pages

May 1, 2009

Useful Links

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

How to Select the Asp.Net server controls + JQuery

People while using MasterPages in your applications client ID of controls will differ from "server" ID. That's because ASP.NET creates a new ID for controls on a page. So this is probably very familiar to you:

ctl00_cphContent_txtName

You set the ID of the textbox to "txtName" and ASP.NET adds "ctl00_cphContent_". Although there were some tries to prevent ASP.NET from generating unique ID's, I think it's better to use quick and "dirty" solutions :-)

So, how to select a server control using JS/jQuery? The usual way to select an element in JavaScript is to use server tags:

document.getElementById("<%=txtName.ClientID %>");

You can use the same approach with jQuery:

$("#'<%=txtName.ClientID %>'");

JQuery enables you to avoid server tags completely. Since we can search for element just by the part of the name, we can do the next:

$("[id$='_txtName']");

This will find elements which id's ends with "_txtName". But why the dash in the front of the name? This will ensure you selected the element by its full control ID, not just a part of it.

Pretty simple, isn't it? The only issue here would be if you have controls with the same name placed in different content pages.

How to access Page's session variable or create a new session variable in a normal C# class

This is Quite simple,
Usually

1 . We have lot of classes in the web application where the have the need to maintain or access the page's data or the server controls data between classes.
2. And there may be a situation where we need to send the data to the UI Page from the AppCode class or any class. For eg: i have processed something and need to send the data to the textbox in the UI Page.

This would be achieved by using
namespace
System.web.UI.;

And like this you can create a session variable in a non UI class and access it in the page's class.

HttpContext.Current.Session["test1"] = "Got from test";

and the same way you can access the page's session variable in the non UI class like

string test = string.Empty;
test = HttpContext.Current.Session["test1"].ToString();


Thus we can maintain the data between the classes no matter whether it is UI or normal class.