-->

Thursday, October 3, 2013

Change BackColor of CheckBox dynamically in ASP.NET

If you want to change back-color of the checkbox programmatically then you should use back-color  property at codebehind.
lets take a simple example for completing the task. Take a webform control and place one checkBox and one button control onto the design window. Now at time we can handle onClick event.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!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">
    <div>
        <asp:CheckBox ID="CheckBox1" runat="server"
            Text="CheckBox for multiple selection" />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Change back color" />
    </div>
    </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;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox1.BackColor = System.Drawing.Color.Green;
    }
}
Output
Change BackColor of CheckBox dynamically in ASP.NET

Change BackColor of CheckBox dynamically in ASP.NET

CheckBox Text alignment change dynamically in ASP.NET

CheckBox Text alignment change dynamically in ASP.NET. By-=default text is aligned to left side . you can change using TextAlign enumeration property. Take a webfrom control and place two button and one checkBox control onit.

lets take an simple example

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

<!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">
    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Gender" />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Left "
            Width="59px" />
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Right"
            Width="59px" />
    </div>
    </form>
</body>

</html>
Codebehind 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 Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox1.TextAlign = TextAlign.Left;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        CheckBox1.TextAlign = TextAlign.Right;
    }

}

Output


Wednesday, October 2, 2013

How to bind ListBox using Array in ASP.NET

In the previous article, we have already read more about ListBox control. Also learn its properties and methods. Today, I am talking about array and how to bind array with the list box. Array is similar type collection, in which we can store same type(int, float, char, string etc) value. Its looking like a box, it has multiple other box. So in this example, first to take a string type array with some values and save these value into the listBox. Here array work as a data source.

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

<!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">
    <div>
 
        <asp:ListBox ID="ListBox1" runat="server" Height="143px" Width="157px">
        </asp:ListBox>
   
    </div>
    </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;

public partial class bindlistbox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] arr = new string[] { "Label Control", "Button control", "textBox Control" };
        ListBox1.DataSource = arr;
        ListBox1.DataBind();

    }



}
Output
How to bind ListBox using Array in ASP.NET

How to delete multiple rows from GridView using CheckBox in ASP.NET

Download the Source file

If you want to delete multiple rows from GridView in ASP.NET. Follow some for this task.
Step-1: Take a GridView Control and one Button control onto the Design window.
Step-2 : Set AutoGenerated column property is to false.
Step-3 : Make some columns like

<Columns>
        <asp:TemplateField HeaderText ="Action">
        <ItemTemplate >
            <asp:CheckBox ID="Chkdelete" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>

           <asp:TemplateField HeaderText ="Id" Visible =false>
        <ItemTemplate >
            <asp:Label ID="snolbl" runat="server" Text='<%# Eval("sno") %>' />
        </ItemTemplate>
        </asp:TemplateField>

similarly bind all column onto label control. 
     
Here is the first template field take a one checkbox control and antother field take one label control.

Step-4: Bind GridView on Page_Load() event
Step-5: Find checkbox control from GridView Rows also find Unique table column (sno).
Step-6 : Perform Delete operation

Lets take a simple example
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="deleteItem.aspx.cs" Inherits="deleteItem" %>

<!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">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
        <Columns>
        <asp:TemplateField HeaderText ="Action">
        <ItemTemplate >
            <asp:CheckBox ID="Chkdelete" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>


          

<asp:TemplateField HeaderText ="Id" Visible =false>
        <ItemTemplate >
            <asp:Label ID="snolbl" runat="server" Text='<%# Eval("sno") %>' />
        </ItemTemplate>
        </asp:TemplateField>



         <asp:TemplateField HeaderText ="Name">
        <ItemTemplate >
            <asp:Label ID="namelbl" runat="server" Text='<%# Eval("Name") %>' />
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText ="Address">
        <ItemTemplate >
      <asp:Label ID="addlbl" runat="server" Text='<%# Eval("Name") %>' />
        </ItemTemplate>
        </asp:TemplateField>
        
        </Columns>
        </asp:GridView>
   
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Delete Multiple rows" />
   
    </div>
    </form>
</body>

</html>

Codebehid Code
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;

public partial class deleteItem : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadgrid();
        }
    }

    private void loadgrid()
    {
        SqlCommand cmd = new SqlCommand("Select * from deltable",con);
        con.Open();
        SqlDataReader rd = cmd.ExecuteReader();
        GridView1.DataSource = rd;
        GridView1.DataBind();
        con.Close();

       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow  row in GridView1 .Rows)
        {
            var check = row.FindControl("Chkdelete") as CheckBox;
            if (check .Checked)
            {
                var id = row.FindControl("snolbl") as Label;
                SqlCommand cmd = new SqlCommand("delete from deltable where sno=@s", con);
                cmd.Parameters.AddWithValue("@s", id.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

               
            }
           
        }
        loadgrid();
    }
}


