Wrap your dropdown code in if(!IsPostBack){...}
On each grid update the dropdownlist on the page appends an additional list of items
Вопрос
This is quite funny and I do not understand what is happening. I have a radgrid on an asp.net page and a dropdownlist completely independent from the grid (at least this is my impression).
This is the dropdownlist and the full code I have in pageLoad:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["IsLogin"] == null)
{
Response.Redirect("Default.aspx");
}
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from Pagamenti ORDER BY [Data] DESC";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "Pagamenti");
string spacer = " --|-- ";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DropDownList1.Items.Add(string.Format("{0:Id ####}", ds.Tables[0].Rows[i]["Id"]) +
spacer + Convert.ToDateTime(ds.Tables[0].Rows[i]["Data"]).ToString("dd/MM/yyyy") +
spacer + string.Format("{0:R #.###,##}", (ds.Tables[0].Rows[i]["Somma"])));
}
con.Close();
}
Every time I make an operation that updates the grid (insert, delete, update, refresh) an additional list of items is added to the dropdownlist. So on a fresh page I have 50 items, after an update of the grid they became 100 and so on. I do I solve this problem?
Решение
Другие советы
Check if its an update with
if(!IsPostback)
{
//yourcodehere
}
or delete it everytime. But better take care about the ASP.NET lifecycle. (You should allways check for PostBack!) A postback occurs if the client does a action which sends a request to the server.