Iri*_*ain 2 asp.net gridview templatefield rowcommand
<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>
Run Code Online (Sandbox Code Playgroud)
我正在使用购物车业务对象,其中包含未在GridView中显示的字段.我在RowCommand事件处理程序中尝试接下来要做的是从所选行中检索其余的数据字段.这为我提供了所选行中正确的产品ID:
if (e.CommandName == "Add")
{
int productID = 0;
int.TryParse(e.CommandArgument as string, out productID);
// Big blank!
}
Run Code Online (Sandbox Code Playgroud)
如何从所选行中获取其余数据字段以填充我的购物车?通过解释,我可以使用productID挖掘从Session状态拉出的DataSet,并以这种方式获取数据.但是,我想要确定的是,是否存在类似于此的语法,可以在这种情况下使用?
DataRow[] rows = ds.Tables[0].Select("ProductID=" +
gridProducts.SelectedDataKey.Values["ProductID"].ToString());
DataRow row = rows[0];
Run Code Online (Sandbox Code Playgroud)
一种方法是使用命令参数来存储行索引(可能将其设置在on row created事件中).
然后,您可以使用以下代码访问您的行(从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];
Run Code Online (Sandbox Code Playgroud)
如果您需要将命令参数保留为产品ID,则可以使用以下代码访问该行:
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用它的FindControl()方法GridViewRow来选择所需的控件.
Label priceLabel = row.FindControl("priceLabel") as Label;
Run Code Online (Sandbox Code Playgroud)
从OP的评论后编辑
所以,如果我是正确的,你想要访问你的行的DataItem?
我不认为在RowCommand事件处理程序中这是可能的 - 我在其他论坛上做了一点挖掘,显然这个属性在期间不会为空DataBind--MSDN有这样的说法:
DataItem属性仅在GridView控件的RowDataBound事件期间和之后可用.
看起来你的方法或类似的东西是链接回RowCommand中的原始对象的唯一方法(希望有人证明我错了)
| 归档时间: |
|
| 查看次数: |
5773 次 |
| 最近记录: |