2012-03-08 13 views
2

我正在C#/ ASP 4.0項目中嘗試製作購物車應用程序。ASP-獲取GridRow值/ OnClick方法

我的產品頁面上有一個顯示所有項目的GridView,我希望用戶能夠在此GridView中單擊一個「添加到購物車」按鈕字段,很明顯,它將向他們的項目添加一個項目大車。

我有問題,但實際上爲gridview設置了一個OnClick事件?這在「屬性」的「事件」菜單中似乎不可用。此外,我似乎無法弄清楚如何獲得特定的行。我有方法,這是否......

int productID = Convert.ToInt32(GridView1.Rows[n].Cells[0].Text); 
AddToCart(productID); 

但我不知道如何找出N,不然怎麼有這樣的方法,當他們單擊ButtonField字段在GridView中被調用。

回答

1

你可以這樣做:

首先,模板字段添加到GridView:

<asp:TemplateField HeaderText="Add to Cart"> 
    <ItemTemplate> 
     <asp:Button id="bthAddToCart" 
       CommandArgument'<%#Eval("ProductID")%>' 
       OnClick="bthAddToCart_Click" 
       Text="Add to Cart" 
       runat="server"/> 
    </ItemTemplate> 
</asp:TemplateField> 

現在,添加處理程序按鈕的Click事件:

protected void bthAddToCart_Click(object sender, EventArgs e) 
{ 

    Button button = (Button)sender; 
    int productID = Convert.ToInt32(button.CommandArgument); 
    AddToCart(productID); 
} 
+0

到底什麼是模板列?在DataGrid上的 – sab669 2012-03-08 21:08:16

+0

@ user1189566這個元素被稱爲TemplateColumn,在GridViews上相當於一個模板字段。有關詳細信息,請參閱此處:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.templatecolumn.aspx – Icarus 2012-03-08 21:08:53

0

使用網格視圖的OnRowCommand事件。更多細節:here

1

您可以使用模板字段是這樣的:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"> 
<Columns> 
    <asp:TemplateField HeaderText="Header Text Here"> 
     <ItemTemplate> 
      CONTROL TO SHOW COLUMN DATA 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Header Text Here"> 
     <ItemTemplate> 
      CONTROL TO SHOW COLUMN DATA 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Header Text Here"> 
     <ItemTemplate> 
      CONTROL TO SHOW COLUMN DATA 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Header Text Here"> 
     <ItemTemplate> 
      CONTROL TO SHOW COLUMN DATA 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderStyle-Width="30px"> 
     <ItemTemplate> 
      <asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="AddToCart" 
       CommandArgument='<%# Eval("ProductID") %>' /> 
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 
<EmptyDataTemplate> 
    No Data Found. 
</EmptyDataTemplate> 
</asp:GridView> 

然後在後面的代碼:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "AddToCart") 
    { 
     int ProductID = Convert.ToInt32(e.CommandArgument); 
     AddToCart(ProductID); 
    } 
} 

希望這有助於!祝你好運!

0

您必須爲Gridview使用OnRowCommand事件。使用下面的代碼:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"> 
<Columns> 
    <asp:TemplateField HeaderText="Header Text Here"> 
     <ItemTemplate> 
      CONTROL TO SHOW COLUMN DATA 
     </ItemTemplate> 
    </asp:TemplateField> 

<asp:TemplateField HeaderStyle-Width="30px"> 
     <ItemTemplate> 
      <asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="AddToCart" 
       CommandArgument='<%# Eval("ProductID") %>' /> 
     </ItemTemplate> 
    </asp:TemplateField> 
</asp:GridView> 

在C#代碼中使用下面的代碼:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Add To Cart") 
    { 
     int ProductID = Convert.ToInt32(e.CommandArgument); 
     AddToCart(ProductID); 
    } 
}