简体   繁体   English

在Repeater链接按钮中将对象作为CommandArguement传递

[英]Pass object as CommandArguement in a Repeater Link Button

I have a Repeater with a list of Customers. 我有一个带有客户列表的中继器。 Against each customer there is a delete link button. 针对每个客户都有一个删除链接按钮。 As part of the linkbutton I want to pass the Customer object to the Command Arguement as follows (where Container.DataItem is the customer object): 作为链接按钮的一部分,我要将客户对象传递给命令参数,如下所示(其中Container.DataItem是客户对象):

<asp:LinkButton  ID="lnkDelete" 
   OnClientClick="return confirmDelete();"  
   OnClick="Customer_OnDelete"  
   CommandArgument="<%# Container.DataItem  %>"  
   CommandName="Delete" 
   runat="server"></asp:LinkButton>

When I do this: 当我这样做时:

    var button = (((LinkButton) sender));

    var customer=  button.CommandArgument;

button.CommandArguement is a string. button.CommandArguement是一个字符串。 I need all the object properties as we are using Nhibernate so everything needs to be set, the ID of the deleted record is not enough. 我在使用Nhibernate时需要所有对象属性,因此需要设置所有内容,删除记录的ID不够。 I have seen examples online regarding passing a comma seperated list of values into the command arguement but want to avoid doing that. 我在网上看到了一些示例,这些示例将逗号分隔的值列表传递到命令参数中,但要避免这样做。 Is this possible? 这可能吗?

Any ideas? 有任何想法吗? Thanks 谢谢

In my opinion the best way for this case is: 我认为这种情况的最佳方法是:

  • Get the ID from CommandArgument CommandArgument获取ID
  • Get the Customer by ID 通过ID获取客户
  • Delete the Customer Entity 删除客户实体

Use the Repeater event OnItemCommand . 使用Repeater事件OnItemCommand This event contains RepeaterCommandEventArgs . 此事件包含RepeaterCommandEventArgs You cant get the CommandArgument this way: 您无法通过以下方式获取CommandArgument

protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
   int customerID= Convert.ToInt32(e.CommandArgument.ToString());
}

At your asp:LinkButton tag use: 在您的asp:LinkButton标记处使用:

CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'

The issue you are running into here is that you repeater has to get translated into HTML. 您在这里遇到的问题是中继器必须转换为HTML。 Therefore you are constrained to the limits of what is allowed by the specification in an element attribute. 因此,您受限于元素属性中的规范所允许的限制。

On the server side CommandArgument will always be a string, so you cannot do what you want as you have it coded. 在服务器端, CommandArgument始终是一个字符串,因此您无法完成编码后的操作。

Now... there are several hacks you could implement to get around this, like the aforementioned CSV, or you could use binary serialization and Base64 encode the result. 现在...您可以实施一些技巧来解决此问题,例如上述CSV,或者可以使用二进制序列化Base64编码结果。 However, these are all terrible solutions! 但是,这些都是可怕的解决方案!

What you need is to re-think how you are doing this. 您需要重新考虑自己的操作方式。 I promise there is an easier way. 我保证有一个更简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM