ASP.Net GridView DropDownList 未回发更新后的数据

ASP.Net GridView DropDownList not posting back with updated data

我有一个包含 DropDownList 的 GridView。除页面回发期间外,它们按预期运行。当用户单击页面上的更新按钮时,我有一个循环遍历网格行、执行业务操作并保存数据的子程序。

问题是在回发期间,DropDownLists 的选定属性并不代表用户对选择所做的更改。所选项目在断点显示'Dirty = True'。

这是我用来参考的代码的一个子集:

<asp:GridView ID="materialGridView" runat="server"
                AutoGenerateColumns="false" >
                <Columns>
                    <asp:BoundField DataField="MaterialTypeName" HeaderText="Material Type" />
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Quantity
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:TextBox ID="quantityTextBox" runat="server" Width ="50" ></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Material
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="materialDropDownList" runat="server" Width="200">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Color
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:TextBox ID="colorTextBox" runat="server" Width="100" BackColor="BlanchedAlmond" ReadOnly="true" ></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            RBK
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:RadioButton id="rbkRadioButton" runat="server" Checked="true" />
                            <asp:TextBox ID="rbkPriceTextBox" runat="server" Width="50" ></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            Wimsatt
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:TextBox ID="wimsattPriceTextBox" runat="server" Width="50"></asp:TextBox>
                            <asp:RadioButton id="wimsattRadioButton" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>




        For Each row As GridViewRow In materialGridView.Rows
        With row

            materialDropDownList = DirectCast(.FindControl("materialDropDownList"), DropDownList)
            quantityTextBox = DirectCast(.FindControl("quantityTextBox"), TextBox)
            rbkPriceTextBox = DirectCast(.FindControl("rbkPriceTextBox"), TextBox)
            wimsattPriceTextBox = DirectCast(.FindControl("wimsattPriceTextBox"), TextBox)
            colorTextBox = DirectCast(.FindControl("colorTextBox"), TextBox)

            rbkRadioButton = (DirectCast(.FindControl("rbkRadioButton"), RadioButton))

            'compare current selecton in drop down and update if nessisary
            For Each materialTableRow As DataRow In materialTable.Rows
                'the item we are on is the item selected ANDALSO it is not yet assigned to the quote, so it's a new selection, update pricing.
                Dim materialTableRowMaterialID As String = bizClass.dbCStr(materialTableRow.Item("MaterialID"))

                If materialTableRowMaterialID = materialDropDownList.SelectedValue Then
                    If IsDBNull(materialTableRow.Item("QuoteID")) Then
                        rbkPriceTextBox.Text = bizClass.dbCStr(materialTableRow.Item("RBK"))
                        wimsattPriceTextBox.Text = bizClass.dbCStr(materialTableRow.Item("Wimsatt"))
                    End If
                End If
            Next materialTableRow

            If rbkRadioButton.Checked = True Then
                materialCostSumDecimal += bizClass.strToDec(quantityTextBox.Text) * bizClass.strToDec(rbkPriceTextBox.Text)
                chosenSupplierString = "RBK"
            Else
                materialCostSumDecimal += bizClass.strToDec(quantityTextBox.Text) * bizClass.strToDec(wimsattPriceTextBox.Text)
                chosenSupplierString = "Wimsatt"
            End If

        End With 'row

首先确保在读取下拉值之前没有在回发时绑定 gridview,因为数据绑定会导致所有控件丢失其回发值。

第二个 gridview 只能回发正在编辑的唯一行的值,因此您需要做的是将下拉列表(和所有其他控件)移动到 EditTemplate 并将行模式设置为编辑因此该值将被发回服务器。 但是,如果您希望能够更改所有行中的所有下拉菜单,您可能需要使用 Repeater 控件而不是 GridView。