2 kyxmly kyxmly 于 2013.07.07 19:15 提问

委托添加ImageButton事件,但没触发,为什么?

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace CinemaWebProject_WebUI
{
public partial class BuyTickets : System.Web.UI.Page
{
string seatNCol = null;
string seatStatus = null;
string[] seatInfoByID = new string[5];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), null, "<script type='text/javascript'>alert('请先登录');window.location='/foreground/Login.aspx';</script>");
        }
        if (!Page.IsPostBack)
        {
            LoadDDLControl();
        }
    }

    private void LoadDDLControl()
    {
        LoadDDLMovie();
        LoadDDLHallL(this.ddl_Movie.SelectedValue);
        LoadDDLDateL(this.ddl_Movie.SelectedValue,this.ddl_Hall.SelectedValue);
        LoadDDLScheduleL(this.ddl_Movie.SelectedValue, this.ddl_Hall.SelectedValue,this.ddl_Date.SelectedValue);
        LoadDDLTicketType();
        LoadDDLActivityL(this.ddl_TicketType.SelectedValue);
        ddl_Activity.Enabled = false;

        LoadSeatNCol();
        BeforeLoadAllSeat();
    }
    private void LoadDDLMovie()
    {
        string tablename = "MovieDDL";

        DataSet ds = Business.MovieBusiness.LoadDDLMovie(tablename);
        this.ddl_Movie.DataSource = ds.Tables[tablename].DefaultView;    //获取数据
        this.ddl_Movie.DataTextField = "name";       //MovieName
        this.ddl_Movie.DataValueField = "MovieID";
        this.ddl_Movie.DataBind();
        if (Request.QueryString["MovieID"] != null)
        {
            this.ddl_Movie.SelectedValue = Request.QueryString["MovieID"];
        }
    }
    private void LoadDDLHallL(string selectedvalue)
    {
        DataSet ds = Business.HallBusiness.LoadDDLHallL(selectedvalue);
        this.ddl_Hall.DataSource = ds.Tables["Hall"].DefaultView;
        this.ddl_Hall.DataTextField = "HallName";
        this.ddl_Hall.DataValueField = "HallID";
        this.ddl_Hall.DataBind();
    }
    private void LoadDDLDateL(string selectedvalue, string selectedvalue2)
    {
        DataSet ds = Business.ScheduleBusiness.LoadDDLDateL(selectedvalue,selectedvalue2);
        this.ddl_Date.DataSource = ds.Tables["date"].DefaultView;
        this.ddl_Date.DataTextField = "ShowDate";
        this.ddl_Date.DataValueField = "DateID";
        this.ddl_Date.DataBind();
    }
    private void LoadDDLScheduleL(string selectedvalue, string selectedvalue2, string selectedvalue3)
    {
        DataSet ds = Business.ScheduleBusiness.LoadDDLScheduleL(selectedvalue,selectedvalue2,selectedvalue3);
        this.ddl_Time.DataSource = ds.Tables["schedule"].DefaultView;
        this.ddl_Time.DataTextField = "TimeList";
        this.ddl_Time.DataValueField = "ScheduleID";
        this.ddl_Time.DataBind();
    }
    private void LoadDDLTicketType()
    {
        //DataSet ds = Business.TicketTypeBusiness.LoadDDLTicketType();
        //this.ddl_TicketType.DataSource = ds.Tables["TicketType"].DefaultView;    //获取数据
        //this.ddl_TicketType.DataTextField = "Type";
        //this.ddl_TicketType.DataValueField = "TypeID";
        //this.ddl_TicketType.DataBind();

        DataSet ds = Business.ActivityBusiness.LoadDDLType("ActivityType");
        this.ddl_TicketType.DataSource = ds.Tables["ActivityType"].DefaultView;    //获取数据
        this.ddl_TicketType.DataTextField = "ActivityTypeCN";
        this.ddl_TicketType.DataValueField = "ActivityTypeID";
        this.ddl_TicketType.DataBind();
    }
    private void LoadDDLActivityL(string selectedvalue)
    {
        DataSet ds = Business.ActivityBusiness.LoadDDLActivityL(selectedvalue);
        this.ddl_Activity.DataSource = ds.Tables["Activity"].DefaultView;
        this.ddl_Activity.DataTextField = "ActivityTitle";
        this.ddl_Activity.DataValueField = "ActivityID";
        this.ddl_Activity.DataBind();
    }
    private void LoadSeatNCol()
    {
        //根据所选影厅,获取座位每行数量
        if (ddl_Hall.SelectedValue != "0")
        {
            seatNCol = Business.SeatBusiness.SearchSeatNColByHallID(Convert.ToInt32(ddl_Hall.SelectedValue));
        }
    }
    private void GetSeatStatus()
    {
        Entity.MHS mhs = new Entity.MHS();
        mhs.MovieID = Convert.ToInt32(ddl_Movie.SelectedValue);
        mhs.HallID = Convert.ToInt32(ddl_Hall.SelectedValue);
        mhs.ScheduleID = Convert.ToInt32(ddl_Time.SelectedValue);
        mhs.DateID = Convert.ToInt32(ddl_Date.SelectedValue);

        seatStatus = Business.SeatBusiness.SearchSeatNColFromMHS(mhs);
        if (seatStatus == "-1")
        {
            lbl_Nothing.Visible = true;
            lbl_Nothing.Text = "没有该场次";
        }
        else
        {
            lbl_Nothing.Visible = false;
        }
    }

    protected void ddl_Movie_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadDDLHallL(this.ddl_Movie.SelectedValue);
        this.LoadDDLDateL(this.ddl_Movie.SelectedValue, this.ddl_Hall.SelectedValue);
        this.LoadDDLScheduleL(this.ddl_Movie.SelectedValue, this.ddl_Hall.SelectedValue, this.ddl_Date.SelectedValue);

        LoadSeatNCol();
        BeforeLoadAllSeat();
    }
    protected void ddl_Hall_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadDDLDateL(this.ddl_Movie.SelectedValue, this.ddl_Hall.SelectedValue);
        this.LoadDDLScheduleL(this.ddl_Movie.SelectedValue, this.ddl_Hall.SelectedValue, this.ddl_Date.SelectedValue);

        LoadSeatNCol();
        BeforeLoadAllSeat();
    }
    protected void ddl_Date_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadDDLScheduleL(this.ddl_Movie.SelectedValue, this.ddl_Hall.SelectedValue, this.ddl_Date.SelectedValue);

        LoadSeatNCol();
        BeforeLoadAllSeat();
    }
    protected void ddl_Time_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadSeatNCol();
        BeforeLoadAllSeat();
    }
    protected void ddl_TicketType_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadDDLActivityL(this.ddl_TicketType.SelectedValue);
        if (ddl_TicketType.SelectedValue == "1")
        {
            ddl_Activity.Enabled = false;
        }
        else
        {
            ddl_Activity.Enabled = true;
        }

        LoadSeatNCol();
        BeforeLoadAllSeat();
    }

    protected void BeforeLoadAllSeat()
    {
        if (ddl_Movie.SelectedValue == "0" || ddl_Hall.SelectedValue == "0" ||
            ddl_Date.SelectedValue == "0" || ddl_Time.SelectedValue == "0" ||
            ddl_TicketType.SelectedValue == "0" || ddl_Activity.SelectedValue == "0")
        {
            lbl_Nothing.Visible = true;
            lbl_Nothing.Text = "没有该场次";
        }
        else
        {
            Entity.MHS mhs = new Entity.MHS();
            mhs.MovieID = Convert.ToInt32(ddl_Movie.SelectedValue);
            mhs.HallID = Convert.ToInt32(ddl_Hall.SelectedValue);
            mhs.ScheduleID = Convert.ToInt32(ddl_Time.SelectedValue);
            mhs.DateID = Convert.ToInt32(ddl_Date.SelectedValue);

            int r = Business.SeatBusiness.IsMHSExist(mhs);
            if (r > 0)
            {
                lbl_Nothing.Visible = false;
                LoadAllSeat();
            }
            else
            {
                lbl_Nothing.Visible = true;
                lbl_Nothing.Text = "没有该场次";
            }
        }
    }
    protected void LoadAllSeat()
    {
        GetSeatStatus();

        if (seatStatus != "-1")
        {
            lbl_Nothing.Visible = false;

            string[] temp = seatNCol.Split(',');
            int[] seatCol = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                seatCol[i] = Convert.ToInt32(temp[i]);
            }

            Table table = new Table();

            //总的行数
            int rowCnt;

            //当前行
            int rowCtr;

            //每行的单元格数
            int cellCtr;

            //当前单元格
            //int cellCnt;

            //获取总的行数和每行的单元格数
            rowCnt = seatCol.Length;

            //初始座位状态
            string initSeatStatus = "-1";
            //是否能被改变ID的标识——为:已售,用户无法改变座位状态 做铺垫
            string enableChanged = "-1";
            //能被改变为1,无法改变为0

            int flag = 0;

            for (rowCtr = 0; rowCtr < rowCnt; rowCtr++)
            {
                //创建新行并把它添加到表格中
                TableRow tRow = new TableRow();
                this.tbl_showSeat.Rows.Add(tRow);

                for (cellCtr = 0; cellCtr < seatCol[rowCtr]; cellCtr++)
                {
                    TableCell tCell = new TableCell();
                    tRow.Cells.Add(tCell);
                    string prodID = rowCtr + "_" + cellCtr;
                    tCell.Text = prodID;

                    //tCell.Controls.Add(new LiteralControl("Buy:"));

                    ImageButton imgbtn = new ImageButton();
                    if (seatStatus.Substring(flag, 1) == "0")
                    {
                        initSeatStatus = "0";
                        enableChanged = "1";

                        imgbtn.ImageUrl = "~/background/seat/img/seat白.png";//Server.MapPath("~/img/") + "shafa白.png";
                    }
                    else if (seatStatus.Substring(flag, 1) == "1")
                    {
                        initSeatStatus = "1";
                        enableChanged = "0";

                        imgbtn.ImageUrl = "~/background/seat/img/seat蓝.png";
                    }
                    else if (seatStatus.Substring(flag, 1) == "2")
                    {
                        initSeatStatus = "2";
                        enableChanged = "0";

                        imgbtn.ImageUrl = "~/background/seat/img/seat黑.png";
                    }

                    imgbtn.ID = "imgbtn_" + rowCtr + "_" + cellCtr + "_" + initSeatStatus+ "_" +enableChanged;

                    imgbtn.Width = 80;
                    imgbtn.Height = 70;

                    imgbtn.Click += new ImageClickEventHandler(this.imgbtn_SelectedEvent_Click);

                    tCell.Controls.Add(imgbtn);

                    flag++;
                }
            }
        }
        else
        {
            lbl_Nothing.Visible = true;
            lbl_Nothing.Text = "没有该场次";
        }
    }

    protected void imgbtn_SelectedEvent_Click(object sender, ImageClickEventArgs e)//ImageClickEventArgs  System.EventArgs
    {
        seatInfoByID = ((ImageButton)sender).ID.Split('_');
        string senderName = seatInfoByID[0];
        string rowCtr = seatInfoByID[1];
        string cellCtr = seatInfoByID[2];
        string initSeatStatus = seatInfoByID[3];
        string enableChanged = seatInfoByID[4];

        if (enableChanged == "1")
        {
            if (initSeatStatus == "0")
                initSeatStatus = "1";
            else if (initSeatStatus == "1")
                initSeatStatus = "0";
        }

        ((ImageButton)sender).ID = "imgbtn_" + rowCtr + "_" + cellCtr + "_" + initSeatStatus + "_" + enableChanged;

        Response.Write("<script>alert('执行了!')</script>");
        Response.Write("你单击了ImageButton1,坐标为:(" + e.X.ToString() + "," + e.Y.ToString() + ")");
    }

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        if (ddl_Movie.SelectedValue == "0" || ddl_Hall.SelectedValue == "0" ||
            ddl_Date.SelectedValue == "0" || ddl_Time.SelectedValue == "0" ||
            ddl_TicketType.SelectedValue == "0" || ddl_Activity.SelectedValue == "0")
        {
            Response.Write("<script>alert('请选择有效场次')</script>");
        }
        else
        {

        }
    }

    protected void btn_SearchPrice_Click(object sender, EventArgs e)
    {

    }
}

}

这是在做电影院选座的程序,不知道为什么,点ImageButton的时候,imgbtn_SelectedEvent_Click这个事件没有被触发。。。求指点。

Csdn user default icon
上传中...
上传图片
插入图片