Click here to Skip to main content
15,886,058 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I have five dropdownlist in my aspx page where there is static values and one button on click event on the button i want the values of dropdownlist in Label but when i m clicking the button the dropdownlist automatically appears the first value,

so in code behind i m getting the first value always,

ASP
<asp:DropDownList ID="drpProduct" runat="server" Width="104%" Style="margin-top: -4.2%; margin-left: 16px">
                                <asp:ListItem Value="--Select Product--"></asp:ListItem>
                                <asp:ListItem Value="All">All Product</asp:ListItem>
                                <asp:ListItem Value="Cotton S6">Cotton S6</asp:ListItem>
                                <asp:ListItem Value="Cotton S5">Cotton S5</asp:ListItem>
                                <asp:ListItem Value="Stone x 1">Stone x 1</asp:ListItem>
                                <asp:ListItem Value="Stone x 2">Stone x 1</asp:ListItem>
                            </asp:DropDownList>

<asp:Button ID="btnSearch" runat="server" CssClass="btn green" Text="Search" ValidationGroup="val" UseSubmitBehavior="false" OnClick="btnSearch_Click" />


And code behind is:

C#
lblProduct.Text = drpProduct.SelectedValue;


Please guide me on this...
Posted
Comments
jaket-cp 10-Feb-15 7:41am    
Where in the page life cycle are you setting lblProduct.Text = drpProduct.SelectedValue;
Maybe the selected index is getting reset somewhere before hand.
ZurdoDev 10-Feb-15 7:49am    
You probably are binding your dropdown in PageLoad without checking for IsPostBack.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // bind your dropdown here
        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Write(drpProduct.SelectedValue);
    }
 
Share this answer
 

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