How to run JavaScript from code behind in ASP.NET

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 TutorialsHow to use XslCompiledTransform class to transform […]

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 set up XHTML 1.0 transitional doctype in ASP.NET 4.0
How to store custom objects in a […]

How to compare strings in ASP.NET

Comparing 2 strings is often used action in ASP.NET. You could want to compare two strings when you check if the username and pasword entrered by user are correct. Also it could be very useful if you search some text.The ASP.NET provides several methods to compare the values of strings.

Compare strings in ASP.NET

Compare strings in C# […]

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.

Adding Custom Control to the Toolbox

1. Create a test Web Forms page. (WebForm1)
2. Using Tools menu, click Add/Remove Toolbox Items.
3. On the .NET Framework Components tab of the Customize ToolBox […]

How to connect to MySQL database using ASP.NET hosting

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

Connect to MysQL database using C# on 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 […]

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 […]

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:

ReadOnlyTextBox.aspx

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

Inherits=”TextBoxSamplesVC.ReadOnlyTextBox” %>

<!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”>

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. The next test .aspx page shows this:

TextBoxAsPassword.aspx

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

Inherits=”TextBoxSamplesVB.TextBoxAsPassword” %>

<!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>Password type TextBox</title>

</head>

<body>

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

<div>

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

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 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”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en-US”>

<head>

<title>How to specify […]

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 […]

Recommended ASP.NET Examples