Senin, 30 September 2013

Change Border Color dynamically in ASP.NET

There is two method for change border color of dropdownlist in asp.net . In first method you can use System.Drawing.Color.Blue enumeration for changing color.

DropDownList1.BorderColor = System.Drawing.Color.Red;
In Second method you can use DropDownList1.Attributes.Add() method.

DropDownList1.Attributes.Add("style", "value");

Lets take an simple example . Drop one dropdownlist control to the design page and handle  SelectedIndexChanged event.


<%@ 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 DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList1.Attributes.Add("style", "border-color:" + DropDownList1.SelectedItem.Text);
        DropDownList1.BorderWidth = 5;
        DropDownList1.BorderStyle = BorderStyle.Dashed;
     
     
    }
</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" AutoPostBack="True"
            Height="22px" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
            Width="223px">
            <asp:ListItem>Red</asp:ListItem>
            <asp:ListItem>Green</asp:ListItem>
            <asp:ListItem>Black</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Output
Change Border Color dynamically in ASP.NET

Tidak ada komentar:

Posting Komentar