asp.net
  • gridview
  • templatefield
  • rowcommand
  • 2009-11-03 45 views 2 likes 
    2
    <asp:TemplateField>  
        <ItemTemplate> 
         <table width="540" cellpadding="5"> 
         <tr> 
          <td align="left" style="width:60%;"> 
           <img src='PurchaseHandler.ashx?ProductID=<%# Eval("ProductID")%>' 
            alt="<%# Eval("ProductName") %>" /> 
          </td> 
          <td align="left"> 
           <h3 style="text-align:left;"> 
            <asp:Label ID="nameLabel" runat="server" 
             Text='<%# Eval("ProductName") %>' /> 
           </h3> 
           <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("Price") %>' /> 
           <br /> 
           <asp:LinkButton ID="cartLink" runat="server" Text="<b>Add to Cart</b>" 
            CommandName="Add" CommandArgument='<%# Eval("ProductID") %>' /> 
          </td> 
         </tr> 
         </table> 
        </ItemTemplate> 
    </asp:TemplateField> 
    

    我正在使用購物車業務對象,其中包含的字段未用於在GridView中顯示。我在RowCommand事件處理程序中接下來要做的是從選定行中檢索其餘的數據字段。這使我從所選行的正確的產品ID:GridView模板 - 如何從選定行抓取數據

    if (e.CommandName == "Add") 
    { 
        int productID = 0; 
        int.TryParse(e.CommandArgument as string, out productID); 
    
        // Big blank! 
    } 
    

    我怎麼能搶的數據字段的其餘部分從選定的行來填充我的車?作爲解釋,我可以使用productID來挖掘從會話狀態拉出的DataSet,並以這種方式獲取數據。然而,我試圖確定的是,如果在這種情況下可以使用類似於此的語法嗎?要做到這一點

    DataRow[] rows = ds.Tables[0].Select("ProductID=" + 
            gridProducts.SelectedDataKey.Values["ProductID"].ToString()); 
    DataRow row = rows[0]; 
    

    回答

    5

    的一種方法是使用命令參數存儲行指數(也許它設置在上排創建的事件)。

    然後,您可以使用如下代碼來訪問你的行(代碼MSDN剪斷):

    // Convert the row index stored in the CommandArgument 
        // property to an Integer. 
        int index = Convert.ToInt32(e.CommandArgument); 
    
        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection. Use the 
        // CommandSource property to access the GridView control. 
        GridView customersGridView = (GridView)e.CommandSource; 
        GridViewRow row = customersGridView.Rows[index]; 
    

    如果你需要保持你的命令參數爲產品ID,那麼你可以用下面的訪問代碼該行:

    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
    

    然後,您可以使用GridViewRow的的FindControl()方法來選擇您需要的控件。

    Label priceLabel = row.FindControl("priceLabel") as Label; 
    

    編輯後從OP

    所以,如果我是正確的,你要訪問你行的DataItem的意見嗎?

    我不認爲這是可能的一個RowCommand事件處理程序中 - 我確實有點挖其他論壇的,顯然這個屬性時DataBind只是不爲空 - MSDN有this說:

    DataItem屬性僅在GridView控件的RowDataBound事件期間和之後可用。

    看起來像你的方法或類似的東西是鏈接回在RowCommand原始對象的唯一途徑(希望有人證明我是錯的)

    +0

    嗨大衛, 感謝您的輸入。我編輯了這個問題,以進一步解釋它是我在之後的DataSet中的其他非顯示字段。我可以通過使用我已經獲得的productID與他們聯繫。然而,有些東西告訴我這有一個更優雅的方法......在這種情況下,TemplateFields總是讓我困惑。 Anthony :-) – IrishChieftain 2009-11-03 04:21:05

    +0

    謝謝大衛, 這就是答案!我能夠直接從DataSet獲取數據。 Anthony :-) – IrishChieftain 2009-11-03 06:11:35

    +0

    爲了其他人的利益,我通過循環訪問DataSet的錶行來獲得所選行的索引,以獲得前面獲得的productID上的匹配。然後我可以訪問其餘的數據。 – IrishChieftain 2009-11-03 06:15:10

    相關問題