Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a problem that seems very common but I can't solve it. I am doing web forms project with C#, visual studio 2013.
I have used a dropownlist to select type of report. I have used 3 panels which are in updatepanel and the dropdownlist is outside of the Updatepanel. Initially I set visible property of each panel to false. I want to make one of the panels visible when user selects item from dropdownlist. The problem is when I change selection in dropdownlist, the SelectedIndexChanged event fires, but the selected index remains -1 every time. I have set autopostback to true. Following is my code.
C#
<asp:DropDownList ID="ddlReportType" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged" >
    <asp:ListItem Selected="True">-- Select which data you want to enter --</asp:ListItem>
    <asp:ListItem Value="Salse" Text="Salse">Salse Reports</asp:ListItem>
    <asp:ListItem Value="Inventory" Text="Inventory">Inventory Reports</asp:ListItem>
    <asp:ListItem Value="Finance" Text="Finance">Financial Reports</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>

         <asp:Panel ID="pnlProductSale"  runat="server">

             <asp:Label ID="lblCaptionSales" runat="server" Text="Total Product based sale"></asp:Label>
             <table>
                 <tr>
                     <td>
                         Select Product
                     </td>
                     <td>
                          <asp:DropDownList ID="ddlProduct" runat="server"></asp:DropDownList>
                     </td>
                 </tr>
                    </table>


         </asp:Panel>

           <asp:Panel ID="pnlInventory" runat="server">

             <asp:Label ID="lblInventory" runat="server" Text="Inventory"></asp:Label>
             <table>
                 <tr>
                     <td>
                         Select Product
                     </td>
                     <td>
                          <asp:DropDownList ID="ddlProductPnl2" runat="server"></asp:DropDownList>
                     </td>
                 </tr>

             </table>


         </asp:Panel>

        <asp:Panel ID="pnlCashFlow" runat="server">
        <asp:Label ID="lblCaption" runat="server" Text="Cashflow"></asp:Label>
<table>
    <tr>
        <td>
            Deposit at start up
        </td>
        <td>
            Cash on hand :   <asp:TextBox ID="txtStartAmt" Text="0" AutoPostBack="true" runat="server" ></asp:TextBox>
            Credit :  <asp:TextBox ID="txtStartCredit" Text="0" AutoPostBack="true" runat="server" ></asp:TextBox>
            Debit :  <asp:TextBox ID="txtstartDebit" Text="0" AutoPostBack="true" runat="server" ></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:ImageButton ID="imgSave" runat="server" />
        </td>
        <td>
            <asp:ImageButton ID="imgCancel" runat="server" />
        </td>
    </tr>
</table>

            </asp:Panel>


        </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlReportType" />

    </Triggers>
    </asp:UpdatePanel>


following is the code in .cs file.
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            pnlCashFlow.Visible = false;
            pnlProductSale.Visible = false;
            pnlInventory.Visible = false;
        }
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProduct.SelectedIndex == 0)
        {
            Label1.Text = ddlProduct.SelectedValue;
            pnlCashFlow.Visible = false;
            pnlProductSale.Visible = false;
            pnlInventory.Visible = true;
        }
        else if (ddlProduct.SelectedIndex ==1)
        {
            Label1.Text = ddlProduct.SelectedValue;
            pnlCashFlow.Visible = false;
            pnlProductSale.Visible = true;
            pnlInventory.Visible = false;
        }
        else if (ddlProduct.SelectedIndex==2)
        {
            Label1.Text = ddlProduct.SelectedValue;
            pnlCashFlow.Visible = true;
            pnlProductSale.Visible = false;
            pnlInventory.Visible = false;
        }
        else
        {
            Label1.Text = ddlProduct.SelectedValue;
            pnlCashFlow.Visible = true;
            pnlProductSale.Visible = false;
            pnlInventory.Visible = false;
        }
    }

Please help me to solve this. Also tell me if I am using the wrong approach.
Posted
Comments
Ralf Meier 23-Jun-15 3:28am    
I'm not sure if you mean the right thing.
If you only scroll the list of the Control the SelectedIndex does not change. You have to Select an Item (for exemple by clicking on it) to get this Event.
sashje- 23-Jun-15 3:39am    
Shouldn't the trigger also have the attribute EventName="SelectedIndexChanged" in order to work ?

1 solution

Yes. Very big mistake you did. You are handling below event handler to handle ddReportType selected change event
ddlReportType_SelectedIndexChanged


But you are using

C#
ddlProduct.SelectedIndex


in the event handler code.
 
Share this answer
 
v2
Comments
Dolly Nimavat 24-Jun-15 23:17pm    
I got it after posting the question. Thanks for your attention.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900