【问题标题】:Error on passing data back to Dropdownlist asp .net webform将数据传递回Dropdownlist asp .net webform时出错
【发布时间】:2015-10-06 14:08:16
【问题描述】:

我有一个不能用于编辑目的的下拉列表。在存在数据的列表视图中单击“编辑”按钮时,数据应该传回下拉列表和表单位于列表视图之外的其他文本框。将数据传回文本框是可以的。问题是我要编辑的下拉列表数据作为另一条记录添加到下拉列表中。请在图片中掠夺,我必须重新选择正确的。否则,所选数据(例如图片中的十二月)没有数据值字段,如果我没有选择底部十二月并单击更新按钮,它将停止运行。这是我几个月的下拉列表代码。对此的任何帮助表示赞赏。谢谢。

   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;
    }

当所选数据发布回编辑时,我有这样的额外数据。

【问题讨论】:

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


    【解决方案1】:

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

    如果您无权访问月份名称的值,您可以执行以下操作:

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

    假设存在一个,它将选择带有月份文本的项目。

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

    【讨论】:

    • 非常感谢。它适用于您的代码。我的问题刚刚解决。我在你的上面又添加了一行代码。 ddlEndMonth.ClearSelection(); 清除原始选择;否则,它会在 ddl 中引发多选异常。谢谢。
    【解决方案2】:

    您必须手动填写列表,因为自动绑定不会让您放置“选择您的月份”项目,除非您的数据库中有一个:

    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");}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 2013-01-07
      • 1970-01-01
      相关资源
      最近更新 更多