datalist里使用js 控制控件的显示隐藏 datalist<ItemTemplate> 面板里 有1个Button 和 1个label 想通过js 点击Button实现label的显示隐藏请教前辈

解决方案 »

  1.   

    获取Datalist
    var dt=document.all.<%=Datalist1.ClientID %>;
      

  2.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListDemo.aspx.cs" Inherits="DataListDemo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
            function showHide(lbl)
            {
                if(document.getElementById(lbl).style.display == "none")
                {
                    document.getElementById(lbl).style.display = "inline";
                }
                else
                {
                    document.getElementById(lbl).style.display = "none"
                }
                return false;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Button" />
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                </ItemTemplate>
            </asp:DataList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select * from products"></asp:SqlDataSource>
        </form>
    </body>
    </html>public partial class DataListDemo : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
                    }
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Button Button1 = e.Item.FindControl("Button1") as Button;
                Label Label1 = e.Item.FindControl("Label1") as Label;            Button1.Attributes.Add("onclick", "return showHide('" + Label1.ClientID + "')");
            }
        }
    }
      

  3.   

    使用DataList的ItemDataBound事件循环的为每一个Button添加事件
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            (e.Item.FindControl("Button1") as Button).Attributes.Add("onclick", "show('" + 当前的行的Label-- > id + "')");
        }
      

  4.   

    谢谢 各位的指教 amandag 1共50分 给您40分 其他2人1人5分 好么 谢谢
      

  5.   

    祝福 http://www.csdn.net/Images/logo_csdn.gif 越办越好