-->

Thursday, October 3, 2013

Change DropdownList Width Dynamically in ASP.NET

Every control have a width property . In width property you can assign any unit value such as 10, 20 and any other number.If you want to change width of the control at runtime then you must assign width to the DropdownList at runtime such as

DropdownList1.Width = 20;

Lets take an simple example.

<!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.Width =int.Parse("50");
       
    }
</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">
            <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 Width"
            onclick="Button1_Click" />
    </div>
    </form>
</body>

</html>
Output
Change DropdownList Width Dynamically in ASP.NET
Change DropdownList Width Dynamically in ASP.NET


Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Works only on sorted items. such as 
1,2,3,4,5,6  etc

Works on sorted as well as unsorted items.
12,4,5,3,2,1 etc
Very efficient if the items are sorted
Very efficient if the items are less and present in the beginning of the list. such as
Suppose your list items are :
12,3,4,5,1
and you want to search 12 number then you get beginning in the list.
Works well with arrays and not on linked lists.
Works with arrays and linked lists.

Number of comparisons are less
More number of comparisons are required if the items are present in the later part of the array or its elements are more.

How to bind DropdownList using Array in ASP.NET

If you want to bind dropdownlist using array in asp.net then first add item to the array on page_Load event.
After that pass dataSource as array name to DropdownList. lets take a simple example for binding dropdownlist on page load.

<%@ 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>How to bind dropdownlist using array</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
             Height="23px"
            Width="173px">
        </asp:DropDownList>
        <br />
        <br />
    </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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] arr = new string[] { "Your first item", "Your Second Item", "Your third item" };
        DropDownList1.DataSource = arr;
        DropDownList1.DataBind();


    }
 

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

Dynamically change checkbox border color in ASP.NET

If you want to change Border-color of the checkbox programmatically then you should use Border-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" BorderColor="Red"
            BorderStyle="Dotted" BorderWidth="4px" Text="CheckBox for multiple selection" />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Change Border 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.BorderColor = System.Drawing.Color.Green;
    }

}
Output
Dynamically change checkbox border color in ASP.NET

Dynamically change checkbox border color in ASP.NET

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

© Copyright 2013 Computer Programming | All Right Reserved