-->

Wednesday, February 12, 2014

Example of Static and instance class member in c# programming

In my previous article, we have already discussed about difference between both static and non static members. Today we will learn example of both. Static member are not accessible by instance of the class. Look
Example of Static and instance class member in c# programming
 In above snap, you can see that compile time error has been occurred, because static member are not accessible by the instance of the class. Static member are only accessible by the class name. Look
Example of Static and instance class member in c# programming
so common variables are composed static members. While, we need object for calling non-static members.
While, we need object for calling non-static members.

How to add Controls Dynamically in ASP.NET

There are times when it is more practical to create a control at run time than at design time. For example, imagine a search results page in which you want to display results in a table. Because you do not know how many items will be returned, you want to dynamically generate one table row for each returned item.

Note Existing controls can often provide the functionality you get from creating controls dynamically. For example, controls such as the Repeater, DataList and RadioButtonList controls can dynamically create rows or other control elements when the page runs.

To programmatically add a control to a page, there must be a container for the new control. For example, if you are creating table rows, the container is the table. If there is no obvious control to act as container, you can use a Placeholder or Panel web server control.

In some instances, you might want to create both static text and controls. To create static text, you can use either a Literal or a Label web server control. You can then add these controls to the container as you would any other control.

Note : Manipulating controls programmatically is often used when creating composite controls.

To Add a Control to a Web Forms Page Programmatically 

Create an instance of the control and its properties

Label firstname = new Label();
firstname.Text = " Enter First Name";

Controls are typically added to the page during the page's load stage.

Add the New Control to the Controls Collection of a Container Already on the Page

Panel1.Controls.Add(firstname);

Note : Because Controls is a collection, you can use the AddAt  method to place the new control at a specific location- for example, in front of other controls. However, this approach can introduce errors into the page.

The following shows the event handler for the SelectedIndexChanged event of a control called DropDownList1. The handler creates as many label controls as the user has selected from the dropdown list. The labels display the word label plus a counter. The container for the controls is a Placeholder Web server control called Placeholder1.

Note : User input in a Web Forms page can include potentially malicious client script. By default, the web forms page validates that user input does not include script or HTML elements.

Complete Code


           <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            font-size: xx-large;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span class="style1">How many label , you want to create</span><br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            Height="25px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
            Width="113px">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>6</asp:ListItem>
            <asp:ListItem>7</asp:ListItem>
        </asp:DropDownList> 
     
    </div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </form>
</body>
</html>

//Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int numbers = Convert.ToInt32(DropDownList1.SelectedItem.Text);
        for (int i = 0; i <=numbers ; i++)
        {
            Label l1 = new Label();
            l1.Text = "Label" + i.ToString();
            l1.ID = "Label" + i.ToString();
            PlaceHolder1.Controls.Add(l1);
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));

        }
        
        
    }
}

Code generate the following output

How to add Controls Dynamically in ASP.NET

Tuesday, February 11, 2014

How to Perform Text Interaction with GUI in JAVA Programming part-2

setText()

To be continued from the previous article this method is used for sorting text into a GUI component. In Java Programming, this method is mostly used to store or change text in a text based GUI component. The swing components that support setText()  method include: Text field, Text area, Button, Label, Check Box, and Radio Button.

Suppose you want to change the content of field classTF to ‘XI’ through a code statement; for this you can use setText() method. To change text of classTF field to “XI”, you need to write:

classTF.setText(“XI”)

The setText() changes the value of field before the dot (.) with the string in its parentheses. The value to be set has been given as string constant "XI" in the above statement. You can also use a String value variable to specify the new value, as it is being explained below:

String newValstr = “XI”;
classTF.setText(newValstr);

JOptionPane.showMessageDialog()

The last but not least method for text interaction with GUI is showMethodDialog() which is used to display message in a dialog form.

Using this method, programmer can produce a basic dialog displaying a message to the user. The user will see your message only an "ok" button to close the dialog. To use this method, you need to perform it in two steps:

Firstly, in the Source Editor, where you type your code, at top most position type the following link:
Import javax.swing.JOptionPane;

Now display desired message as per following syntax (please notice carefully the case of letters):
JOptionPane.showMessageDiallog(null,” <desired message here>”);

For example, to display a message “Hello there!” you should write
JoptionPane.showMessageDialloglog(null, “Hello there!”) ;

It will display a separate message dialog displaying your message

How to Create a File System Web Site in Visual Studio

In a file-system Web site, you store the files for your application in a folder on your local computer or on a computer that is accessible on your network. File system Web sites are useful for developing locally on your computer because you do not need Internet Information Services (IIS) to create or test them.

