填充数据库中的下拉列表

时间:2014-10-21 09:15:54

标签: c# asp.net drop-down-menu

我的asp.net网页上有一个下拉列表控件和一个按钮控件。 我正在从数据库中填充下拉列表。

我有这个方法来填充下拉列表。

     public void fillDD()
{
    SqlDataReader rd = new ViewBorrowersOP().getTitlestoCombo();
    while (rd.Read())
    {
        DDTitle.Items.Add((String)rd[0]);
    }
}

这是我的业务层。方法。

     public SqlDataReader getTitlestoCombo()
{
    string query1 = "EXEC getAllBKTitles";
    return new DataAccessLayer().executeQuerys(query1);
}

我正在调用页面加载事件中的方法。

 protected void Page_Load(object sender, EventArgs e)
{
    fillDD();
}

我点击按钮来填充数据网格。它工作正常。填充下拉列表的代码也可以正常工作。但每次当我单击按钮以填充网格视图或每次刷新页面时,下拉列表将再次填充相同的项目。

   Let's say for the first time when I load the page the drop down list is populated with following items. 


    CAT
    DOG 
    BAT

如果我点击按钮或者页面重新加载它会再次填充,当这种情况发生三次时,我的下拉列表就像这样。

    CAT
    DOG 
    BAT
    CAT
    DOG 
    BAT
    CAT
    DOG 
    BAT

如何防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

替换下面的内容
 protected void Page_Load(object sender, EventArgs e)
    {
        fillDD();
    }

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
    fillDD();
   }
}
相关问题