<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" CssClass="news" >
                <ItemStyle VerticalAlign="Top" />
                <ItemTemplate>
                <table>
                <tr style="vertical-align:top" >
                <td >
                    <asp:LinkButton id="btnSelect" Text= <%# DataBinder.Eval(Container.DataItem,"newsTitle") %> CommandName="edit" runat="server" Width="150px" Height="10px" Font-Overline="false"/>
                </td>
             
                <td width="20px">
                    <asp:Label ID="Label1" runat="server" Text= <%# DataBinder.Eval(Container.DataItem,"newsTime","{0:yyyy-MM-dd}") %> Width="60px" Height="10px"> </asp:Label>
                </td>
                </tr>
                </table> 
                </ItemTemplate>
            </asp:DataList> </div>function dzLinkColor()
    {
        document.getElementById("btnSelect").style.color="Red"; 
        document.getElementById("Label1").style.color="Red";     }
我在 <tr 后面加了onmousemove="dzLinkColor(),运行的时候出现js错误,btnSelect没有找到。

解决方案 »

  1.   

    服务器控件的ID属性从来不变。但是你可能误把控件的ID看做输出到客户端的html标签的id属性了吧?那个东西是服务器控件的ClientID,而不是ID。
      

  2.   

    看到  ID="DataList1"就在脚本中使用 DataList1?这是你选错了入门书或者人导致的。如果带你入门,最起码会给你讲你脚本中的那个是控件的ClientID这种浅显的知识。
      

  3.   

    服务器端控件的id如果在某个命名容器内是和其客户端标记id不一样的(但有规律)楼主对DataList这样的控件可能了解还不足,其实想想DataList里如果有多项,就会生成多个LinkButton1和Label1,楼主打算访问的是那一个呢?
      

  4.   

    如果你看到一本书、一个范例,它搞不懂ID与ClientID的区别,就千万别再学下去了。从csdn上,我看到竟然有大部分人入门时都没有建立这个知识,这真的是惊人的。现在的书籍都写了些什么?只会抄袭国外的编程册么?
      

  5.   

    恩,确实是没搞清楚id和ClientID,现在明白了,但是该如何获取我鼠标移动到的其中一个linkbutton的ID呢?
      

  6.   

    var LinkButton = '<%=LinkButton.ClientID %>'
    LinkButtonobj = document.getElementById(LinkButton)
      

  7.   

    SP1234说的不错,
    我来说一个简单的解决方案:
    <tr 后面加onmousemove="dzLinkColor(this);"函数改成:
    function dzLinkColor(obj) 
        { 
    var tdOfButton = obj.childNodes[0];// 找到td标签
    var button = tdOfButton.childNodes[0];// 找到td的第一个控件(可能要把<td>后面的空格和换行删除)
            button.style.color="Red"; 
            button.style.color="Red"; 
        }