【问题标题】:GridView edit button cause exceptionGridView 编辑按钮导致异常
【发布时间】:2011-06-24 02:43:36
【问题描述】:

我有一个deleteedit 按钮的命令模板。

当我单击edit 按钮并为每个字段填写正确数据类型的数据时,这将正常工作。

但是,当我在字段中填写错误数据类型的数据时,网页浏览器会触发异常。我该如何纠正这个问题,你们背后的任何代码都可以建议,以及放在哪里?

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
                    CellPadding="4" DataKeyNames="ProductCode" DataSourceID="SqlProductmaster" 
                    ForeColor="#333333" GridLines="None">
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <Columns>
                        <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" 
                            ReadOnly="True" SortExpression="ProductCode" />
                        <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                            SortExpression="ProductName" />
                        <asp:BoundField DataField="Category" HeaderText="Category" 
                            SortExpression="Category" />
                        <asp:BoundField DataField="SellingPrice" HeaderText="SellingPrice" 
                            SortExpression="SellingPrice" />
                        <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                            SortExpression="Quantity" />
                        <asp:BoundField DataField="BrandName" HeaderText="BrandName" 
                            SortExpression="BrandName" />
                        <asp:BoundField DataField="ReOrderQty" HeaderText="ReOrderQty" 
                            SortExpression="ReOrderQty" />
                        <asp:BoundField DataField="ReOrderLevel" HeaderText="ReOrderLevel" 
                            SortExpression="ReOrderLevel" />
                        <asp:BoundField DataField="Ordered" HeaderText="Ordered" 
                            SortExpression="Ordered" />
                        <asp:BoundField DataField="Allocated" HeaderText="Allocated" 
                            SortExpression="Allocated" />
                        <asp:BoundField DataField="FreeQty" HeaderText="FreeQty" 
                            SortExpression="FreeQty" />
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:Button ID="Update" runat="server" CausesValidation="True" 
                                    CommandName="Update" Text="Update" />
                                &nbsp;<asp:Button ID="Cancel" runat="server" CausesValidation="False" 
                                    CommandName="Cancel" Text="Cancel" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Button ID="edit" runat="server" CausesValidation="False" 
                                    CommandName="Edit" Text="Edit" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:Button ID="Delete" runat="server" CausesValidation="False" 
                                    CommandName="Delete" Text="Delete" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#990000" Font-Bold="false" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="False" ForeColor="Navy" />
                    <HeaderStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView> 

【问题讨论】:

  • 异常名称是什么?也许你需要写一个事件。
  • @Marco 抱歉,Marco,现在异常消失了,但出现了其他问题。似乎更新按钮在我单击时没有触发任何内容。取消和删除工作正常。奇怪吗?
  • 我猜这更多是你的代码隐藏问题。

标签: c# asp.net gridview button edit


【解决方案1】:

您可以对该特定控件进行验证,并且永远不会生成异常。

【讨论】:

    【解决方案2】:

    您可以覆盖允许您编辑 BoundFields 的函数,而不是使用模板字段。为此,请尝试以下操作:

    <asp:GridView runat="server" ID="gvCurrentLocations" ... OnRowEditing="rowEditing" OnRowCancelingEdit="rowCancel" OnRowUpdating="rowUpdating">
        <Columns>
            <asp:BoundField DataField="locationname" HeaderText="Location Name" />
            <asp:TemplateField HeaderText="Action">
                <ItemTemplate>
                    <asp:CheckBox ID="cbToRemove" runat="server" CssClass="remove" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="boundpdas" HeaderText="Assigned To" ReadOnly="true" />
            <asp:CommandField ShowEditButton="true" UpdateText="Save"  />
        </Columns>
        <EmptyDataTemplate>
            There are currently no locations.
        </EmptyDataTemplate>
    </asp:GridView>
    

    注意第二个绑定字段是只读的,这将阻止它被编辑。第 4 列中的命令字段公开了一个“编辑”按钮,该按钮将绑定字段更改为文本框。

    在您的后端代码中,您可以通过访问 GridViewUpdateEventArgs 参数的“NewValues”字典来获取新值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多