簡體   English   中英

將值從GridView傳遞到ASP.NET中文件后面的代碼

[英]Passing values from GridView to code behind file in ASP.NET

我有一個GridView綁定數據庫中的值。 這是代碼:

<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:BoundField  DataField="ProductName" HeaderText="Title" />
                <asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
                </asp:ImageField>
                <asp:BoundField DataField="ProductDescription" HeaderText="Description" />
                <asp:BoundField DataField="ProductCost" HeaderText="Cost" />
             <asp:TemplateField HeaderText="Quantity">
                 <ItemTemplate>
                     <asp:TextBox runat="server" TextMode="Number" ID="txtQuantity"></asp:TextBox>
                 </ItemTemplate>
             </asp:TemplateField>
                <asp:ButtonField Text="Button" CommandName="AddToCart" />
            </Columns>
        </asp:GridView>

我想將包含txtQuantity和ProductID值的命令參數傳遞給CommandName“ AddToCart”后面的代碼。 我怎樣才能做到這一點?

在GridView中綁定ProductID和Quantity的值。 在GridView中,如下所示:

<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField  DataField="ProductName" HeaderText="Title" />
            <asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
            </asp:ImageField>
            <asp:BoundField DataField="ProductDescription" HeaderText="Description" />
            <asp:BoundField DataField="ProductCost" HeaderText="Cost" />
         <asp:TemplateField HeaderText="Quantity">
             <ItemTemplate>
                 <asp:TextBox runat="server" TextMode="Number" ID="txtQuantity" Text="<%# Bind("Quantity")%>"></asp:TextBox>
             </ItemTemplate>
         </asp:TemplateField>
            <asp:Button Text="Button" CommandName="AddToCart" CommandArgument="<%# Eval("Quantity") + "," + Eval("ProductID")%>"  />
        </Columns>
    </asp:GridView>

在GridView RowCommand事件中,提供以下代碼:

  if(e.CommandName == "AddToCart")
  {
      string[] args = e.CommandArgument.ToString().Split(",");
      Decimal Quantity = Convert.ToDecimal(args[0]);
      int ProductID = Convert.ToInt32(args[1]);          
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM