In this article I will explain with an example, how to prevent the Selected Item Value of ASP.Net DropDownList from getting Lost / Cleared / Reset on PostBack when the Submit button is clicked.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The HTML Markup consists of an ASP.Net DropDownList and a Button.
<asp:DropDownList ID="ddlCustomers" runat="server">
</asp:DropDownList>
<br />
<br />
<asp:Button Text="Submit" runat="server" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
ASP.Net DropDownList Selected Item Value Lost / Cleared / Reset on PostBack
Inside the Page Load event of the page, the DropDownList is populated with the records of the Customers Table.
C#
protected void Page_Load(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name FROM Customers"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
ddlCustomers.DataSource = cmd.ExecuteReader();
ddlCustomers.DataTextField = "Name";
ddlCustomers.DataValueField = "CustomerId";
ddlCustomers.DataBind();
con.Close();
}
}
ddlCustomers.Items.Insert(0, new ListItem("", ""));
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT CustomerId, Name FROM Customers")
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
ddlCustomers.DataSource = cmd.ExecuteReader()
ddlCustomers.DataTextField = "Name"
ddlCustomers.DataValueField = "CustomerId"
ddlCustomers.DataBind()
con.Close()
End Using
End Using
ddlCustomers.Items.Insert(0, New ListItem("", ""))
End Sub
Now when the above code is executed and the Button is clicked, the Selected Item Value of ASP.Net DropDownList is lost / cleared / reset on PostBack.
This happens because the code to populate the DropDownList is not placed inside the Not IsPostBack condition and hence the DropDownList is populated twice i.e. when the page loads first time as well as when the Button is clicked, which clears its ViewState and ultimately the Selected Item Value.
Whenever populating a control from database you need to make sure that it is populated only once i.e. when the page loads first time by placing it inside the Not IsPostBack condition.
Thus after correction the code looks as shown below.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name FROM Customers"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
ddlCustomers.DataSource = cmd.ExecuteReader();
ddlCustomers.DataTextField = "Name";
ddlCustomers.DataValueField = "CustomerId";
ddlCustomers.DataBind();
con.Close();
}
}
ddlCustomers.Items.Insert(0, new ListItem("", ""));
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT CustomerId, Name FROM Customers")
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
ddlCustomers.DataSource = cmd.ExecuteReader()
ddlCustomers.DataTextField = "Name"
ddlCustomers.DataValueField = "CustomerId"
ddlCustomers.DataBind()
con.Close()
End Using
End Using
ddlCustomers.Items.Insert(0, New ListItem("", ""))
End If
End Sub
After placing the DropDownList population code inside the Not IsPostBack condition, the Selected Item Value of ASP.Net DropDownList is lost / cleared / reset on PostBack.
Demo
Downloads