gridview

时间:2017-07-05 14:41:23

标签: gridview data-binding webforms multiview

单击编辑按钮时,我在行rowdatabound事件中获取超出范围异常的参数,但仅适用于第9项和第10项,无论数据如何(任何页面都相同)。

此处发生错误。

DropDownList DropDownList_QuantityType = (DropDownList)e.Row.Cells[e.Row.RowIndex].FindControl("DropDownList_QuantityType");

这是rowdatabound事件。

protected void GridView_BidItemTypeTables_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            DropDownList DropDownList_QuantityType = (DropDownList)e.Row.Cells[e.Row.RowIndex].FindControl("DropDownList_QuantityType");
            DropDownList_QuantityType.SelectedValue = GridView_BidItemTypeTables.DataKeys[e.Row.RowIndex].Values[1].ToString();
            DropDownList DropDownList_FHWA = (DropDownList)e.Row.Cells[e.Row.RowIndex].FindControl("DropDownList_FHWA");
            DropDownList_FHWA.SelectedValue = GridView_BidItemTypeTables.DataKeys[e.Row.RowIndex].Values[2].ToString();
        }
    }

PageSize设置为10,所以我不明白为什么[8]和[9]项会超出范围异常。

我尝试将PageSize更改为8,并且所有8项都正常工作。当我将PageSize更改为11时,最后3项失败而不是最后2项。

这是aspx页面中的视图。

