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

时间:2010-02-16 19:30:54

标签: asp.net data-binding objectdatasource

我有一个对象数据源的gridview数据绑定。一切都很顺利,直到我从asp:BoundField改为2列:asp:TemplateField。这些是UPC列和Part列。我想为每个字段显示一个链接按钮,将用户带到另一个页面。现在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。

1 个答案:

答案 0 :(得分:2)

问题是我使用&lt;%#Bind(“UPC”)%&gt;我应该使用&lt;%#Eval(“UPC”)%&gt;。绑定是双向的,Eval是单向的(只读)