在向数据源添加项目时,刷新下拉列表而不重新加载页面

时间:2015-12-07 16:27:31

标签: asp.net vb.net

我有一个FormView,可以更新,删除和插入数据库中的行。 我使用DropDownList从表中获取所有ID,并根据SelectedValue填充FormView的控件。

当从FormView将新行插入表时,我希望使用新ID更新ID DropdownList。同样,删除行时,应从ID DropDownList中删除已删除的ID。目前,我通过调用

来实现这一目标
Response.Redirect(Request.RawUrl, False)

重新加载整个页面,但我觉得这不是一个好习惯。

我尝试过将ID DropDownList和FormView放入更新面板并使用以下代码。

Protected Sub InsertButton_Click(sender As Object, e As EventArgs)
    ''Response.Redirect(Request.RawUrl, False) ''Current Solution
    SqlDataSource2.Select(DataSourceSelectArguments.Empty)
    IDLookupDDL.DataBind()        
End Sub

我还尝试了itemInserting事件中的数据绑定。

Protected Sub FormView1_ItemInserting(sender As Object, e As FormViewInsertEventArgs) Handles FormView1.ItemInserting        
    SqlDataSource2.Select(DataSourceSelectArguments.Empty)
    IDLookupDDL.DataBind()        
End Sub

另外,我试过没有调用SqlDataSource.select()

1 个答案:

答案 0 :(得分:3)

使用FormView的ItemInsertedItemDeleted事件而不是ItemInserting事件:

List<SimpleQuestion> listOfQ = {from user}
List<Question> testQuestions = {from db}
List<Question> correctAnswers = new List<Question>();
for (int i = 0; i < listOfQ.Count(); i++)
{
   var item = testQuestions.Where(t => t.QuestionId.ToString() == listOfQ[i].QuestionId).FirstOrDefault();
   if (item != null)
   {
     if (listOfQ[i].Answer == item.CorrectAnswer.ToString())
     {
        correctAnswers.Add(item);
     }
   }               
}
相关问题