ASP.NET DropDownList SelectedIndexChaned Event | AutoPostBack Property

DropDownListWithAutopostback.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownListWithAutopostback.aspx.cs" Inherits="WebDemo01.DropDownListWithAutopostback" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h3>Inventory Details</h3>
            <table style="width:auto;">
                <tr>
                    <td>Product Name</td>
                    <td>
                        <asp:TextBox ID="txtName" runat="server"                                   Width="170px"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Select Product Category</td>
                    <td>
                        <asp:DropDownList ID="ddlProductCategory"                                   runat="server" Width="180px" 
                         AutoPostBack="True" OnSelectedIndexChanged=
                             "ddlProductCategory_SelectedIndexChanged">
                        </asp:DropDownList>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Select Measure</td>
                    <td>
                        <asp:DropDownList ID="ddlMeasure" 
                        runat="server" Width="180px">
                        </asp:DropDownList>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Enter Quantity</td>
                    <td>
                        <asp:TextBox ID="txtQuanityt" 
                         runat="server" Width="170px">
                        </asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" 
                         Text="Enter Price"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" 
                          runat="server" Width="170px"></asp:TextBox>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Button ID="btnSubmit" 
                            runat="server" Text="Submit" Width="180px" />
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="3">
                        <asp:Label ID="lblMsg" runat="server" 
                           Text="Save Status...."></asp:Label>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Mandatory Actions

1. Select DropDown List set AutoPostBack Property to true.

2. Double click the DropDownList to get SelectedIndexChanged Event.


DropDownListWithAutopostback.aspx.cs

using System;

namespace WebDemo01
{
    public partial class DropDownListWithAutopostback : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) //Getting loaded very first time
            {
                ddlProductCategory.Items.Add("Solid Items");
                ddlProductCategory.Items.Add("Liquid Items");
            }
        }
       
        protected void ddlProductCategory_SelectedIndexChanged
                           (object sender, EventArgs e)
        {
            ddlMeasure.Items.Clear();

            if (ddlProductCategory.SelectedItem.Text.Equals
                                         ("Solid Items"))
            {
                ddlMeasure.Items.Add("100Gms");
                ddlMeasure.Items.Add("200Gms");
                ddlMeasure.Items.Add("500Gms");
                ddlMeasure.Items.Add("01 #Kg");
                ddlMeasure.Items.Add("02 Kgs");
                ddlMeasure.Items.Add("05 Kgs");
                ddlMeasure.Items.Add("10 Kgs");
            }
            else if (ddlProductCategory.SelectedItem.Text.Equals
                                            ("Liquid Items"))
            {
                ddlMeasure.Items.Add("100ml");
                ddlMeasure.Items.Add("250ml");
                ddlMeasure.Items.Add("250ml");
                ddlMeasure.Items.Add("1 Ltr");
                ddlMeasure.Items.Add("2 Ltr");
                ddlMeasure.Items.Add("5 Ltr");
            }
        }
    }
}

No comments:

Post a Comment