Retain (Keep) DropDownList selected value on PostBack using C# and VB.Net in ASP.Net

Last Reply 15 days ago By pandeyism

Posted 16 days ago

In below code when i am selecting a value from dropdown list after postback the dropdown list select first value of list but not the actual selected value. so please help me.

<td>
    <asp:DropDownList CssClass="form-control dropdown" ID="ddl_nprice" runat="server"
        DataSourceID="SqlDataSource4" DataTextField="nozzle_price" DataValueField="company_email"
        AutoPostBack="true">
    </asp:DropDownList>
    <asp:SqlDataSource runat="server" ID="SqlDataSource4" ConnectionString='<%$ ConnectionStrings:Mycon %>'
        SelectCommand="SELECT [company_email], [nozzle_lph], [nozzle_price] FROM [company_sprinkler_data] WHERE (([company_email] = @company_email) AND ([nozzle_lph] != @nozzle_lph) AND ([nozzle_price] != @nozzle_price))">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddl_comp" PropertyName="SelectedValue" Name="company_email"
                Type="String"></asp:ControlParameter>
            <asp:Parameter DefaultValue="0" Name="nozzle_lph" Type="Int32"></asp:Parameter>
            <asp:Parameter DefaultValue="0" Name="nozzle_price" Type="Double"></asp:Parameter>
        </SelectParameters>
    </asp:SqlDataSource>
</td>

 

Posted 16 days ago Modified on 16 days ago

Hi nid@patel,

You need to take a HiddenField and SelectedIndexChanged assigned value to hiddend field and then assigned to dropdownlist on pageload.

Refer below code.

HTML

<asp:DropDownList ID="ddl_nprice" runat="server" AutoPostBack="true" DataTextField="nozzle_price"
    DataValueField="company_email" DataSourceID="SqlDataSource4" OnSelectedIndexChanged="ddl_nprice_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource runat="server" ID="SqlDataSource4" ConnectionString='<%$ ConnectionStrings:Mycon %>'
    SelectCommand="SELECT [company_email], [nozzle_lph], [nozzle_price] FROM [company_sprinkler_data] WHERE (([company_email] = @company_email) AND ([nozzle_lph] != @nozzle_lph) AND ([nozzle_price] != @nozzle_price))">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddl_comp" PropertyName="SelectedValue" Name="company_email"
            Type="String"></asp:ControlParameter>
        <asp:Parameter DefaultValue="0" Name="nozzle_lph" Type="Int32"></asp:Parameter>
        <asp:Parameter DefaultValue="0" Name="nozzle_price" Type="Double"></asp:Parameter>
    </SelectParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="hfSelecteValue" runat="server" />

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(hfSelecteValue.Value))
    {
        ddl_nprice.ClearSelection();
        ddl_nprice.Items.FindByText(hfSelecteValue.Value).Selected = true;
    }
}

protected void ddl_nprice_SelectedIndexChanged(object sender, EventArgs e)
{
    hfSelecteValue.Value = ddl_nprice.SelectedItem.Text;
}

VB.Net

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handle Me.Load
        If Not String.IsNullOrEmpty(hfSelecteValue.Value) Then
            ddl_nprice.ClearSelection()
            ddl_nprice.Items.FindByText(hfSelecteValue.Value).Selected = True
        End If
    End Sub

    Protected Sub ddl_nprice_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        hfSelecteValue.Value = ddl_nprice.SelectedItem.Text
    End Sub