Sponsored Ad

Tuesday, May 11, 2010

How to Declare variable in ASP.NET Page using C#

How to Declare variable in ASP.NET Page using C#

Server side coding (C#, VB or any other language can be used with ASP.NET page. Here is sample code to implement the same. ASP.NET page allows developer to write C# syntax inside the <% %> block. Its means it can contain C# block of code same as written in .cs file.

This simple program declare a string and print it using c# syntax. The whole code for aspx page is given below. Please note that This page does not contain the .cs file.

you need to add AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" in Page directive to work with .cs file.

 

Source Code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<body>

<%

string strStatement;

strStatement = "This is an example of C# coding within ASP.NET Page.";

Response.Write("My Statement is: " + strStatement);

%>

</body>

</html>

Set Image Properties in a webpage

 

Set Image Properties in a webpage

 

Sometimes while designing webpage, placing a image and setting their properties are touph task. This example helps you to arrange a image on webpage using background-image property. with some other image arrangement properties. Let me know if you face any problem while arranging a image.

Source Code:

<html>

<head>

<style type="text/css">

body{ background-image: url('Sunset.jpg'); background-repeat: no-repeat; background-position: center; }

</style>

</head>

<body>

</body>

</html>

Monday, May 10, 2010

Check Characters of String for All Digits in C#

 

This program will help you to check character by character string that it contains the digits or not.

There is separate method to check this functionality , you can copy and paste to your program to use it.

Digits along with Char - C#

Digits along with No Char - C#

Source code:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is my First Console Application");
            // user input
            Console.WriteLine("Please enter some string:");
            string strInput = Console.ReadLine();
            Console.WriteLine("you have entered a string: " + strInput);
            Console.WriteLine();
            bool blnResults = CheckForDigits(strInput);
            strInput = blnResults?"Yes":"No";
            Console.WriteLine("String contains the all digits? " + strInput);

            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }

        public static bool CheckForDigits(string strTemp)
        {
            //remove white spaces
            strTemp = strTemp.Trim();

            if (strTemp.Length == 0)
            {
                return false;
            }
            // loop through the string
            for (int i = 0; i < strTemp.Length; i++)
            {
                if (Char.IsDigit(strTemp[i]) == false)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

Sponsored Ad

Development Updates