-->

Sunday, July 28, 2013

How to use MultiView and View Control in ASP.NET

The MultiView Control
The MultiView control is a container for a group of view controls . It allows you to define a group of View controls , where each View control contains child controls . Your application can then render a specific View control based on specific criteria , such as user identity, user preferences, and information passed in a querystring parameter. The MultiView control is also used to create wizard . To allow user to navigate between View controls within a MultiView control, you can add a LinkButton or Button control to each View control.

Public properties of the MultiView Class
ActiveViewIndex : Obtains or set the index value of the active View control within a MultiView control.
EnableTheming : Obtains or sets a value showing whether themes apply to the MultiView control.
Views : Obtains the collections of view controls in the MultiView control.

Public Methods of the MultiView Class
GetActiveView : Obtains the current active View control within a MultiView control.
setActiveView : Sets the specified View control to the active view within a MultiView control.

Public Event of the MultiView Class
ActiveViewChnaged : Raised when the active View control of a multiView control changes between posts to the server.

The View Control 
The view control is a container for a group of controls . A View control must always be stored within a MultiView control. Only one View control can be defined as the active view within a MultiView control at a time. A view control can contain any type of controls, including other MultiView controls. A View control does not support any style properties . To apply styles to a View Control , You have to add one or more panel controls to the View controls. To allow users to navigate between multiple View controls within a MultiView control , you can add a LinkButton or Button control to each View Control.

Public Properties of the View Class
EnableTheming : Obtains or sets a value showing if themes apply to this controls or not.
Visible : Obtains or sets a value which indicates whether the  View control is visible.




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

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.Visible = true;
        MultiView1.SetActiveView(View1);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        MultiView1.Visible = true;
        MultiView1.SetActiveView(View2);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            font-size: larger;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <strong><span class="auto-style1">Select Buttons for birthday gift </span></strong>

    <div>
 
        <asp:Button ID="Button1" runat="server" Text="Gift 1" OnClick="Button1_Click" />&nbsp;
        <asp:Button ID="Button2" runat="server" Text="Gift 2" OnClick="Button2_Click" />
 
        <br />
        <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex ="0" Visible ="false">
            <asp:View ID="View1" runat ="server">
                <asp:Image ID="Image1" runat="server" Height="165px" ImageUrl="~/picture/images.jpg" Width="194px" />

            </asp:View>
            <asp:View ID="View2" runat ="server" >
                <asp:Image ID="Image2" runat="server" Height="165px" ImageUrl="~/picture/motorola droid.jpg" Width="191px" />

            </asp:View>
        </asp:MultiView>
 
    </div>
    </form>
</body>
</html>


Output
How to use MultiView and View Control in ASP.NET

How to use MultiView and View Control in ASP.NET

Friday, July 26, 2013

How to Change file attributes in C# Programming

File attributes are metadata associated with computer files that define system behaviour.  Same as files, folders, volumes and other file system objects may have these attributes. All the operating system have often four attributes i.e. archive, hidden, read-only and system. Windows has more attributes. Read more about File Attributes

User have to right click on desired file or folder and then open properties, to change the attributes of that file or folder. If one want to change attributes of multiple files then he/she has to either select all the files and then change or change attributes of each file or folder one by one.

In programming context we can do this task with a predefined class i.e. FileAttributes which provides all the attributes for files and directories. We can easily change the desired attribute of a file or directory using the above class FileAttributes that is exists in System.IO namespace.

Design a new form in windows form application that will look like the below form:


Change file attributes in C#

There are some controls which are used for specific task in this project, we will discuss them one by one:
  • TextBox: show the path of folder selected by user.
  • Browse Button: open a folderBrowserDialog which select a path of folders and files.
  • CheckBox: if checked program will check all the sub-folders for files.
Except these controls there are three more buttons which are used to play with attributes of files or folders as their text shows. For example "Remove all" will remove all the attributes of files and folder at selected path.

Write the following code in the click event of Only Hidden button:
private void btnSetHidden_Click(object sender, EventArgs e)
        {
            count = 0;
            setHidden(txtPath.Text);
            lblStatus.Text = "Number Of Scanned Files And Folders:" + count.ToString();
        }
In the above code there is an integer variable count that will count the scanned file and folders. Then it will call a function setHidden(string ) that will use the selected path and set the hidden property to true of all the files and folders at that selected path.


