2012-05-14 20 views
0

OnCommandイベントに問題があります。このパラメタを引数として与えたり、すべてを表示したりするのは大丈夫ですが、CommandArgumentとして使用するとInvalidCastExceptionが発生します。私はこのImageButtonコントロールのOnCommandイベント

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; 
} 
+0

「e.CommandArgument」のデータ型がブール値ではないためです。だから、 '(bool)e.CommandArgument'でキャストすることはできません –

答えて

2

代わりにダブルクォート n CommandArgument

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

を持っているファイルの後ろに私のコードでは

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

:メソッドCommandArgumentの背後にあるコードは(String.Emptyを)私のaspxファイルで

"" 等しいでは、私は以下のコードを持っていますCommandArgumentはここで述べたような文字列型です - >http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.commandargument.aspx

関連する問題