Jacek
Jacek

Reputation: 12053

OnCommand event on ImageButton control

I have problem with OnCommand event. When I give this parametr as argument or display everything is all right, but if I used it as CommandArgument I get InvalidCastException. In code behind method CommandArgument is equal "" (string.Empty)

In my aspx file I have this code below:

<%# (bool)Eval("IsCandidateFavourite") %> //just display value
<asp:ImageButton id="ImageButton1" runat="server" 
    CommandArgument="<%# (bool)Eval("IsCandidateFavourite") %>"
    OnCommand="imBtnFavorite_Command"
    ImageUrl='<%# GetIsFavoriteImageUrl((bool)(Eval("IsCandidateFavourite")) ) %>'/>

In my code behind file I have this

public string GetIsCandidateFavoriteImageUrl(bool isNowFavorite)
{
    if (isNowFavorite)
    {
        return @"~/_images/icon_grid_fav.gif";
    }
    return @"~/_images/icon_grid_unfav.gif";
}

protected void imBtnFavorite_Command(object sender, CommandEventArgs e)
{
    bool isFavorite =(bool) e.CommandArgument;
}

Upvotes: 0

Views: 949

Answers (2)

Nalaka526
Nalaka526

Reputation: 11464

Try using Single Quotes (') instead Double Quotes (") in CommandArgument

CommandArgument='<%# (bool)Eval("IsCandidateFavourite") %>'

Upvotes: 2

rt2800
rt2800

Reputation: 3045

CommandArgument is of type string as mentioned here--> http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.commandargument.aspx

Upvotes: 0

Related Questions