-->

Tuesday, October 22, 2013

Programmatically change CheckBox Border Style in ASP.NET

Introduction

If you want to change Border-Style of the CheckBox programmatically then you should use Border-style property at Codebehind. Lets take a simple example for completing this task. Take a WebForm control and place one CheckBox and one Button control onto it.

Algorithm behind the scene is :

Step-1: Assign Border Color to CheckBox control.
Step-2: Set Border Width of the CheckBox Control in Units.
Step-3: Change Border Style.

Complete code

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

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

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox1.BorderColor = System.Drawing.Color.Green;
        CheckBox1.BorderWidth = 5;       
        CheckBox1.BorderStyle = BorderStyle.Outset;
 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Change CheckBox border style</title>
    <style type="text/css">
        #form1 {
            height: 78px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:CheckBox ID="CheckBox1" runat="server" Text="Change Border Style" />
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Change CheckBox Border Style"
        Width="198px" />
 
    </form>
</body>

</html>

Output
Programmatically CheckBox Border Style in ASP.NET

Programmatically CheckBox Border Style in ASP.NET

ASP.NET: Change DropdownList font name programmatically

Introduction

You can change the font face of the Text which is inside the DropdownList easily. If you want to change the face or family of the text then you must assign name of the face into font name property.

Lets take an simple example to change the font face/family Programmatically

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

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

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        DropDownList1.Font.Name = "Arial";       
       
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        DropDownList1.Font.Name = "Algerian";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="124px">
            <asp:ListItem>Hyperlink</asp:ListItem>
            <asp:ListItem>CheckBox</asp:ListItem>
            <asp:ListItem>Label</asp:ListItem>
            <asp:ListItem>Panel</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Change Font size Arial"
            onclick="Button1_Click" />
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Change Font size Algerian"
            onclick="Button1_Click1" />
        <br />
        <br />
    </div>
    </form>
</body>

</html>
Output
ASP.NET: Change DropdownList font name programmatically

ASP.NET: Change DropdownList font name programmatically

Monday, October 21, 2013

Uses of "Using" Statement in C#

Every object consumes some amount of storage in memory, when created by the programmer. Programmer have to dispose all the objects created and empty the storage allocated. This disposing can be done through Dispose() menthod in in-built IDisposable interface, in Visual Studio.

If programmer don’t release the memory, then it can be done by the CLR, according to its decision to perform garbage collection. Using statement allows the programmer to specify, when to release the resources used. The syntax of using statement:
using (ClassName obj = new ClassName())
{
// Line of code to be executed
}

The ClassName denotes the name of class to which the object is created. Obj is the object, which will be disposed at the last of this scope of “using” statement. The following code will create an object of Student class and when the debugger will reached on last line of code, it will release the memory allocated by the stu object.
using(Student stu = new Student())
{
//Line of code to be executed
}

The other reason of exiting the using statement may be an exception in our line of code. When an exception is generated, it will go to catch method, and dispose method will not be called. So we should use this using statement for better memory management.

ASP.NET : Change DropdownList font Size programmatically

Introduction

FontSize enumeration specifies the font sizes defined by HTML 4.0 . This enumeration available in System.Web.UI.WebControls.FontSize. There are many enumeration value available in FontSize enumeration those are shown below:

ASP.NET : Change DropdownList font Size programmatically


Complete code :

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

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

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
       
        DropDownList1.Font.Size =FontUnit.XLarge;
   
   
       
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="124px">
            <asp:ListItem>Hyperlink</asp:ListItem>
            <asp:ListItem>CheckBox</asp:ListItem>
            <asp:ListItem>Label</asp:ListItem>
            <asp:ListItem>Panel</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Change Font size Dynamically"
            onclick="Button1_Click" />
        <br />
        <br />
    </div>
    </form>
</body>

</html>
Output
ASP.NET : Change DropdownList font Size programmatically

ASP.NET : Change DropdownList font Size programmatically

Example of FindByText method in ASP.NET

Introduction

Searches the collection for a ListItem with a Text property that equals the specified text(According to msdn library).
Lets take an simple example to remove item from DropdownList using FindByText method. Basically this method works on collection.
Algorithm behind the scene is
Step-1 : Bind DropdownList with some values
Step-2 : Drop TextBox and Button control to the design page.
Step-3:  First check the capacity of the DropdownList is more than 0 . if capacity is more than 0 then remove the item from the list using FindByText( ) method.
Step-4: End Program.

