加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 删除一些ListItem后,在GridView中的DropDownList中维

发布时间:2020-12-16 09:35:31 所属栏目:asp.Net 来源:网络整理
导读:我有一个GridView,每行都有一个DropDownList. (DropDownList中的项目对于每个项目都是相同的.)我在GridView之外有一个DropDownList“ddlView”,用于过滤其他DropDownLists中的可用选项. ddlView的默认选择是没有过滤器. 当用户为ddlView选择新值时,如果它们
我有一个GridView,每行都有一个DropDownList. (DropDownList中的项目对于每个项目都是相同的.)我在GridView之外有一个DropDownList“ddlView”,用于过滤其他DropDownLists中的可用选项. ddlView的默认选择是没有过滤器.

当用户为ddlView选择新值时,如果它们不是应用过滤器后存在的值之一,则其他DropDownLists中的任何选定值都将消失.在这种情况下我想要发生的是先前选择的值仍然存在并被选中.

完成此任务的最佳方法是什么?

之前选择的值在回发期间可用,但在GridView上调用DataBind()后似乎会被清除,因此我无法在填充它们的方法中确定它们之前的值(RowDataBound事件).

到目前为止,我最好的想法是在回发期间手动将该信息存储到对象或集合中,并在数据绑定事件期间稍后引用它.

有没有更好的办法?

解决方法

我不认为有更好的方法来实现这一点,因为当GridView被绑定时,所有控件都被重新创建,从而删除了选择.

以下工作原理:(我将选择存储在postback上,以便在RowDataBound事件中再次检索)

标记

<asp:Button ID="button1" runat="server" Text="Post Back" OnClick="button1_Click" />
<br />
<asp:GridView ID="gridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="gridViewDropDownList" runat="server">
                    <asp:ListItem Value="1">Item 1</asp:ListItem>
                    <asp:ListItem Value="2">Item 2</asp:ListItem>
                    <asp:ListItem Value="3">Item 3</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

public class GridViewDropDownSelections
{
    public int RowIndex { get; set; }
    public int SelectedIndex { get; set; }
}

...

private List<GridViewDropDownSelections> selectedDropDownListItems = new List<GridViewDropDownSelections>();

protected override void OnLoad(EventArgs e)
{
    var selections = gridView1.Rows.Cast<GridViewRow>().Where(r => r.RowType == DataControlRowType.DataRow)
        .Select(r => new GridViewDropDownSelections() { RowIndex = r.RowIndex,SelectedIndex = ((DropDownList)r.FindControl("gridViewDropDownList")).SelectedIndex }).ToList();

    selectedDropDownListItems.AddRange(selections);

    gridView1.RowDataBound += new GridViewRowEventHandler(gridView1_RowDataBound);

    if (!IsPostBack)
    {
        BindDataGrid();
    }

    base.OnLoad(e);
}

protected void button1_Click(object sender,EventArgs e)
{
    BindDataGrid();
}

private void BindDataGrid()
{
    //Dummy data
    string[] data = new string[] { "Item 1","Item 2","Item 3" };
    gridView1.DataSource = data;
    gridView1.DataBind();
}

void gridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var selection = selectedDropDownListItems.FirstOrDefault(i => i.RowIndex == e.Row.RowIndex);
        if (selection != null)
        {
            try
            {
                DropDownList gridViewDropDownList = (DropDownList)e.Row.FindControl("gridViewDropDownList");
                gridViewDropDownList.SelectedIndex = selection.SelectedIndex;
            }
            catch (Exception)
            {
            }
        }
    }
}

希望能帮助到你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读