Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

SelectedIndexChanged for DropDown not fired when first item selected

$
0
0
I wish it was a little easier to post screenshots here, but what's going on is my SelectedIndexChanged event handler is being triggered if I choose any item in the dropdown OTHER THAN THE TOP ONE. When I choose the top item in the dropdown, Page_Load is getting fired, but not SelectedIndexChanged. What's going on?

<%@ControlLanguage="C#"AutoEventWireup="true"CodeBehind="First2023WebUserControl.ascx.cs"Inherits="First2023Control.UserControls.First2023WebUserControl"%><asp:LabelID="Label1"runat="server"Text="Hello Year 2023"></asp:Label><p> </p>
Category:<asp:DropDownListID="ddlCategory"runat="server"AutoPostBack="True"OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged"></asp:DropDownList><p>
    Subcategory:<asp:DropDownListID="ddlSubcategory"runat="server"AutoPostBack="True"></asp:DropDownList></p><p><asp:LabelID="Label2"runat="server"Text="Label"></asp:Label></p><asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Button"/><asp:LabelID="Label3"runat="server"Text="Label"></asp:Label>


protectedvoid Page_Load(object sender, EventArgs e)
{

        Label3.Text = "In Page Load selected index is " + ddlCategory.SelectedIndex.ToString();
        XXXDbSupport objDBSupport = new XXXDbSupport();
        DbCommand objDBCommand = objDBSupport.CreateDbCommand();
        objDBCommand.CommandType = CommandType.Text;string sSQL = "select distinct isnull(Category,'(blank)') as ProdCat from xxx_product order by isnull(Category,'(blank)')";
        objDBCommand.CommandText = sSQL;
        DataTable objDBTable = objDBSupport.FillDataTable(objDBCommand);if (objDBTable.Rows.Count > 0)
        {
            ddlCategory.DataTextField = "ProdCat";
            ddlCategory.DataValueField = "ProdCat";
            ddlCategory.DataSource = objDBTable;
            ddlCategory.DataBind();
        }else
        {
            ddlCategory.Text = "n/a";
        }

}

protectedvoid ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    Label2.Text = "The selected index changed to " + ddlCategory.SelectedIndex.ToString();
    XXXDbSupport objDBSupport = new XXXDbSupport();
    DbCommand objDBCommand = objDBSupport.CreateDbCommand();
    objDBCommand.CommandType = CommandType.Text;string sSQL = "select distinct isnull(SubCategory,'(blank)') as SubCat from xxx_product ";if (ddlCategory.Text == "(blank)")
    {
        sSQL = sSQL + "where isnull(Category,'(blank)') = '(blank)' order by isnull(SubCategory,'(blank)')";
    }else
    {
        sSQL = sSQL + "where Category = '" + ddlCategory.Text + "' order by isnull(SubCategory,'(blank)')";
    }
    objDBCommand.CommandText = sSQL;
    DataTable objDBTable = objDBSupport.FillDataTable(objDBCommand);if (objDBTable.Rows.Count > 0)
    {
        ddlSubcategory.DataTextField = "SubCat";
        ddlSubcategory.DataValueField = "SubCat";
        ddlSubcategory.DataSource = objDBTable;
        ddlSubcategory.DataBind();
    }else
    {
        ddlSubcategory.Text = "n/a";
    }

}

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>