Complete code

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

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

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        string item = TextBox1.Text;
        if (DropDownList1 .Items .Capacity >0)
        {
            DropDownList1.Items.Remove(DropDownList1.Items.FindByText(item));
        }
       
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="124px">
            <asp:ListItem>Hyperlink</asp:ListItem>
            <asp:ListItem>CheckBox</asp:ListItem>
            <asp:ListItem>Label</asp:ListItem>
            <asp:ListItem>Panel</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        Item to be removed from Dropdownlist:<br />
        <asp:TextBox ID="TextBox1" runat="server" Width="186px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Remove Item "
            onclick="Button1_Click" />
        <br />
        <br />
    </div>
    </form>
</body>

</html>
Output
Example of FindByText method in ASP.NET

Example of FindByText method in ASP.NET

Example of FindByText method in ASP.NET

Example of FindByValue method in ASP.NET

Introduction

Searches the collection for a ListItem with a Value property that contains the specified value (According to msdn library).
Lets take an simple example to remove item from DropdownList using FindByValue method. Basically this method works on collection.
Algorithm behind the scene is
Step-1 : Bind DropdownList with some values
Step-2 : Drop TextBox and Button control to the design page.
Step-3:  First check the capacity of the DropdownList is more than 0 . if capacity is more than 0 then remove the item from the list using FindByValue( ) method.
Step-4: End Program.

Complete code of the program is

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

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

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        string item = TextBox1.Text;
        if (DropDownList1 .Items .Capacity >0)
        {
            DropDownList1.Items.Remove(DropDownList1.Items.FindByValue(item));
        }
       
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="124px">
            <asp:ListItem>Hyperlink</asp:ListItem>
            <asp:ListItem>CheckBox</asp:ListItem>
            <asp:ListItem>Label</asp:ListItem>
            <asp:ListItem>Panel</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        Item to be removed from Dropdownlist:<br />
        <asp:TextBox ID="TextBox1" runat="server" Width="186px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Remove Item "
            onclick="Button1_Click" />
        <br />
        <br />
    </div>
    </form>
</body>

</html>
Output
Bind DropdownList in asp.net

Remove item from dropdownlist using textbox value

Remove item from FindByValue method

How to use SqlDataReader class in ASP.NET

Introduction

Provides a way of reading a forward-only stream of rows from a SQL Server database (according to msdn library). Means you can say a SqlDataReader class read DataRows in forward only stream. SqlDataReader class available in  System.Data.SqlClient namespace.

There are some useable public properties available in Library

 1. HasRows : You can check that your DataReader either empty or take some DataRows.
 2. FieldCount : Obtains numbers of fields in current rows.

There are some public method available in Library

Read( ) : Read Next record from the database table.
GetValue( ) : Gets the value of the specified column in its native format.

Lets take an simple example to read data from database_table using SqlDataReader class.

Algorithm behind the example

Step-1 : Create a SqlConnection( ) with proper connection string parameter.

SqlConnection con = new SqlConnection();
        con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();

        con.Open ();

Step-2 : Retrieve data from Database table using SqlCommand Class.

SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from deltable";

        cmd.Connection = con;

Step-3 : Create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, instead of directly using a constructor.

SqlDataReader rd = cmd.ExecuteReader();

Step-4: Read data from Read() method 

while (rd.Read())
        {

            snolabel.Text += "sno=" + rd.GetValue(0) + "<br/>";
            name.Text += "name=" + rd.GetValue(1) + "<br/>";
            Addlabel.Text += "Address=" + rd.GetValue(2) + "<br/>";
 
        }
 Step-5: Close reader.

Complete code


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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="GetData"
        Width="123px" />
    <br />
    <br />
    <asp:Label ID="snolabel" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <div>
   
    <asp:Label ID="name" runat="server"></asp:Label>
   
        <br />
   
    </div>
    <p>
        <asp:Label ID="Addlabel" runat="server"></asp:Label>
    </p>
    </form>
</body>

</html>

Codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;

public partial class binding : System.Web.UI.Page
{
    int sno = 0;
    string Name = String.Empty;
    String Address = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
        con.Open ();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from deltable";
        cmd.Connection = con;
        SqlDataReader rd = cmd.ExecuteReader();
       
        while (rd.Read())
        {

            snolabel.Text += "sno=" + rd.GetValue(0) + "<br/>";
            name.Text += "name=" + rd.GetValue(1) + "<br/>";
            Addlabel.Text += "Address=" + rd.GetValue(2) + "<br/>";
 
        }
      
        rd.Close();
      
      



    }

   
}
Output
How to use SqlDataReader class in ASP.NET

© Copyright 2013 Computer Programming | All Right Reserved