-->

Friday, July 26, 2013

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.

Sunday, July 21, 2013

Filter data in datagridview according to combobox in c#

As we have previously studied that datagridview is used for display rows and columns of data in a grid format in windows form. Mostly data are previously defined which are to be shown in datagridview. Sometimes we have to change the data on basis of some conditions.

In previous post we have learnt to bind a datagridview with search option and in else case all the data from database will bind to datagridview in C# language. In this post we will learn to bind datagridview with the selected index changed event of combobox in C#.

The selection changed event of combobox occurs when the value of selected index property changes. We are going to change our data of datagridview according to that changed value.

Let’s create a student class, which will be used to bind our datagridview, having some properties like below:
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string Branch { get; set; }
}

Create a new list of student that will be the data source of datagridview and insert some records in that list having branch CS or IT as I have inserted in my example using C# language. Set this list as a data source of datagridview.
dataGridView1.DataSource = studentList;

Now when we run this project all the records will be shown in datagridview. We want to change the data of datagridview according to selection changed event. So write the below code in selection changed event of combobox
if (comboBox1.SelectedItem != null)
{
string branch = comboBox1.Text;
var students = studentList.Where(a => a.Branch.Equals(branch));
dataGridView1.DataSource = students.ToList();
}

Run this project now and change the selection value of combobox to CS and we will show the data of datagridview have been changed as following image:

Filter data in datagridview according to combobox in c#

Change the selection value to IT and the data will be changed as following image:


Filter data in datagridview according to combobox in c#
Go for example

How to use TextBox control in ASP.NET

Introduction

The TextBox control is an input control , which allows you to enter text. The TextBox control exists within the System.Web.UI.WebControls namespace.
You can set the style of the TextBox by using the TextMode property . By default , the TextMode property is set to SingleLine to create a single-line HTML . text field but it can also be set to MultiLine for a multiline textbox.
You can convert a mode of TextBox control to a Password control, where the text, which the user types is masked with special symbols such as asterisks (*).
The display width of a textbox is set with its columns property and if it is a multiline textbox , the display height is set with the Rows property.

Public Properties of the TextBox Classes
AutoCompleteType : Obtains or sets a value that indicates the AutoComplete behavior of the TextBox control.
AutoPostBack : Obtains or sets a value that indicates whether an automatic postback to the server occurs when the TextBox control loses focus.
CausesValidation : Obtains or sets a value indicating whether validation is performed when the textbox control is set to validate when a postback occurs.
Columns : Obtains or set the display width of the textbox in characters.
MaxLength : Obtains or sets the maximum number of characters allowed in the textbox.
ReadOnly : Obtains or sets a value indicating whether the contents of the TextBox control can be changed.
Rows : Obtains or sets the number of rows displayed in a multiline textbox.
Text : Obtains or sets the text content of the TextBox control.



TextMode : Obtains or sets the behavior mode (single-line , multiline or password) of the TextBox control.
ValidationGroup : Obtains or sets the group of controls for which the TextBox control causes validation when it postback to the server.
Wrap : Obtains or sets a value indicating whether the text content wraps within a multiline textbox.

Public Event of the TextBox Class
TextChanged : Occurs when the user changes the text of the TextBox.


Example of the TextBox Control

<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = " this is the single line textbox";
        TextBox2.Text = " this is the multi-line text box having 10 rows.";
        TextBox4.Text = TextBox3.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:Label ID="Label1" runat="server" style="font-weight: 700" Text="Different types of TextBox control"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Click me" />
        <br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server" Height="27px" Width="220px"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server" Rows="10" TextMode="MultiLine"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" style="font-weight: 700" Text="Enter Password"></asp:Label>
        <br />
        <asp:TextBox ID="TextBox3" runat="server" Height="23px" TextMode="Password" Width="211px"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="TextBox4" runat="server" Height="23px" Width="206px"></asp:TextBox>
 
    </div>
    </form>
</body>
</html>

© Copyright 2013 Computer Programming | All Right Reserved