0

我想将 Gridview 中的第一列设置为零,但这样做会出现以下错误: System.Web.UI.WebControls.GridViewRow 在此上下文中不可访问,因为它是“朋友”。

这是我的网格:

 <asp:GridView ID="gvPurchaseOrderNum" runat="server" AutoGenerateColumns="False"
                        onrowdeleting="gvPurchaseOrderNum_RowDeleting" onrowediting="gvPurchaseOrderNum_RowEditing"
                        onrowupdating="gvPurchaseOrderNum_RowUpdating" onrowcommand="gvPurchaseOrderNum_RowCommand"
                        ShowFooter="True" Width="482px" 
                        onrowcancelingedit="gvPurchaseOrderNum_RowCancelingEdit">
                        <Columns>
                             <asp:TemplateField HeaderText="Purchase Order ID" Visible="true">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPonumberID" runat="server" Text='<%# Bind("ponumberID") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtPonumberIDInsert" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblPonumberID" runat="server" Text='<%# Bind("PonumberID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:TemplateField HeaderText="Purchase Order Number">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPonumber" runat="server" Text='<%# Bind("poNumber") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtPoNumberInsert" runat="server"></asp:TextBox>
                                   <%-- <asp:RequiredFieldValidator runat="server" ID="reqtxtPoNumberInsert" ControlToValidate="txtPoNumberInsert" ErrorMessage="Required Field" Display="Dynamic" />--%>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblPoNumber" runat="server" Text='<%# Bind("PoNumber") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="False">
                                <EditItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
                                        CommandName="Update" Text="Update"></asp:LinkButton>
                                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
                                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:LinkButton ID="btnInsert" runat="server" CommandName="insertXMLData">Insert</asp:LinkButton>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                        CommandName="Edit" Text="Edit"></asp:LinkButton>
                                </ItemTemplate>
                                <ItemStyle Width="120px" />
                            </asp:TemplateField>
                            <asp:CommandField ShowDeleteButton="True" />
                         </Columns>
                         <emptydatatemplate>
                          <b>Enter Purchase Order Number(s)</b> <br /> 
                             <asp:TextBox ID="txtStartpoNumID" runat="server"></asp:TextBox><br />
                             <asp:TextBox ID="txtStartpoNum" runat="server"></asp:TextBox><br />
                             <%-- <asp:RequiredFieldValidator runat="server" ID="reqtxtStartpoNum" ControlToValidate="txtStartpoNum" ErrorMessage="Required Field" Display="Dynamic" />--%>
                             <asp:LinkButton ID="lnkpro" runat="server" OnClick="writeStartpoNum"  Text="Add Part Description"></asp:LinkButton> 
                            <p>&nbsp;</p>
                        </emptydatatemplate> 
                    </asp:GridView> 

这是我为执行任务而实现的代码:

Protected Sub gvPurchaseOrderNum_RowDataBound(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' set the first column to zero
            e.Row.Cells(0).Text = 0
        End If


    End Sub

...关于我在这里做错了什么,我能得到一些帮助吗?我用谷歌搜索了错误,但无法找到解决问题的方法。

4

1 回答 1

0

使用FindControl()方法而不是Cells集合,如下所示:

Protected Sub gvPurchaseOrderNum_RowDataBound(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' set the first column to zero
        Dim theLabel As Label = e.Row.FindControl("lblPonumberID")
        If theLabel Is Not Nothing Then
            theLabel.Text = "0"
        End If
    End If
End Sub

由于您在 a 中可能有多个控件,因此使用该方法来定位您想要更改其属性的确切项目TemplateField会更容易,而不是依赖于通过索引获取正确的单元格。FindControl()Text

于 2013-08-27T04:36:31.527 回答