The code of this function is:
private void setHidden(string dir)
{
if (!Directory.Exists(dir))
{
lblStatus.Text = "Invalid path!";
return;
}
if (chkSub.Checked)
{
foreach (string d in System.IO.Directory.GetDirectories(dir))
{
count++;
lblStatus.Text = dir;
setHidden(d);
DirectoryInfo df = new DirectoryInfo(d);

df.Attributes = System.IO.FileAttributes.Hidden;
foreach (string fil in System.IO.Directory.GetFiles(d))
{
FileInfo f = new FileInfo(fil);
f.Attributes = System.IO.FileAttributes.Hidden;
Application.DoEvents();
count++;
}
}
}
}
In above code it will change the attribute of a directory and then all the files in that directory. Here, a new Method doEvents() is used which process all windows messages currently in the message queue. Calling this method causes the current thread to be suspended while all waiting window messages are processed. You can read more about DoEvents().

Go for example.

How to use CustomValidator control in ASP.NET

Introduction
The CustomValidator control is used to customize and implement data validation as per your requirement. Occasionally, You might need to validate your data in a way that is not possible directly with the help of existing the Validation controls. For example , if you want to find out whether a number is odd or even , you cannot use an existing validation control for doing that because this functionality is not included in any of the Validation controls. To solve this problem , you need a Validation control that can be customized to solve this problem. The CustomValidator control exists within the System.Web.UI.WebControls namespace. The CustomValidator control checks whether or not the input you have given, such as prime, even, or odd number, matches a given condition.

Public Properties of the CustomValidator Class:
ClientValidationFunction : Obtains or sets the name of the custom client-side script function used for validation.
ValidateEmptyText : Obtains or sets s Boolean value indicating whether empty text should be validated or not.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="logincontrol.aspx.cs" Inherits="logincontrol" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>  
        Enter number :
        <asp:TextBox ID="TextBox1" runat="server" Height="19px" Width="234px"></asp:TextBox>
&nbsp;<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="CustomValidator" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
        <br />  
    </div>
    </form>
</body>
</html>
Code-Behind file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class logincontrol : System.Web.UI.Page
{    protected void Page_Load(object sender, EventArgs e)
    {
        }
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Convert.ToInt32(args.Value) % 2 == 0)
        {
            Response.Write("even number");

        }
        else
        {
            Response.Write("odd number");
        }
    }
}

Wednesday, July 24, 2013

Example of AutoComplete Type property of TextBox Control in ASP.NET

Obtains or sets a value that indicates the AutoComplete behavior of the TextBox control.
Introduction
AutoComplete means your textbox control should filled automatically after given some word or letter into your textbox. Suppose you have a email input textbox on your browser screen and this textbox automatically  filled after giving some words or letter.
Lets take an Example:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Email" Height="26px" Width="270px"></asp:TextBox>
    </div>
    </form>
</body>
</html>
Output
Example of AutoComplete Type property of TextBox Control in ASP.NET

In this example we set email as a AutoCompleteType property of the textbox control. In above image you can see when we enter "n" letter into the textbox then a popup appear bellow to the textbox with having email id which start  letter "n".

If you want to disable your AutoComplete property of the TextBox control then set Disabled value. 
you can set for  Re-Type password ,  Email -conformation etc. 

Tuesday, July 23, 2013

How to make comment box in ASP.NET

Basically CommentBox shows list of data. So we can use repeater control for this types of application.The Basic logic behind of this application is.

  • Feed your user data to database table first
  • Show data from database table using repeater control.
Introduction on repeater control
The Repeater control is a data bound control that is used to display repeated list of items from the associated data source. It is a single WebControl that allows splitting markup tags across the templates . The Repeater control does not provide support to in-Built layout or styles . Therefore, While working with the repeater control you have to explicitly declare all layout  , formatting , and style. In other words , The Repeater control does not support built-in selection or editing features.


Create database Table

Make Stored procedure 
CREATE PROCEDURE insrtdata

AS
select * from commentbox

RETURN 0




