objectDataSource 与 ASP.NET gridview 自动添加参数绑定

发布于 2024-08-21 23:20:22 字数 5566 浏览 0 评论 0原文

我有一个绑定到对象数据源的 gridview 数据。一切都很顺利,直到我将 2 列从 asp:BoundField 更改为 asp:TemplateField。它们是 UPC 列和零件列。我想为每个字段显示一个链接按钮,将用户带到另一个页面。现在,asp.net 期望这两个字段作为更新存储过程的参数。这是为什么呢?如何让它不假设这些列需要传递给存储过程?

这是网格视图的代码:

          <asp:GridView ID="GridView1" runat="server" SkinID="MSDefault" AutoGenerateColumns="False" DataSourceID="CartDataSource" PageSize="25"
              Width="100%" DataKeyNames="CartId" EmptyDataText="The cart is empty" OnRowDataBound="GridView1_RowDataBound" onrowcommand="GridView1_RowCommand">
              <Columns>
                  <asp:CommandField ShowDeleteButton="True" ShowEditButton="True">
                      <ItemStyle Width="8%" />
                  </asp:CommandField>
                  <asp:BoundField DataField="CartId" HeaderText="CartId" InsertVisible="False" ReadOnly="True"
                      SortExpression="CartId" Visible="False" />
                  <asp:TemplateField HeaderText="UPC" SortExpression="UPC">
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ShowInfo" CommandArgument='<%# Bind("UPC") %>' Text='<%# Bind("UPC") %>'></asp:LinkButton>
                      </ItemTemplate>
                      <ItemStyle Width="9%" />
                  </asp:TemplateField>
                  <asp:BoundField DataField="Mfr" HeaderText="Mfr" ReadOnly="True" SortExpression="Mfr">
                      <ItemStyle Width="11%" />
                  </asp:BoundField>
                  <asp:TemplateField HeaderText="Part" SortExpression="Part">
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton2" runat="server" CommandName="ShowInfo" CommandArgument='<%# Bind("UPC") %>' Text='<%# Bind("Part") %>'></asp:LinkButton>
                      </ItemTemplate>
                      <ItemStyle Width="10%" />
                  </asp:TemplateField>
                  <asp:BoundField DataField="CustPart" HeaderText="Cust Part" ReadOnly="True" SortExpression="CustPart">
                      <ItemStyle Width="10%" />
                  </asp:BoundField>
                  <asp:BoundField DataField="PartDesc" HeaderText="Description" ReadOnly="True" SortExpression="PartDesc">
                      <ItemStyle Width="30%" Wrap="True" />
                  </asp:BoundField>
                  <asp:TemplateField HeaderText="Marked" SortExpression="Marked">
                      <EditItemTemplate>
                          <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Marked") %>' />
                      </EditItemTemplate>
                      <ItemTemplate>
                          <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Marked") %>' Enabled="false" />
                      </ItemTemplate>
                      <ItemStyle Width="2%" />
                  </asp:TemplateField>
                  <asp:BoundField DataField="Quantity" DataFormatString="{0:d}" HeaderText="Quantity"
                      SortExpression="Quantity">
                      <ItemStyle HorizontalAlign="Right" Width="4%" />
                  </asp:BoundField>
                  <asp:TemplateField HeaderText="Price" SortExpression="Price">
                      <ItemStyle HorizontalAlign="Right" Width="8%" />
                      <ItemTemplate>
                          <asp:Label ID="lblPrice" runat="server"></asp:Label>
                          <asp:LinkButton ID="LnkBtnCallLocalBranch" runat="server" Visible="false" PostBackUrl="~/UserProfile/LocalDistributors.aspx">call local branch</asp:LinkButton>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Ext Price" SortExpression="ExtPrice">
                      <ItemStyle HorizontalAlign="Right" Width="8%" />
                      <ItemTemplate>
                          <asp:Label ID="lblExtPrice" runat="server"></asp:Label>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>

这是 objectDataSource 代码

<asp:ObjectDataSource ID="CartDataSource" runat="server" OldValuesParameterFormatString="{0}" DeleteMethod="DeleteWithKey" SelectMethod="GetDataByCUserId"
    TypeName="PunchoutData.CartDataSetTableAdapters.CartTableAdapter" UpdateMethod="UpdateQuantityAndMarked" OnUpdated="CartDataSource_Updated" OnObjectCreated="CartDataSource_ObjectCreated">
    <DeleteParameters>
        <asp:Parameter Name="cartid" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="marked" Type="Boolean" />
        <asp:Parameter Name="quantity" Type="Int32" />
        <asp:Parameter Name="cartid" Type="Int32" />
    </UpdateParameters>
    <SelectParameters>
        <asp:SessionParameter Name="cuserid" SessionField="CUserId" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

这是正在生成的错误

ObjectDataSource 'CartDataSource' 找不到具有参数的非通用方法 'UpdateQuantityAndMarked':Marked、Quantity、CartId、UPC、Part 。

