ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Retain (Keep) DropDownList selected value on PostBack using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="nid@patel"> nid@patel</a>,</p> <p>You need to take a HiddenField and SelectedIndexChanged assigned value to hiddend field and then assigned to dropdownlist on pageload.</p> <p>Refer below code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DropDownList ID=&#34;ddl_nprice&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; DataTextField=&#34;nozzle_price&#34; DataValueField=&#34;company_email&#34; DataSourceID=&#34;SqlDataSource4&#34; OnSelectedIndexChanged=&#34;ddl_nprice_SelectedIndexChanged&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:SqlDataSource runat=&#34;server&#34; ID=&#34;SqlDataSource4&#34; ConnectionString=&#39;&lt;%$ ConnectionStrings:Mycon %&gt;&#39; SelectCommand=&#34;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))&#34;&gt; &lt;SelectParameters&gt; &lt;asp:ControlParameter ControlID=&#34;ddl_comp&#34; PropertyName=&#34;SelectedValue&#34; Name=&#34;company_email&#34; Type=&#34;String&#34;&gt;&lt;/asp:ControlParameter&gt; &lt;asp:Parameter DefaultValue=&#34;0&#34; Name=&#34;nozzle_lph&#34; Type=&#34;Int32&#34;&gt;&lt;/asp:Parameter&gt; &lt;asp:Parameter DefaultValue=&#34;0&#34; Name=&#34;nozzle_price&#34; Type=&#34;Double&#34;&gt;&lt;/asp:Parameter&gt; &lt;/SelectParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;asp:HiddenField ID=&#34;hfSelecteValue&#34; runat=&#34;server&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">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; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb"> 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</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/213777/Retain-Keep-DropDownList-selected-value-on-PostBack-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/213777/Retain-Keep-DropDownList-selected-value-on-PostBack-using-C-and-VBNet-in-ASPNet/Wed, 04 Sep 2019 05:27:30 GMTRetain (Keep) DropDownList selected value on PostBack using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="nid@patel">nid@patel</a>,</p> <p>Refer below link -</p> <p><a href="https://stackoverflow.com/questions/16460891/how-to-clear-exisiting-dropdownlist-items-when-its-content-changes">https://stackoverflow.com/questions/16460891/how-to-clear-exisiting-dropdownlist-items-when-its-content-changes</a></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/213777/Retain-Keep-DropDownList-selected-value-on-PostBack-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/213777/Retain-Keep-DropDownList-selected-value-on-PostBack-using-C-and-VBNet-in-ASPNet/Thu, 05 Sep 2019 03:11:35 GMT