代码改变世界

datalist 嵌套 datalist 及 内部按钮事件触发

2010-01-23 19:53  三皮开发时  阅读(609)  评论(0编辑  收藏  举报

前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="TNT.ETourBusinessPlatform.Web.bar.test"
    MasterPageFile="~/MasterPage.Master" %>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <style type="text/css">
        .s1
        {
            width: 100px;
            float: right;
        }
    </style>

    <script src="../js/jquery-min-lastest.js" type="text/javascript"></script>

    <asp:DataList ID="DataList1" runat="server"
        OnItemDataBound="DataList1_ItemDataBound"
        onitemcreated="DataList1_ItemCreated" onitemcommand="DataList1_ItemCommand">
        <ItemTemplate>
            <div style="width: 100px; height: 300px; float: left; border: solid 1px green">
                <%#Eval("HotelName") %><asp:Button ID="Button2" runat="server" Text="Button" CommandArgument='<%#Eval("HotelCode") %>'  CommandName="dt1" />
            </div>
            <div style="width: 100px; float: left; border: solid 1px red">
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("HotelCode") %>' ></asp:Label></div>
            <asp:DataList ID="DataList2" runat="server" CssClass="test1" onitemcommand="DataList2_ItemCommand"
               >
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("RoomName") %>'></asp:Label>
                    <asp:Button ID="Button1" runat="server"  CommandName="dt2" Text='<%#Eval("HotelCode") %>'  CommandArgument='<%#Eval("RoomCode") %>'/>
                </ItemTemplate>
            </asp:DataList>
        </ItemTemplate>
    </asp:DataList>
</asp:Content>
================================================================

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace TNT.ETourBusinessPlatform.Web.bar
{
   
    public partial class test : System.Web.UI.Page
    {

        public string str = ConfigurationManager.ConnectionStrings["DefaultSqlServer"].ConnectionString;
   
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();

            }
        }
        public void Bind()
        {
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand("select * from dbo.Hotel",con);
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            con.Close();
            DataSet ds = new DataSet();
            adapt.Fill(ds);

            DataList1.DataSource = ds.Tables[0];
            DataList1.DataBind();
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
           
           
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataList dl = (DataList)e.Item.FindControl("DataList2");
                Label lblTypeID = (Label)e.Item.FindControl("Label1");
             
                int typeID = Convert.ToInt32(lblTypeID.Text.ToString());
                string commandText = "select * from dbo.RoomType";
                commandText = commandText + " Where HotelCode='" + typeID+"'";


                SqlConnection con2 = new SqlConnection(str);
                con2.Open();
                //SqlCommand com2 = new SqlCommand(, con2);

                SqlDataAdapter adapt = new SqlDataAdapter(commandText,con2);
                DataSet ds = new DataSet();
                adapt.Fill(ds,"RoomType");
                con2.Close();

                dl.DataSource = ds.Tables["RoomType"];
                dl.DataBind();
            }

         
        }

        protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
        {
           
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "dt1")
            {
                Response.Write(e.CommandArgument.ToString());
            }
        }

        protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "dt2")
            {
                Response.Write(e.CommandArgument.ToString());
            }
            //if (e.CommandName == "dt1")
            //{
            //    Response.Write(e.CommandArgument.ToString());
            //}

        }


    }

}