<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];
嗨大衛, 感謝您的輸入。我編輯了這個問題,以進一步解釋它是我在之後的DataSet中的其他非顯示字段。我可以通過使用我已經獲得的productID與他們聯繫。然而,有些東西告訴我這有一個更優雅的方法......在這種情況下,TemplateFields總是讓我困惑。 Anthony :-) – IrishChieftain 2009-11-03 04:21:05
謝謝大衛, 這就是答案!我能夠直接從DataSet獲取數據。 Anthony :-) – IrishChieftain 2009-11-03 06:11:35
爲了其他人的利益,我通過循環訪問DataSet的錶行來獲得所選行的索引,以獲得前面獲得的productID上的匹配。然後我可以訪問其餘的數據。 – IrishChieftain 2009-11-03 06:15:10