c#
  • asp.net
  • 2015-04-12 40 views 0 likes 
    0

    我有這個在ASP.NET中VS 我在直放站不能打電話的ImageButton在按需功能

    <asp:Repeater ID="RepeaterImages" runat="server"> 
    <ItemTemplate> 
        <asp:ImageButton ID="Imagebutton2" runat="server" CssClass="choicebutton" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "answer")%>' CommandArgument='<%# DataBinder.Eval(Container.DataItem, "question")%>' CommandName='<%# DataBinder.Eval(Container.DataItem, "answer")%>' OnCommand="checkAnswer" /> 
    </ItemTemplate> 
    </asp:Repeater> 
    

    圖像按鈕,我想改變圖像按鈕的邊框顏色當它是假的

    public void checkAnswer(object sender, CommandEventArgs e) 
    { 
    
        string trueanswer = e.CommandArgument.ToString(); 
        string urlanswer = e.CommandName.ToString(); 
        if (urlanswer == q_image) 
        { 
        } 
        else 
        { 
         // Imagebutoon2 doesnt exist in current context why? 
         Imagebutton2.BorderColor = Color.Red; 
        } 
    } 
    

    回答

    0

    您可以使用ItemDataBound此任務:

    protected void RepeaterImages_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
        RepeaterItem item = e.Item; 
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem)) 
        { 
         Imagebutton Imagebutton2= (Imagebutton)item.FindControl("Imagebutton2"); 
    
         string trueanswer = Imagebutton.CommandArgument.ToString(); 
         string urlanswer = Imagebutton.CommandName.ToString(); 
    
         if (urlanswer == q_image) 
         { 
         } 
         else 
         { 
          // Imagebutoon2 doesnt exist in current context why? 
          Imagebutton2.BorderColor = Color.Red; 
    
         } 
    
        } 
    } 
    

    不要忘了它添加到中繼器服務器標籤:OnItemDataBound="RepeaterImages_ItemDataBound"

    UPDATE

    如果你想點擊的ImageButton當這種情況發生,那麼它就更簡單了。您可以使用sender訪問的ImageButton點擊:

    public void checkAnswer(object sender, CommandEventArgs e) 
    { 
         ImageButton btn = (ImageButton)sender; 
    
         string trueanswer = e.CommandArgument.ToString(); 
         string urlanswer = e.CommandName.ToString(); 
         if (urlanswer == q_image) 
         { 
         } 
         else 
         { 
          // Imagebutoon2 doesnt exist in current context why? 
          btn.BorderColor = Color.Red; 
    
         } 
        } 
    
    +0

    首先非常感謝幫助我與您寶貴的時間。但我要的是檢查每個圖像true或false(帶有參數和名稱),所以我爲每個圖像調用oncommand(當客戶端點擊時發生)。 – user3397383

    +0

    @ user3397383你的意思是當這些圖像被點擊時,你想改變它們的邊框?不是在第一次生成中繼器時? – renakre

    +0

    @ erkaner是啊,當點擊圖像檢查他們的價值(名稱)和chacnge邊界綠色或紅色 – user3397383

    相關問題