1

抱歉,如果这是重复的,但我找不到以前发布的这个确切的排列。类似的解决方案实际上并没有解决我的问题。

所以我有一个多视图,其中一个视图中有一个 Gridview。网格的一行包含单独的 DropDownLists,网格 A 的更改更新了应该出现在网格 B 中的选择。并且更新不会发生。代码的要点如下:

<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:GridView ID="GridView1" runat="server"
         DataSourceID="ObjectDataSource1" AutoGenerateColumns="False" GridLines="None"
         OnRowDataBound="GridView1_RowDataBound" >
            <Columns>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlA"
                         OnSelectedIndexChanged="ddlA_SelectedIndexChanged"
                         runat="server"/>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlB" runat="server" />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </asp:View>
</asp:MultiView>

以及背后的代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                MyCustomObject myObject = (MyCustomObject)e.Row.DataItem;
                PopulateObjectRow(myObject, e.Row);
            }
        }
}

protected void ddlA_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList sender = (DropDownList)sender;
    GridViewRow row = (GridViewRow)sender.NamingContainer;
    MyCustomObject myObject = (MyCustomObject)e.Row.DataItem;
    myObject.FirstSelection = sender.SelectedItem.Value;
    PopulateObjectRow(row, sender.SelectedItem.Value);
}

private void PopulateObjectRow(MyCustomObject myObject, GridViewRow row)
{
    DropDownList ddlA = (DropDownList)row.FindControl("ddlA");
    ddlA.SelectedItem = GetSelectionItems();
    ddlA.DataBind();
    if(myObject.FirstSelection.IsValid())
    {
        SetSelection(ddlA, myObject.FirstSelection);
    }

    DropDownList ddlB = (DropDownList)row.FindControl("ddlB");
    ddlB.DataSource = GetDataValues(ddlA.SelectedItem.Value);
    ddlB.DataBind();
}

现在最糟糕的是,当我调试代码并遍历它时,ddlB.DataSource 中有预期的值。看起来 ddlB.DataBind() 事件实际上并没有做任何事情。GridView.OnRowDataBound() 是否为时已晚,无法实现此更改?作为一个额外的头痛,这一切都可以作为一个独立的 GridView 正常工作。只有当我将它包装在 MultiView 中时它才会停止运行!我的假设是我在这里做了一些乱序的事情,但如果我能找到它,我会被诅咒的......

4

0 回答 0