在每个网格更新上,页面上的下拉列表会附加一个项目列表

时间:2014-01-22 15:53:26

标签: c# asp.net radgrid

这很有趣,我不明白发生了什么。我在asp.net页面上有一个radgrid和一个完全独立于网格的下拉列表(至少这是我的印象)。

这是我在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();
}

每次进行更新网格的操作(插入,删除,更新,刷新)时,都会在下拉列表中添加其他项目列表。所以在一个新的页面上我有50个项目,在更新网格后它们变为100,依此类推。 我能解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

将您的下拉代码换入if(!IsPostBack){...}

答案 1 :(得分:1)

检查是否使用

进行更新
if(!IsPostback)
{
//yourcodehere
}

或每次删除它。 但最好还是要关注ASP.NET lifecycle。 (你应该总是检查PostBack!) 如果客户端执行向服务器发送请求的操作,则会发生回发。

相关问题