2015-10-06 40 views
0

我有一个不能用于编辑目的的下拉列表。当按钮Edit在数据存在的listview内部被点击时,数据应该返回到下拉列表和其他文本框,其中该表格位于listview之外。将数据传回文本框是可以的。问题是我想编辑的dropdownlist数据被添加到dropdownlist中作为另一个记录。请拍一张照片,我必须重新选择正确的照片。否则,选择的数据(例如图片中的12月)没有数据值字段,如果我没有选择12月底,并且点击更新按钮,它将停止运行。这是我的代码为dropdownlist几个月。任何帮助表示赞赏。谢谢。将数据传回Dropdownlist的错误asp .net webform

public void BindMonth() 
{ 
    ddlStartMonth.DataSource = objUIHelpers.GetAllMonths(); 
    ddlStartMonth.DataTextField = "StartMonthName"; 
    ddlStartMonth.DataValueField = "MonthId"; 
    ddlStartMonth.DataBind(); 
    ddlStartMonth.Items.Insert(0, "Select Start Month");} 

然后,我把这种方法在页面加载这样。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     BindMonth(); 
    } 
} 

这是列表视图数据项编辑

protected void lvEducation_ItemCommand(object sender, ListViewCommandEventArgs e) 
{ 
    switch (e.CommandName) 
    { 
     //Delete Method will be fired when command name "Delete" inside Listview is clicked. 
     case ("Delete"): 

      int EducationId = Convert.ToInt32(e.CommandArgument);//pass Id of Experience to identify datarow to delete 
      // DeleteEducationById(ExperienceId);//Call bind to delete method and pass ExperienceId as argument 

      break; 

     //Edit Method will fired when command name "Edit" inside Listview is clicked. 
     case ("Edit"): 
      EducationId = Convert.ToInt32(e.CommandArgument); //pass Id of Experience to identify datarow to edit 
      BindEducationDataToEdit(EducationId);//Call bind to edit method and pass ExperienceId as argument 
      break; 
    }} 

这是方法的一部分触发回传数据进行编辑。

public void BindEducationDataToEdit(int EducationId) 
{ 
    Education edu = objJFUserBAL.GetEducationByIdToEdit(EducationId); 

    txtAdditionalInfo.Text = edu.AdditionalInfo.ToString(); 
    ddlEndMonth.SelectedItem.Text = edu.mo.EndMonthName; 
    } 

当选定的数据回发进行编辑时,我有这样的额外数据。 enter image description here

回答

1

你不应该更新SelectedItem.Text。这正在改变显示的文字。相反,您应该更新选择哪个项目。

如果您没有访问月份名称的值,你可以做到以下几点:

ddlEndMonth.Items.FindByText(edu.mo.EndMonthName).Selected = true; 

将与本月文本假定存在一个选择的项目。

如果可能在项目列表中不存在edu.mo.EndMonthName,那么您需要对null进行一些检查并进行相应处理。

+0

非常感谢。它适用于您的代码。我的问题刚刚解决。我在你的上面增加了一行代码。 'ddlEndMonth.ClearSelection();'清除原始选择;否则,它会抛出ddl中的多重选择异常。谢谢。 – jkhaung

0

您必须手动填充列表,因为自动捆绑是不会让你把一个“选择你的月”项目,除非你有一个在你的数据库:

public void BindMonth() 
{ 

    List<Month> listOfMonth = new List<Month>(); 
    Month fakeMonth = new Month(); 

     // you need to see your own 
     //code and try to make a fake month with these parameters you want 

    fakeMonth.StartMonthName = "Select Start Month"; 
    fakeMonth.MonthId = 0; 


    listOfmounth.Add(fakeMonth); 

    foreach(Month m in objUIHelpers.GetAllMonths()) 
    { 
     listOfMonth.Add(m) 
    } 

    ddlStartMonth.DataSource = listOfMonth; 
    ddlStartMonth.DataTextField = "StartMonthName"; 
    ddlStartMonth.DataValueField = "MonthId"; 
    ddlStartMonth.DataBind(); 
    ddlStartMonth.Items.Insert(0, "Select Start Month");} 
}