To create a file-system Web site


  1.   In Microsoft Visual Studio, on the File menu, point to New and then click Web Site.
  2. Under Visual Studio installed templates, select the template for the type of web site you want to create.
  3. In the Location list, click File System.
  4. In the language list, click the language you want to use as the default programming language for the Web application.
  5. In the Location text box, type the path and folder where you want to create the Web site. You can specify a local path or a UNC path to a different computer on your network.
  6. If you prefer to browse to an existing location, do the following:
  • Click Browse
  • In the Choose Location dialog box, click the File System tab.
  • In the File System tree, select the folder you want to use or type the path in the folder box.
  • To create a new folder, select the location, click Create New Folder, and then type a name for the new folder.
  • Click Open to return to the New Web Site dialog box.
  1. Click
Visual Studio creates the site, opens a default page in the page designer, and then displays the folder in Solution Explorer.
If the path you specified already contains files, Visual studio prompts you to specify a different location, open the existing web site, or create the Web site anyway. In the last case, the files are created with the Web site overwrite any files with the same name that are already in the folder.

Monday, February 10, 2014

Types of Web Sites in Visual Web Developer

You can use Microsoft Visual Studio to create and work with Microsoft ASP.NET Web Sites (Web applications) in a variety of hosting scenarios: local Internet Information Services(IIS), file system, FTP, and remote sites.
Types of Web Sites in Visual Web Developer

Local IIS Web Sites

Local IIS Web Sites run using a copy of IIS that is installed on your computer. When you create a local IIS Web Site, the pages and folders for your site are stored in a folder under the default IIS folder for Web Sites (Inetpub\wwwroot). Visual Studio also create the appropriate IIS configuration so that the Web Site is recognized by IIS as an application.

Note : To create a local IIS Web Site, you need Administrator privileges on the computer.
Alternatively, you can create an IIS virtual directory in Visual Studio. In this case, the pages and folders for your Web site can be placed in any accessible folder, a virtual directory in your local copy of IIS points to this location.

File System Web Sites

In a file-system Web site, you can create files in any folder you like, whether on your local computer or in a folder on another computer that you access across a network share. You do not need to run IIS on your computer. Instead, you can test pages by using the Visual Studio Development Web Server.

Note : The Visual Studio Development Web Server works locally. It cannot serve pages to another computer and is therefore suitable only for testing pages locally.
If you create a file system Web site, you can later create an IIS virtual directory that points to the Web site.

FTP Sites

Visual Studio allows you to open and edit Web site that are accessible by using an FTP server. This is a typical scenario if your Web site is located on a hosting site.
You can connect from within visual studio to any FTP server on which you have read/write privileges. You can then create and edit Web pages on that server. If the FTP server is configured with ASP.NET and an IIS virtual root that points to the FTP directory, you can also run your pages from the server to test them.

Remote Web Sites

A remote Web Site is a site that uses IIS but is on another computer that you can access over a local area network. The remote computer must have IIS installed and must be configured with Microsoft Office FrontPage Server Extension. When you create a remote Web site, the pages and folders for your site are stored in the default IIS folder on the remote computer. When you run the pages, they are served by using IIS on the remote computer. 

Sunday, February 9, 2014

How to access base class constructor from derived class in C# Programming

Its a concepts of inheritance, child class derived from base class or you can say, a class inherits property from other class known as derived class or child class.
This example cover, If you want to access base class method into child class, you should use base keyword for that. Lets take an simple example to demonstrate of this.

The base class that is accessed is the base class specified in the class declaration. For example, if you specify class childclass : baseclass, the members of baseclass are accessed from childclass, regardless of the base class of baseclass.

Lets take an simple example 

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

namespace ConsoleApplication8
{
    class Program
    {
        public Program ()
        {
            Console .WriteLine ("Base class Constructor");
        }
       
        static void Main(string[] args)
        {

            abc obj = new abc();
            Console.ReadKey();

        }
        
    }
    class abc : Program
    {
        public abc():base()
        {

            Console.WriteLine("Child Class");

          
        }
    }
}

Code Generate the following output 

How to access base class constructor from derived class in C# Programming

How to call base class methods from derived class in c# programming

Its a concepts of inheritance, child class derived from base class or you can say, a class inherits property from other class known as derived class or child class.
This example cover, If you want to access base class method into child class, you should use base keyword for that. lets take an simple example to demonstrate of this.

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

namespace ConsoleApplication8
{
    class Program
    {
       
        static void Main(string[] args)
        {

            abc obj = new abc();
            Console.ReadKey();

        }
        public void getg()
        {
            Console.WriteLine("base class");

        }
    }
    class abc : Program
    {
        public abc()
        {
            base.getg();
            Console.WriteLine("Child class");

          
        }
    }
}

Code generate the following output

How to call base class methods from derived class in c# programming

© Copyright 2013 Computer Programming | All Right Reserved