我在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一次,则应从网格的选定行下拉列表中删除它。它不会再次出现。
以上代码正常运行。但是如果我刷新页面,它就会显示
如何解决这个问题?
是否需要在任何其他事件中创建项目?
答案 0 :(得分:3)
在回发后(刷新页面时),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"));
}
}
}
答案 1 :(得分: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);
}