Sponsored Ad

Wednesday, July 14, 2010

How to restrict a dropdown selection in C#

You can restring a user to select particular value. Here is sample code to implement this using C#. on selection change event you can check for values and if it matches. Chnge the selected index to 0.

 

How to restrict a dropdown selection in C#

How to restrict a dropdown selection in C# Example:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<script runat="server">

    protected void cmbMyList_SelectedIndexChanged(object sender, EventArgs e)
    {   
        Response.Write("You Have Selected: " + cmbMyList.SelectedValue + "<br>");
        if (cmbMyList.SelectedValue.ToString() == "Select 2")
        {
            Response.Write("You are not allowed to select 'Select 2'" );
            cmbMyList.SelectedIndex = 0;
        }      
    }
</script>

<html xmlns="">
<head>
    <title>
     </title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           <asp:DropDownList ID="cmbMyList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cmbMyList_SelectedIndexChanged">
           <asp:ListItem Text="Select 1" Value="Select 1"></asp:ListItem>
           <asp:ListItem Text="Select 2" Value="Select 2"></asp:ListItem>
           <asp:ListItem Text="Select 3" Value="Select 3"></asp:ListItem>
           <asp:ListItem Text="Select 4" Value="Select 4"></asp:ListItem>
           </asp:DropDownList>
        </div>
    </form>
</body>
</html>

No comments:

Post a Comment

Sponsored Ad

Development Updates