ASP.Net GridView DropDownList没有回发更新的数据

时间:2015-06-07 19:48:52

标签: asp.net vb.net gridview

我有一个包含DropDownLists的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

1 个答案:

答案 0 :(得分:2)

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

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