<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
     
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            con.Open();
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandText = "insrtdata";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Connection = con;
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        con.Open();
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.Parameters.AddWithValue("@na",nametxt .Text);
        cmd.Parameters.AddWithValue("@em",emailtxt .Text);
        cmd.Parameters.AddWithValue("@web",webtxt .Text);
        cmd.Parameters.AddWithValue("@con",contenttxt .Text);
        cmd.CommandText = "insert into [commentbox](Name,Email,Website,Content)values(@na,@em,@web,@con)";
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
     

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        Your Comment:<br />
        Name :&nbsp;&nbsp;
        <asp:TextBox ID="nametxt" runat="server" Height="24px" Width="231px"></asp:TextBox>
        <br />
        <br />
        Email :&nbsp;&nbsp;&nbsp; <asp:TextBox ID="emailtxt" runat="server" Height="23px" Width="230px"></asp:TextBox>
        <br />
        <br />
        Website:
        <asp:TextBox ID="webtxt" runat="server" Height="23px" Width="230px"></asp:TextBox>
        <br />
        <br />
        Content :
        <asp:TextBox ID="contenttxt" runat="server" Rows="5" TextMode="MultiLine"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit " />
 
    </div>
        <asp:Repeater ID="Repeater1" runat="server">
         
            <ItemTemplate>
                <hr />
             
                    <div style="background-color: #CCCC00">
Welcome,<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                    </div>
           
                <div style="background-color: #3399FF">
                    <asp:Literal ID="lit" runat ="server" Text ='<%# Eval("Content") %>' Mode ="Transform" />
                    <br />
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ='<%# Eval("Website") %>' Text ='<%# Eval("Website") %>' />
                      </div></ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>

Output
How to make comment box in ASP.NET

Programming techniques and logic in c language

Introduction

The tools like algorithm and flow charts help in understanding the problem and analyzing it. Once the problem and the required solution format are clear , the programming is too easy . In order to program the logical structure derived from algorithm or flowchart , one of the programming languages is picked . The selection of the programming language also depends on the nature of the problem.
You will see the different programming techniques that are used to solve a problem. The solution of the problem is nothing but to design the program . Before going into the actual design , one should understand the features, merits and demerits of each programming technique .

After selecting the technique for programming , the next step is the selection of logic . In general , a program can be written in different ways. The different ways give rise to the logic of the program. One of the programs whose logic is simple and understandable is finally selected for implementation. The logic of the program depends on the capability of the programming language . The capabilities of the programming language include the availability of different types of statements . A good logic selection will also improve the performance and efficiency of the designed program.

Related post

Introduction to programming techniques

Now , let us see " what are the different types of programming techniques?"
Definition : A method used to design the program to solve a problem or task is called programming technique. The following content shows the number of techniques available for programming.

Programming Techniques:

  • Top-down
  • Bottom-up
  • Unstructured
  • Structured
  • Modular

Monday, July 22, 2013

How to prevent duplicate child forms in windows forms C#

In my previous post we have create an MDI container form in which we have opened some individual forms called child forms. Each form contains a property MdiParent that is used to get or set the parent form of a particular form.
Form2 obj = new Form2();
obj.MdiParent = this;
obj.Show();

As we know, in MDI container application user opens multiple child forms at a time. Most often when user opens a child form, application opens a new instance of the form, doesn't matter the form is previously opened or not. This is programmer’s job to check if the same form is previously opened then bring that form to front otherwise create a new instance.

To check the instance is null or not we have to write following code in our code behind file of form that will be the child form.
private static Form3 instance;
public static Form3 GetInstance()
{
if (instance == null)
instance = new Form3 ();
return instance;
}

In above code we have defined a new object of type Form3. Each time when we call this method it will check this object whether it is null or not. If this is null it will create a new instance of the form otherwise it simply return that object.

Our next task is to set the value of that instance to null each time user closes that form. Each form has a FormClosing event which occur whenever user closes the form, before the form will closed. We will set the instance to null in this FormClosing event of the form.
Instance = null;

In the MDI parent form replace the above three line of code with the following code:
Form3 obj = Form3.GetInstance();
obj.MdiParent = this;

if (!obj.Visible)
obj.Show();
else
obj.BringToFront();

Through the above code we will use the newly defined method i.e. GetInstance() in place of its constructor. Now when user opens this child form it will check that if that form is visible then it will bring that in front otherwise display the form to user using show method.

© Copyright 2013 Computer Programming | All Right Reserved