突出显示数据列表中的选定行

时间:2012-03-06 04:15:32

标签: c# asp.net

我在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来选择项目。如何突出显示相应的行或仅突出显示单元格?

3 个答案:

答案 0 :(得分: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'");

}

答案 1 :(得分:0)

使用以下Method for 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));
     }
}

答案 2 :(得分: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;
}
相关问题