【问题标题】:Remove item from dropdown permanently inside the Gridview从 Gridview 内的下拉列表中永久删除项目
【发布时间】:2017-03-07 16:35:08
【问题描述】:

我在 Gridview 中有下拉列表,例如,

<asp:TemplateField HeaderText="Leave Category" >
    <ItemTemplate>
        <asp:DropDownList ID="LCList" runat ="server"  AutoPostBack="true" OnSelectedIndexChanged="LCList_TextChanged"/>
    </ItemTemplate>
</asp:TemplateField>

并添加类似的项目,

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList LCList = (e.Row.FindControl("LCList") as DropDownList);
        LCList.Items.Insert(0, new ListItem("casual Leave"));
        LCList.Items.Insert(1, new ListItem("sick Leave"));
        LCList.Items.Insert(2, new ListItem("LOP"));
    }
}

如果我选择 LOP 一次,它应该从网格的选定行下拉列表中删除。不会再显示了。

上面的代码正在运行。但是如果我刷新页面,它正在显示

如何解决?

是否需要在任何其他事件中创建项目?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    在回发中(当您刷新页面时),DataBound 方法再次被执行,这就是您看到所有值的原因。在将数据绑定到网格之前,您必须调整数据绑定方法以检查值。

    就在您的 Databind 调用之前...

    // 检查是否有任何 Grid 值选择了 LOP

    if (DataTable.Select(dr => dr.dropDown.Value = "LOP").Count > 0) 
    {
     blnIsLOPselected = True;
    }
    
    DataGrid.DataSource = <dataTable>
    DataGrid.DataBind();
    

    在你的 DataBind 中

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList LCList = (e.Row.FindControl("LCList") as DropDownList);
            LCList.Items.Insert(0, new ListItem("casual Leave"));
            LCList.Items.Insert(1, new ListItem("sick Leave"));
    
            If (!blnIsLOPselected )
            {
               LCList.Items.Insert(2, new ListItem("LOP"));
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      显示它是因为您必须将从 DropDownList 中删除的值存储在某个地方,例如 Session。您必须保存行号和删除的值,然后在构建 GridView 时将 DropDown 中的值与已删除的值进行比较。

      //create a list with keyvalue pairs to hold the removed items
      public static List<KeyValuePair<int, string>> itemsRemoved = new List<KeyValuePair<int, string>>();
      
      protected void Page_Load(object sender, EventArgs e)
      {
          //check if the session with the removed items exists and if so cast back to a list
          if (Session["itemsRemoved"] != null)
          {
              itemsRemoved = Session["itemsRemoved"] as List<KeyValuePair<int, string>>;
          }
      
          //bind the gridview
          if (!Page.IsPostBack)
          {
              GridView1.DataSource = Common.LoadFromDB();
              GridView1.DataBind();
          }
      }
      
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          //check if the row is a normal datarow
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              DropDownList LCList = (e.Row.FindControl("LCList") as DropDownList);
      
              //create an array with the dropdown option for easier looping
              string[] listItems = new string[] { "casual Leave", "sick Leave", "LOP" };
      
              for (int i = 0; i < listItems.Length; i++)
              {
                  bool wasRemoved = false;
      
                  //check if the listitem was remove for this row
                  for (int j = 0; j < itemsRemoved.Count; j++)
                  {
                      if (e.Row.RowIndex == itemsRemoved[j].Key && listItems[i] == itemsRemoved[j].Value)
                      {
                          wasRemoved = true;
                      }
                  }
      
                  //if not removed, add it to the dropdownlist
                  if (wasRemoved == false)
                  {
                      LCList.Items.Insert(LCList.Items.Count, new ListItem(listItems[i]));
                  }
              }
          }
      }
      
      protected void LCList_SelectedIndexChanged(object sender, EventArgs e)
      {
          //cast the sender back to a dropdownlist
          DropDownList LCList = sender as DropDownList;
      
          //get tne namingcontainer from the dropdownlist to get the rownumber
          GridViewRow row = (GridViewRow)LCList.NamingContainer;
          int rowIndex = row.RowIndex;
      
          //create a new keyvalue pair with the correct rowindex and selected value
          KeyValuePair<int, string> kv = new KeyValuePair<int, string>(rowIndex, LCList.SelectedValue);
      
          //add it to the list with removals
          itemsRemoved.Add(kv);
      
          //remove from the dropdownlist immediately
          LCList.Items.RemoveAt(LCList.SelectedIndex);
      }
      

      【讨论】:

      • 如果我删除了第二行网格中的 LOP(页面索引 1)。它影响所有网格页面的所有第二行
      • 那么你在session中需要第三个条件,GridView的page或者id。将 KeyValuePair 替换为您自己的包含 3 个属性的类。
      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多