c#
  • asp.net
  • linkbutton
  • 2011-12-16 30 views 0 likes 
    0

    我想顯示一週中的天數列表(動態,有時候全天,有時只有2-3) 然後用戶將點擊一天並刷新同一頁面。 以上功能是通過下面的代碼實現的。Repeater控件中的鏈接按鈕,如何更改點擊項目的背景/前景色?

    <asp:Repeater ID="DayList" Runat="server"> 
    <ItemTemplate> 
        <asp:LinkButton ID="lbDayList" Runat="server" CommandName='<%# DataBinder.Eval (Container.DataItem, "wkdayVal")%>' 
        OnCommand="lbDayList_click" 
        DataBinder.Eval(Container.DataItem, "wkday")%> 
        </asp:LinkButton> 
        </ItemTemplate>                    
    </asp:Repeater> 
    

    我想顯示不同顏色的點擊日!請幫助實現此功能!

    回答

    0

    您是否考慮過創建styles.css頁面並使用您的LinkBut​​ton的CssClass=""屬性?

    在你styles.css的文件,你將不得不像下面這樣:

    .Visited 
    { 
        color: #fff; 
        background: inherit; 
        text-decoration: none; 
    } 
    

    然後你的鏈接按鈕的CSS屬性會使用像

    CssClass="visited" 
    

    東西,你會想在這樣做Repeater的PreRender部分將Repeater的OnPreRender屬性設置爲Repeater_OnPreRender。然後在你的代碼隱藏創建像這樣

    protected void Repeater_OnPreRender(object sender, EventArgs e) 
    { 
    //get the index of the selected item 
    
    //loop through your items colleciton until you find the item with the corresponding index 
    
    //find your link button 
    
    //set your link button's css attribute. 
    
    } 
    

    你會在PreRender階段執行此原因的功能是因爲你的轉發器已加載到其中的數據並傳回給Web瀏覽器的HTML還沒有尚未創建。

    希望這會有所幫助。 GS

    +0

    更好地創建一個類(例如「MyLink」),然後樣式MyLink.visited等。然後,只需設置CssClass =「MyLink」。讓HTML和CSS完成工作,而不是C#代碼:) – HardCode 2011-12-16 17:02:54

    相關問題