Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从数据库填充dropdownlist_C#_Asp.net_Drop Down Menu - Fatal编程技术网

C# 从数据库填充dropdownlist

C# 从数据库填充dropdownlist,c#,asp.net,drop-down-menu,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]); } } 这是我的业务层。方法 pub

我的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
如何防止这种情况发生

将以下内容替换为

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


希望答案会有帮助。让我知道任何进一步的帮助,请将其标记为答案,以便其他人可以从中受益
protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
    fillDD();
   }
}