2017-03-07 56 views
3

我有在GridView里面像下拉列表中删除项目,从下拉列表永久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一次,它应该被删除从网格的选定行下拉列表中选择。它不会再显示。

上述代码正在工作。但是,如果我刷新页面,它正在显示

如何解决此问题?

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

回答

1

据显示,因为您将不得不将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); 
} 
+0

如果我删除网格的第二行(页面索引1)中的LOP。它会影响网格所有页面的所有第二行。 – user7415073

+0

然后,您需要在会话中使用第三个条件,即GridView的页面或标识。用你自己的包含3个属性的类代替'KeyValuePair'。 – VDWWD

+0

你非常感谢你的回答 – user7415073

3

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

只是你的数据绑定调用之前...

//检查是否有任何网格值已经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")); 
     } 
    } 
}