Question

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):

<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. 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. 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

Était-ce utile?

La solution

In my opinion the best way for this case is:

  • Get the ID from CommandArgument
  • Get the Customer by ID
  • Delete the Customer Entity

Use the Repeater event OnItemCommand. This event contains RepeaterCommandEventArgs. You cant get the CommandArgument this way:

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

At your asp:LinkButton tag use:

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

Autres conseils

The issue you are running into here is that you repeater has to get translated into 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.

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. 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top