I have a gridview databound to an objectdatasource. Everything worked great until I changed 2 columns from asp:BoundField to asp:TemplateField. These are the UPC column and the Part column. I wanted to show a link button for each of these fields that take the user to another page. Now asp.net expects these two fields to be parameters on the update store procedure. Why is this? How do I get it to not assume these columns need to be passed to the store procedure?

Here is the code for the grid view:

          <asp:GridView ID="GridView1" runat="server" SkinID="MSDefault" AutoGenerateColumns="False" DataSourceID="CartDataSource" PageSize="25"
              Width="100%" DataKeyNames="CartId" EmptyDataText="The cart is empty" OnRowDataBound="GridView1_RowDataBound" onrowcommand="GridView1_RowCommand">
              <Columns>
                  <asp:CommandField ShowDeleteButton="True" ShowEditButton="True">
                      <ItemStyle Width="8%" />
                  </asp:CommandField>
                  <asp:BoundField DataField="CartId" HeaderText="CartId" InsertVisible="False" ReadOnly="True"
                      SortExpression="CartId" Visible="False" />
                  <asp:TemplateField HeaderText="UPC" SortExpression="UPC">
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ShowInfo" CommandArgument='<%# Bind("UPC") %>' Text='<%# Bind("UPC") %>'></asp:LinkButton>
                      </ItemTemplate>
                      <ItemStyle Width="9%" />
                  </asp:TemplateField>
                  <asp:BoundField DataField="Mfr" HeaderText="Mfr" ReadOnly="True" SortExpression="Mfr">
                      <ItemStyle Width="11%" />
                  </asp:BoundField>
                  <asp:TemplateField HeaderText="Part" SortExpression="Part">
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton2" runat="server" CommandName="ShowInfo" CommandArgument='<%# Bind("UPC") %>' Text='<%# Bind("Part") %>'></asp:LinkButton>
                      </ItemTemplate>
                      <ItemStyle Width="10%" />
                  </asp:TemplateField>
                  <asp:BoundField DataField="CustPart" HeaderText="Cust Part" ReadOnly="True" SortExpression="CustPart">
                      <ItemStyle Width="10%" />
                  </asp:BoundField>
                  <asp:BoundField DataField="PartDesc" HeaderText="Description" ReadOnly="True" SortExpression="PartDesc">
                      <ItemStyle Width="30%" Wrap="True" />
                  </asp:BoundField>
                  <asp:TemplateField HeaderText="Marked" SortExpression="Marked">
                      <EditItemTemplate>
                          <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Marked") %>' />
                      </EditItemTemplate>
                      <ItemTemplate>
                          <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Marked") %>' Enabled="false" />
                      </ItemTemplate>
                      <ItemStyle Width="2%" />
                  </asp:TemplateField>
                  <asp:BoundField DataField="Quantity" DataFormatString="{0:d}" HeaderText="Quantity"
                      SortExpression="Quantity">
                      <ItemStyle HorizontalAlign="Right" Width="4%" />
                  </asp:BoundField>
                  <asp:TemplateField HeaderText="Price" SortExpression="Price">
                      <ItemStyle HorizontalAlign="Right" Width="8%" />
                      <ItemTemplate>
                          <asp:Label ID="lblPrice" runat="server"></asp:Label>
                          <asp:LinkButton ID="LnkBtnCallLocalBranch" runat="server" Visible="false" PostBackUrl="~/UserProfile/LocalDistributors.aspx">call local branch</asp:LinkButton>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Ext Price" SortExpression="ExtPrice">
                      <ItemStyle HorizontalAlign="Right" Width="8%" />
                      <ItemTemplate>
                          <asp:Label ID="lblExtPrice" runat="server"></asp:Label>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>

Here is the objectDataSource code

<asp:ObjectDataSource ID="CartDataSource" runat="server" OldValuesParameterFormatString="{0}" DeleteMethod="DeleteWithKey" SelectMethod="GetDataByCUserId"
    TypeName="PunchoutData.CartDataSetTableAdapters.CartTableAdapter" UpdateMethod="UpdateQuantityAndMarked" OnUpdated="CartDataSource_Updated" OnObjectCreated="CartDataSource_ObjectCreated">
    <DeleteParameters>
        <asp:Parameter Name="cartid" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="marked" Type="Boolean" />
        <asp:Parameter Name="quantity" Type="Int32" />
        <asp:Parameter Name="cartid" Type="Int32" />
    </UpdateParameters>
    <SelectParameters>
        <asp:SessionParameter Name="cuserid" SessionField="CUserId" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

Here is the error that is being generated

ObjectDataSource 'CartDataSource' could not find a non-generic method 'UpdateQuantityAndMarked' that has parameters: Marked, Quantity, CartId, UPC, Part.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2024-08-28 23:20:22

问题是我正在使用 <%# Bind("UPC") %>我应该使用 <%# Eval("UPC") %>。 Bind 是双向的,Eval 是单向的(只读)

The issue was that I was using <%# Bind("UPC") %> and I should have been using <%# Eval("UPC") %>. Bind is two-way and Eval is one-way(read-only)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文