2013-05-10 83 views
0

你好我正在创建一个购物应用程序,我有一个购物车,我有一些列是从代码后面添加的,有些列是模板字段中不存在的数据库但将来要使用。在gridview中编辑同一个单元

这是我的购物车

代码:

<asp:GridView ID="GridViewProduct" runat="server" CellPadding="5" CssClass="grid" 
    ForeColor="#333333" OnRowCreated="GridViewProduct_RowCreated" OnRowDataBound="GridViewProduct_RowDataBound" 
    ShowFooter="True" CellSpacing="1" 
    onrowupdating="GridViewProduct_RowUpdating"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
     <asp:ButtonField Text="Remove From Cart" CommandName="RemoveFromCart" /> 
     <asp:TemplateField HeaderText="Quantity"> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBoxQuantity" runat="server" Width="50px">1</asp:TextBox><br /> 
       <asp:LinkButton ID="LinkButtonEditQuantity" runat="server" CommandName="EditTextBox">edit</asp:LinkButton> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:LinkButton ID="LinkButtonUpdate" runat="server">Update</asp:LinkButton> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Total Price"> 
      <ItemTemplate> 
       <asp:Label ID="LabelTotalPrice" runat="server" Text="0"></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <EditRowStyle BackColor="#7C6F57" /> 
    <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" /> 
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#E3EAEB" /> 
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> 
    <SortedAscendingCellStyle BackColor="#F8FAFA" /> 
    <SortedAscendingHeaderStyle BackColor="#246B61" /> 
    <SortedDescendingCellStyle BackColor="#D4DFE1" /> 
    <SortedDescendingHeaderStyle BackColor="#15524A" /> 
</asp:GridView> 

enter image description here

我在模板领域的编辑链接按钮,并在editItem模板更新链接按钮和文本框上方以便在用户点击编辑链接按钮时编辑文本框。

我的问题是:我如何编辑同一列中的文本框而不是整行。

我应该使用哪种方法来编辑同一个单元格rowupdating,rowcommand并将值保存在session中,并将编辑后的值乘以数量和单位价格以显示在gridview的总价格行中。

请帮我落实落实这正确的方式,因为我坚持这许多天..请在你的GridView模板

回答

1

,看EditRowTemplate,你会看到,它包含了所有的编辑控件(很可能是文本框)删除文本框并添加标签,并将其绑定到您的数据源。这样的数据仍然显示,但不会被编辑(除了你想要编辑的控制)

试试吧,让我知道你是否需要更多的帮助。

+0

我已添加标签并将其文本属性设置为默认值1.现在,我必须在用户希望编辑购物车中的数量时编辑标签。我怎么能实现这一点,我应该使用哪种方法的gridview多元化到单位价格,并显示总价格在其他列中的总价值。我编辑我的帖子来显示我的购物车图片。 – 2013-05-10 18:21:54

+1

好的,你如何将数据绑定到代码隐藏的gridview?我应该从一开始就复制和粘贴你的代码,我刚刚做过,似乎你的文本框已经可编辑了,所以你不需要在EditItemTemplate上做任何事情(这意味着你可以改变它的值,但如果你的意思是别的,请解释一下)要根据数量的值计算总数,请在RowDataBound事件上执行此操作,例如http://weblogs.asp.net/stevewellens/archive/2008/10/31/summing-columns-in-a-gridview.aspx – Marcianin 2013-05-10 18:37:21

+0

我有三个数据绑定字段(ProductName,ProductUnitPrice,ProductAuthor)和两个未绑定到任何数据源的模板字段。我刚刚创建它们以使用户能够更新数量并显示总价格,而我的产品数据库中没有元素数量。所以,我需要做一个GridView列数据绑定,使其可编辑或我可以编辑标签。请回复.. – 2013-05-10 19:18:07