2013-05-10 82 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