Output
How to delete multiple rows from GridView using CheckBox in ASP.NET
How to delete multiple rows from GridView using CheckBox in ASP.NET
How to delete multiple rows from GridView using CheckBox in ASP.NET



How to bind DropDownlist using AddRange method in ASP.NET

Introduction

AddRange ( ) method 

Adds the items in an array of System.Web.UI..WebControls.ListItem objects to the collection or we can say add items to the array using list of item. Here we take a simple example for binding dropdownlist using list of item.
Step-1: Drop one DropDownlist to the design window.
Step-2: Take a array of ListItem also overload with ListItem class.
Step-3: Add item to the ListItem class constructor.
Step-4: Bind DropDownList using AddRange method.

Example of AddRange Method

Source 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>

</html>

Codebehind 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)
    {
        ListItem[] arr = new ListItem[]
        { new ListItem("apple", "20"),
            new ListItem("mango", "15"),
            new ListItem("orange", "30") };
             
        DropDownList1.Items.AddRange(arr);


    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "Your selected item detail are <br/>Item Text : " + DropDownList1.SelectedItem.Text + "<br/>Item value " + DropDownList1.SelectedValue;

    }

}

Output
How to bind DropDownlist using AddRange method in ASP.NET

Tuesday, October 1, 2013

Built in types in c#

There are many Built-in type in C#

Boolean Type  : In Boolean type  variable can hold only true or false value whether in many other programming language a boolean type variable can hold 0 or 1 also. If you would assign any integer value then compiler get error in the code.

Integeral Type : sbyte , byte, short, ushort, int, uint, long, ulong, char. If you want to know that how much bytes a Integer can hold. Check it now in my previous post. Decimal precision is not available in integeral data type. So for decimal value we should use floating type variable.  

Floating type : float and double. If you want to use some precision type value then you choose floating type. A floating type can hold 32bit and double can 64bit value.

float: 7 precision
double : 15-15 precision. 

Decimal Type : A decimal data type much have precision value approx 28-29 significant digits. Also a Double data type can hold 128bit value. 

String type : A string is basically enclosed in double quotes.If you want to store a name of person then you should use string type variable. such as

string name="jacob";

here is the first quote define the beginning of the string and last quote define the end of the string.If you will run this type then output comes without quote string. If you want to print string in double quote then you should use "\" (backslash) or you can say use escape sequence character. lets take a simple example

string name= "\"jacob\"";

Escape sequence character : there are special character that has special meaning in c# compiler. Also you can say its a non printable character such as line break , carriage return. According to msdn article there are some special escape sequence characters.these are
\a : bell(alert)
\b : backspace
\f : formfeed
\n: newline
\r: carriage return
\t: Horizontal tab
\v : vertical tab.
\' : single quotation mark
\" : Double quotation mark.
\\ : backslash.

Example of Boolean Type :

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool yes = true;
            System.Console.WriteLine(yes);
            Console.ReadKey();

        }
    }
}

Output
Boolean Type output in c#

1

Note : Get error in the code if you assign any other number to boolean type

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool yes = 0;
            System.Console.WriteLine(yes);
            Console.ReadKey();

        }
    }
}

Output

Get error in the code if you assign any other number to boolean type

Example of floating type value and decimal type values

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

namespace ConsoleApplication2
{
    class Program
    {
       static void Main(string[] args)
        {
            float f = 1.22222244444444f;
            Console.WriteLine(f);
            Console.ReadKey();
        }
    }

}
Output
Float type variable

How to change Text color using colorDialog in windows form

Introduction : allows the user to pick a color exposed by the Color property of type System.Drawing.Color.

If you want to change TextColor using colorDialog. WinForms ships with several standard dialogs (sometimes known as "common dialogs") provided as components from the System.Windows. Forms namespace. A component is like a control in that you can drag it from the Toolbox onto a design surface and set its properties using the Property Browser. However, unlike a control, a component doesn't render in a region. Instead, it shows up on the tray along the bottom of the design surface so that you can choose it, and it isn't shown at run time at all.

Step-1 : Take a TextBox and button control to the winform.
Step-2 :  Handle Button_click event for generating colorDialog and pick color from colorDialog.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CoffieShop
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog cld = new ColorDialog();
            DialogResult dr = cld.ShowDialog();
            if (dr ==DialogResult .OK)
            {
                textBox1.ForeColor = cld.Color;
           
            }
        }
    }
}

Output
How to change Text color using colorDialog in windows form
How to change Text color using colorDialog in windows form
How to change Text color using colorDialog in windows form



© Copyright 2013 Computer Programming | All Right Reserved