2015-10-06 17 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");} 
}