【问题标题】:Set Dropdownlist value in listview on itemdatabound在 itemdatabound 的 listview 中设置 Dropdownlist 值
【发布时间】:2010-04-02 12:30:47
【问题描述】:

我有一年的下拉列表是动态的。我已经使用对象数据源填充了下拉列表。在插入列表视图控件时它工作正常。但是当我单击编辑按钮时,应该设置来自数据库的下拉列表值。 例如如果我有一行包含 Year=2006 和 month="Jan" 然后点击编辑按钮,这些下拉列表应该被填满。

我已经在 ItemDataBound 中编写了代码来设置 dropdownlilst 的值。但是当我使用 findcontrol 时,它取空,所以对象引用错误即将到来。所以请给我解决方案。

谢谢

萨米尔

【问题讨论】:

    标签: asp.net listview itemdatabound


    【解决方案1】:
     protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
     {
         if (e.Item.ItemType == ListViewItemType.DataItem)
         {
              DropDownList ddl = (DropDownList)e.Item.FindControl("nameOfDDLOnAspxPage");
              ddl.SelectValue = (however you are getting the year data for this row);
         }
     }
    

    【讨论】:

      【解决方案2】:

      我写了下面的代码

      protected void ListView_Articles_ItemDataBound(object sender, ListViewItemEventArgs e) {

              if (e.Item.ItemType == ListViewItemType.DataItem)
              {
                  if (cmd == "edit")
                  {
                      // Display the e-mail address in italics.
                      int month, year;
                      month = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem,"Created")).Month;
                      year = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem, "Created")).Year;
                      ListViewDataItem item = (ListViewDataItem)e.Item;
      
                      DropDownList ddlmonth = (DropDownList)e.Item.FindControl("ddlmonth");
                      DropDownList ddlyear = (DropDownList)e.Item.FindControl("ddlyear");
                      ListItem lstitem = ddlyear.Items.FindByValue(year.ToString()); 
      

      // 我发现 ddlyear 为空,所以无法绑定数据。

      if (ddlmonth != null)
                      {
                          foreach (ListItem monthitem in ddlmonth.Items)
                          {
                              if (int.Parse(monthitem.Value) == month)
                              {
                                  ddlmonth.ClearSelection();
                                  monthitem.Selected = true;
                                  return;
                              }
                          }
                      }
                      if (ddlyear != null)
                      {
                          foreach (ListItem yearitem in ddlyear.Items)
                         {
                             if (int.Parse(yearitem.Value) == year)
                              {
                                 ddlyear.ClearSelection();
                                  yearitem.Selected = true;
                                  return;
                              }
                          }
                      }
                  }
      
              }
      
      
          }
      

      【讨论】:

      • 我已经解决了这个问题。删除 foreach 循环,而不是写 ddlyear.selectedvalue=year,月份也一样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 2021-10-30
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      相关资源
      最近更新 更多