How to run JavaScript from code behind in ASP.NET

This tutorial for programmers will show you how to run JavaScript from code behind follow the next steps:

1. Set the Body tag in your page/master page:

<body runat="server" id="MyBody">

2. Write the script tag in your page:

<script src="MyFunction.js" type="text/javascript"></script>

3. In the code behind attach the onload attribute to the body tag:

protected void Page_Load(object sender, EventArgs e)
MasterPageBodyTag = (HtmlGenericControl)Page.Master.FindControl(“MyBody”);
MasterPageBodyTag.Attributes.Add(“Onload”, “MyFunction()”);

Related …

Learn more

How to replace \n line breaks with HTML <br /> line breaks in GridView

This is very common scenario when you would like to show well formatted GridView content. To do this you could use the ItemTemplate which is used by default to render the cell for each row:

<%# ((string)Eval(“GridviewContent”)).Replace(“\n”, “<br />”) %>

Related TutorialsHow to use timeout property of the proxy class in VB.NET
How to refresh a page in ASP.NET
How to remove …

Learn more

How to compare strings in ASP.NET

Comparing 2 strings is often used action in any programming language. You could want to compare two strings when you check if the username and password entered by user are correct. Also it could be very useful if you search some text.This C# ASP.NET tutorial with show you several methods to compare the values of strings.

Compare strings in …

Learn more

How to Create a Custom Control in ASP.NET

If none of the existing ASP.NET server controls meet your requirements, then you can create a custom control by deriving from one of the base control classes. This Web Forms ASP.NET tutorial will show you how to achieve this.

Adding Custom Control to the Toolbox

1. Create a test Web Forms page. (WebForm1)
2. Using Tools menu, click Add/Remove Toolbox Items.
3. …

Learn more

How to connect to MySQL database using ASP.NET hosting

Many developers use ASP.NET with MySQL with their applications. If you use ASP.NET hosting services and some of the .NET stack as VB.NET or C# then you could connect to the MySQL database using ODBC .net data provider.

Connect to MysQL database using C# using ASP.NET hosting

OdbcConnection cn = new OdbcConnection(“Driver={MySQL ODBC ODBC 5.2a Driver};Server=localhost or remote;Database=test;User=user;Password=pass;Option=3;”);

OdbcCommand cmd …

Learn more

How to disable a button after click in ASP.NET

This is a very common scenario when you would like to disable button after click, so the user could not click it again if you need some time to do some task which takes longer time to be executed.

Disable a button after click in ASP.NET before the PostBack

If you would like to disable button to do some task, …

Learn more

How to make the TextBox readonly or disabled in ASP.NET

You can make the TextBox control readonly by setting its ReadOnly property to true. You can set this property in the .aspx page which contains the TextBox control, or programmatically from the code-behind. The next test .aspx page shows this:


<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”ReadOnlyTextBox.aspx.cs”

Inherits=”TextBoxSamplesVC.ReadOnlyTextBox” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>

<html xmlns=””>

<head runat=”server”>

Learn more

How to render a password type textbox in ASP.NET

You can render the TextBox control to as password type by setting its TextMode property to Password.


<%@ Page Language=”vb” AutoEventWireup=”false” CodeBehind=”TextBoxAsPassword.aspx.vb”

Inherits=”TextBoxSamplesVB.TextBoxAsPassword” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>

<html xmlns=””>

<head runat=”server”>

<title>Password type TextBox</title>



<form id=”form1″ runat=”server”>


<asp:TextBox ID=”tbPassword” runat=”server” TextMode=”Password”></asp:TextBox>





Related TutorialsHow to search XML content with XPath …

Learn more

How to indent text with CSS

Sometimes you need to indent text in your web page. In this case you can apply a rule to the container that sets a  padding-left value. You can follow this CSS ASP.NET tutorial and to use the next two files to test this:


File test.css

.indent {

padding-left: 40px;



File test.html

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”


<html xmlns=”” …

Learn more

How to specify horizontal rule style with CSS

When your web site includes horizontal rules you can use CSS code to change their color, height and width. In this case for example the CSS code is the following:

File test.css

hr {

border: none;

background-color: #E6E6E6;

color: #000000;

height: 2px;

width: 70%;


You can use the next file to test:


File test.html


<!DOCTYPE html …

Learn more