c#
  • asp.net
  • 2012-03-06 64 views 1 likes 
    1

    我在ym網頁上有一個DataList,用戶可以從DataList行中選擇某個選項。突出顯示數據列表中的選定行

    爲此,我使用DataList的ItemCommand。實際上,我想在用戶點擊行中的項目時突出顯示選定的行。

    <ItemTemplate> 
         <tr> 
         <td style="text-align:center"><asp:LinkButton ID="Item" Text='<%#Eval("Item")%>' CommandName="select" runat="server" /> <br /></td> 
         <td style="text-align:center"><asp:Label ID="lbQuery" Text='<%#Eval("Query")%>' runat="server" /><br /> </td> 
         </tr> 
         </ItemTemplate> 
    

    如上所示,用戶可以點擊LinkBut​​ton來選擇一個項目。我如何突出顯示相應的行或僅顯示單元格?

    回答

    0

    使用以下方法Datalist中突出顯示選定的行:

    protected void DataList1_ItemDataBound(object sender, 
              DataListItemEventArgs e) 
    { 
        if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem) 
        { 
         //Add eventhandlers for highlighting 
         //a DataListItem when the mouse hovers over it. 
         e.Item.Attributes.Add("onmouseover", 
           "this.oldClass = this.className;" + 
           " this.className = 'EntryLineHover'"); 
         e.Item.Attributes.Add("onmouseout", 
           "this.className = this.oldClass;"); 
         //Add eventhandler for simulating 
         //a click on the 'SelectButton' 
         e.Item.Attributes.Add("onclick", 
           this.Page.ClientScript.GetPostBackEventReference(
           e.Item.Controls[1], string.Empty)); 
        } 
    } 
    
    0

    在你的RowDataBound事件中添加像這樣。

    // Get the linklabel object and check for non nullity 
    LinkLabel lblItem = e.Item.FindControl("Item") as LinkLabel 
    
    if(lblitem !=null) 
    { 
    // add properties to it 
    lblItem.Attributes.Add("onclick", "this.style.background='#eeff00'"); 
    
    } 
    
    0

    上項目命令

    string[] str = e.CommandArgument.ToString().Split(';'); 
    int index = Convert.ToInt32(str[2]); // ur item selected index 
    DataListItemCollection xx = DataList1.Items; 
    int count = 0; 
    foreach (DataListItem x in xx) 
    { 
        if (count == index) 
        { 
         (x.FindControl("Item") as LinkButton).BorderColor = System.Drawing.Color.Red; 
         (x.FindControl("Item") as LinkButton).BorderWidth = 1; 
        } 
        else 
        { 
         (x.FindControl("Item") as LinkButton).BorderColor = System.Drawing.Color.White; 
         (x.FindControl("Item") as LinkButton).BorderWidth = 0; 
    
        } 
    count = count + 1; 
    } 
    
    相關問題