Dan Wier
Dan Wier

Reputation: 344

ASP.Net GridView DropDownList not posting back with updated data

I have a GridView that contains DropDownLists. They function as expected except during page postback. When the user clicks the update button on the page, I have a sub that loops through the grid rows, performs business ops and saves the data.

The problem is that during postback, the DropDownLists' selected properties do not represent that changes to selections made by the user. The selected item shows 'Dirty = True' in the break point.

Here is a subset of the code I use for reference:

<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

Upvotes: 0

Views: 415

Answers (1)

coster128
coster128

Reputation: 322

First of all make sure you are not binding the gridview on postback before reading dropdown values, because databind causes all controls to lose their postback value.

Second gridview is only able to postback the value of the only row that is being edited, so what you need to do is to move the dropdown (and all other controls) to the EditTemplate and set the row mode to Edit so the value will get posted back to server. But if you want to be able to change all the dropdowns in all rows you might need to use Repeater control instead of GridView.

Upvotes: 2

Related Questions