c#-从Gridview中的下拉列表中永久删除项目
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-从Gridview中的下拉列表中永久删除项目,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2825字,纯文字阅读大概需要5分钟。
内容图文
![c#-从Gridview中的下拉列表中永久删除项目](/upload/InfoBanner/zyjiaocheng/685/f105bce1708441bbb1c2c66dbd8f97c3.jpg)
我在Gridview中有dropdownList,
<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,则应将其从网格的选定行下拉列表中删除.它不会再显示.
上面的代码正在工作.但是,如果我刷新页面,它将显示
如何解决呢?
是否需要在其他任何事件中创建商品?
解决方法:
之所以显示,是因为您必须将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);
}
内容总结
以上是互联网集市为您收集整理的c#-从Gridview中的下拉列表中永久删除项目全部内容,希望文章能够帮你解决c#-从Gridview中的下拉列表中永久删除项目所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。