user11621331
user11621331

Reputation:

DropDownList Items repeat its value after every post-back

Dropdown shows the below value after binding:

Dropdown show the below value after every postback:

Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
   DropDownList1.DataSource = db.ComplaintTypes.ToList();
   DropDownList1.DataTextField = "ct_Name";
   DropDownList1.DataBind();

   cboCpriority.DataSource = db.ComplaintPriorities.ToList();
   cboCpriority.DataTextField = "cp_Desc";
   cboCpriority.DataBind();

   ...
}      

Upvotes: 2

Views: 130

Answers (1)

Tanveer Badar
Tanveer Badar

Reputation: 5522

You should use IsPostBack property to only bind during first load, since values will be preserved through view state after that.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        DropDownList1.DataSource = db.ComplaintTypes.ToList();
        DropDownList1.DataTextField = "ct_Name";
        DropDownList1.DataBind();

        cboCpriority.DataSource = db.ComplaintPriorities.ToList();
        cboCpriority.DataTextField = "cp_Desc";
        cboCpriority.DataBind();
    }
}

Upvotes: 5

Related Questions