<asp:View ID="View_BidItemTypeTables" runat="server">
        <div>
            <div style="width: 100%; overflow-x: scroll; overflow-y: auto;">
                <asp:GridView ID="GridView_BidItemTypeTables" runat="server" AutoGenerateColumns="false" EmptyDataText="No Records" DataKeyNames="GUID, QuantityTypeGuid,FHWA" AllowPaging="true" PageSize="10"
                    CssClass="gridview" Width="50%" BorderColor="Black" BorderWidth="1px" BorderStyle="Solid" OnRowCancelingEdit="GridView_BidItemTypeTables_RowCancelingEdit" OnRowEditing="GridView_BidItemTypeTables_RowEditing" OnRowUpdating="GridView_BidItemTypeTables_RowUpdating" OnRowDataBound="GridView_BidItemTypeTables_RowDataBound" OnPageIndexChanging="GridView_BidItemTypeTables_PageIndexChanging" OnRowCommand="GridView_BidItemTypeTables_RowCommand">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="Button_EditTypeTables" runat="server" CommandName="Edit" Text="Edit" Visible="true" CommandArgument="<%# Container.DataItemIndex %>" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Button ID="Button_SaveTypeTables" runat="server" CommandName="Update" Text="Save" Visible="true" OnClick="Button_SaveTypeTables_Click" CausesValidation="true" ValidationGroup="ValidationFailed" /><asp:Button ID="Button_CancelTypeTables" runat="server" CommandName="Cancel" Text="Cancel" Visible="true" />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Code">
                            <ItemTemplate>
                                <asp:Label ID="Label_Code" Text='<%# Bind("Code") %>' runat="server" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox_Code" Text='<%# Bind("Code") %>' runat="server" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator_Code" runat="server" ErrorMessage="Code is Required." ControlToValidate="TextBox_Code" Display="Dynamic" ValidationGroup="ValidationFailed" CssClass="failureNotification" SetFocusOnError="true">*</asp:RequiredFieldValidator>
                                <asp:CustomValidator runat="server" ID="CustomValidator_CodeGrid" ControlToValidate="TextBox_Code" ClientIDMode="Static" ValidationGroup = "ValidationFailed" Display="Dynamic" Enabled="true" CssClass="failureNotification" OnServerValidate="TextBoxCodeValidatorBidItem_Grid" SetFocusOnError="true">*</asp:CustomValidator>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Description">
                            <ItemTemplate>
                                <asp:Label ID="Label_Description" Text='<%# Bind("Description") %>' runat="server" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox_Description" Text='<%# Bind("Description") %>' runat="server" />
                                <asp:CustomValidator runat="server" ID="CustomValidator_DescriptionGrid" ControlToValidate="TextBox_Code" ClientIDMode="Static" ValidationGroup = "ValidationFailed" Display="Dynamic" Enabled="true" CssClass="failureNotification" OnServerValidate="TextBoxDescriptionValidatorBidItem_Grid" SetFocusOnError="true">*</asp:CustomValidator>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="QuantityType">
                            <ItemTemplate><%# Eval("QuantityType.Code") %></ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList_QuantityType" AppendDataBoundItems="True" DataSourceID="ObjectDataSource_QuantityType" DataTextField="Code" DataValueField="Guid" runat="server">
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator_QuantityType" runat="server" ErrorMessage="QuantityType is Required." ControlToValidate="DropDownList_QuantityType" Display="Dynamic" ValidationGroup="ValidationFailed" CssClass="failureNotification" SetFocusOnError="true">*</asp:RequiredFieldValidator>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Unit">
                            <ItemTemplate>
                                <asp:Label ID="Label_Unit" Text='<%# Bind("Unit") %>' runat="server" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox_Unit" Text='<%# Bind("Unit") %>' runat="server" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator_Unit" runat="server" ErrorMessage="Unit is Required." ControlToValidate="TextBox_Unit" Display="Dynamic" ValidationGroup="ValidationFailed" CssClass="failureNotification">*</asp:RequiredFieldValidator>
                                <asp:CustomValidator runat="server" ID="CustomValidator_UnitGrid" ControlToValidate="TextBox_Code" ClientIDMode="Static" ValidationGroup = "ValidationFailed" Display="Dynamic" Enabled="true" CssClass="failureNotification" OnServerValidate="TextBoxUnitValidatorBidItem_Grid" SetFocusOnError="true">*</asp:CustomValidator>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="UnitPrice">
                            <ItemTemplate>
                                <asp:Label ID="Label_UnitPrice" Text='<%# Bind("UnitPrice") %>' runat="server" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox_UnitPrice" Text='<%# Bind("UnitPrice") %>' runat="server" MaxLength="9"/>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator_UnitPrice" runat="server" ErrorMessage="Unit Price is Required." ControlToValidate="TextBox_UnitPrice" Display="Dynamic" ValidationGroup="ValidationFailed" CssClass="failureNotification">*</asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator_UnitPrice" runat="server" CssClass="failureNotification" ErrorMessage="Unit Price is in incorrect format or exceeds (999999.99)." ControlToValidate="TextBox_UnitPrice" Display="Dynamic" ValidationGroup="ValidationFailed" ValidationExpression="^\d{0,6}(\.\d{1,2})?$" SetFocusOnError="true">*</asp:RegularExpressionValidator>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FHWA">
                            <ItemTemplate>
                                <asp:Label ID="Label_FHWA" Text='<%# Bind("FHWA") %>' runat="server" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList_FHWA" AppendDataBoundItems="True" runat="server">
                                    <asp:ListItem Value="" Selected="true">Select FHWA</asp:ListItem>
                                    <asp:ListItem Value="Y">Y</asp:ListItem>
                                    <asp:ListItem Value="N">N</asp:ListItem>
                                </asp:DropDownList><asp:RequiredFieldValidator ID="RequiredFieldValidator_FHWA" runat="server" ErrorMessage="FHWA is Required." ControlToValidate="DropDownList_FHWA" Display="Dynamic" ValidationGroup="ValidationFailed" CssClass="failureNotification">*</asp:RequiredFieldValidator>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="IsActive">
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox_IsActive" Checked='<%# Bind("IsActive") %>' runat="server" Enabled="False" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:CheckBox ID="CheckBox_IsActive" Checked='<%# Bind("IsActive") %>' runat="server" />
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <br />
            <br />
            <asp:Panel ID="Panel_AddBidItemTypeTables" runat="server" Visible="false">
                <table width="35%">
                    <tr>
                        <td>Code:</td>
                        <td>
                            <asp:TextBox ID="TextBox_CodeBidItemTypeTables" runat="server"></asp:TextBox>
                           <asp:RequiredFieldValidator ID="RequiredFieldValidator_CodeBidItemTypeTables" runat="server" ErrorMessage="Code is Required." ControlToValidate="TextBox_CodeBidItemTypeTables" Display="Dynamic" ValidationGroup="ValidationFailed" Enabled="false" CssClass="failureNotification"  SetFocusOnError="true">*</asp:RequiredFieldValidator>
                            <asp:CustomValidator runat="server" ID="CustomValidator_CodeBidItemTypeTables" ControlToValidate="TextBox_CodeBidItemTypeTables" ClientIDMode="Static" ValidationGroup="ValidationFailed" Display="Dynamic" Enabled="true" CssClass="failureNotification" OnServerValidate="TextBoxCodeValidatorBidItem" SetFocusOnError="true">*</asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Description:</td>
                        <td>
                            <asp:TextBox ID="TextBox_DescriptionBidItemTypeTables" runat="server" ClientIDMode="Static"></asp:TextBox>
                            <asp:CustomValidator runat="server" ID="CustomValidator_DescriptionBidItemTypeTables" ControlToValidate="TextBox_DescriptionBidItemTypeTables" ClientIDMode="Static" ValidationGroup="ValidationFailed" Display="Dynamic" Enabled="true" CssClass="failureNotification" OnServerValidate="TextBoxDescriptionValidatorBidItem" SetFocusOnError="true">*</asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Quantity Type:</td>
                        <td>
                            <asp:DropDownList ID="DropDownList_QuantityTypeBidItemTypeTables" AppendDataBoundItems="True" DataSourceID="ObjectDataSource_QuantityType" DataTextField="Code" DataValueField="Guid" runat="server">
                                <asp:ListItem Value="" Selected="true">Select QuantityType</asp:ListItem>
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator_QuantityTypeBidItemTypeTables" runat="server" Enabled="false" ErrorMessage="Quantity Type is Required." ControlToValidate="DropDownList_QuantityTypeBidItemTypeTables" Display="Dynamic" ValidationGroup="ValidationFailed" CssClass="failureNotification" SetFocusOnError="true">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Unit:</td>
                        <td>
                            <asp:TextBox ID="TextBox_UnitBidItemTypeTables" runat="server"></asp:TextBox>
                           <asp:RequiredFieldValidator ID="RequiredFieldValidator_UnitBidItemTypeTables" runat="server" ErrorMessage="Unit is Required." ControlToValidate="TextBox_UnitBidItemTypeTables" Display="Dynamic" ValidationGroup="ValidationFailed" Enabled="false" CssClass="failureNotification">*</asp:RequiredFieldValidator>
                            <asp:CustomValidator runat="server" ID="CustomValidator_UnitBidItemTypeTables" ControlToValidate="TextBox_UnitBidItemTypeTables" ClientIDMode="Static" ValidationGroup="ValidationFailed" Display="Dynamic" Enabled="true" CssClass="failureNotification" OnServerValidate="TextBoxUnitValidatorBidItem" SetFocusOnError="true">*</asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>UnitPrice:</td>
                        <td>
                            <asp:TextBox ID="TextBox_UnitPriceBidItemTypeTables" runat="server" MaxLength="9"></asp:TextBox>
                           <asp:RequiredFieldValidator ID="RequiredFieldValidator_UnitPriceBidItemTypeTables" runat="server" ErrorMessage="Unit Price is Required." ControlToValidate="TextBox_UnitPriceBidItemTypeTables" Display="Dynamic" ValidationGroup="ValidationFailed" Enabled="false" CssClass="failureNotification">*</asp:RequiredFieldValidator>
                            <asp:CustomValidator runat="server" ID="CustomValidator_UnitPriceBidItemTypeTables" ControlToValidate="TextBox_UnitPriceBidItemTypeTables" ClientIDMode="Static" ValidationGroup="ValidationFailed" Display="Dynamic" Enabled="true" CssClass="failureNotification" OnServerValidate="TextBoxUnitPriceValidatorBidItem" SetFocusOnError="true">*</asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>FHWA:</td>
                        <td>
                            <asp:DropDownList ID="DropDownList_FHWABidItemTypeTables" AppendDataBoundItems="True" runat="server">
                                <asp:ListItem Value="" Selected="true">Select FHWA</asp:ListItem>
                                <asp:ListItem Value="Y">Y</asp:ListItem>
                                <asp:ListItem Value="N">N</asp:ListItem>
                            </asp:DropDownList>
                           <asp:RequiredFieldValidator ID="RequiredFieldValidator_FHWABidItemTypeTables" runat="server" Enabled="false" ErrorMessage="FHWA is Required." ControlToValidate="DropDownList_FHWABidItemTypeTables" Display="Dynamic" ValidationGroup="ValidationFailed" CssClass="failureNotification">*</asp:RequiredFieldValidator>
                        </td>
                        <td>
                            <asp:Button ID="Button_AddBidItemTypeTables" runat="server" Text="Add" CausesValidation="true" ValidationGroup="ValidationFailed" OnClick="Button_AddBidItemTypeTables_Click" />
                        </td>
                    </tr>
                </table>
            </asp:Panel>
        </div>
    </asp:View>

以前有人遇到类似问题吗?

1 个答案:

答案 0 :(得分:0)

尝试使用e.Row.FindControl代替e.Row.Cells[e.Row.RowIndex].FindControl