如何找到点击的图像按钮? [英] how to find which imagebutton was clicked?

查看:62
本文介绍了如何找到点击的图像按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个动态显示公交车座位布局的功能。现在将所有这些动态创建的表格和单元格添加到标签中后,我想知道单击了哪个图像按钮。我还提到了单元格中每个图像按钮的正确ID。



I have created a function which displays a bus seat layout, dynamically. Now after adding all this dynamically created table and cells into a tag, I want to know which imagebutton was clicked.I have also mentioned proper ID to each imagebutton in cell.

public void DisplaySeatLayout() {
        //fetching data from database     
        SeatBUS _seatBUS = new SeatBUS();
        DataTable dt = _seatBUS.GetAllSeatByBusRouter(_listBus);

        //Layout generation code    
        ImageButton img ;
        HtmlTable table = new HtmlTable();
        table.Attributes.Add("runat", "server");
        table.Attributes.Add("id", "LayoutTable");

        HtmlTableRow [] tr = new HtmlTableRow[] { new HtmlTableRow(), new HtmlTableRow(), new HtmlTableRow(),new HtmlTableRow(),new HtmlTableRow()};
        HtmlTableCell tc = null;
        int SeatNo=0;
        //Getting Total no of seats.
        int MaxSeatNo = dt.Rows.Count;
        //Iterating datatable

            //Displaying labels for displaying column names in the table
            for (int columnCounter = 0; columnCounter < 8; columnCounter++){

                for (int rowCounter = 0; rowCounter < 5; rowCounter++){

                    if (SeatNo < MaxSeatNo){
                            if (rowCounter == 2 && columnCounter < 7){
                                tc = new HtmlTableCell();
                                Label lbl = new Label();
                                lbl.Text = "";
                                lbl.ID = "lbl " + rowCounter.ToString() + columnCounter.ToString();

                                tc.Controls.Add(lbl);
                                tr[rowCounter].Controls.Add(tc);
                                //reducing seat number for sake of sequence.

                            }
                            else{
                                //adding label in each cell.
                                tc = new HtmlTableCell();
                                Label lbl = new Label();
                                lbl.Text = dt.Rows[SeatNo]["NumberSeat"].ToString();
                                lbl.ID = "lbl " + rowCounter.ToString() + columnCounter.ToString();

                                tc.Controls.Add(lbl);
                                //adding imagebutton in each cell . 
                                img = new ImageButton();
                                img.Attributes.Add("type", "image");
                                img.Attributes.Add("id", rowCounter.ToString());
                                img.CssClass = "seatRightMostRow1";
                                img.ImageUrl = "../Images/available_seat_img.png";
                                img.ID = dt.Rows[SeatNo]["NumberSeat"].ToString();
                                img.Click += new ImageClickEventHandler(Imagebutton_Click);
                                tc.Controls.Add(img);
                                tr[rowCounter].Controls.Add(tc);
                                SeatNo++;
                            }
                    }//SeatNo < MaxSeatNo
                  table.Controls.Add(tr[rowCounter]);
                }
                seatArranngement.Controls.Add(table);
            }

}

    protected void Page_Load(object sender, EventArgs e)
      {
      _listBusBUS = new ListBusBUS();
       _routerBUS = new RouterBUS();
      _listBus = new ListBus();
      _promoteBUS = new PromoteBUS(); 

       if (!Page.IsPostBack)
      {
          LoadData();

      }
    }
    protected override void OnPreRender(EventArgs e)
     {
    if (MultiView1.ActiveViewIndex == 1)
    {
        int listBusID = int.Parse(hdlListBusID.Value.ToString());
        _listBus = _listBusBUS.GetAllListBusById(listBusID);

        litListBusID.Text = _listBus.ListBusID.ToString();
        litRouterID.Text = _listBus.RouterID.ToString();
        litArrival.Text = _listBus.Arrival.ToString();
        litDeparture.Text = _listBus.Departure.ToString();
        litPrice.Text = _listBus.Price.ToString();
         DisplaySeatLayout();
    }
    else if (MultiView1.ActiveViewIndex == 2)
    {

        //original code starts from here
        litListBusIDS.Text = litListBusIDS.Text;
        litRouterIDS.Text = litRouterID.Text;
        litArrivalS.Text = litArrival.Text;
        litDepartureS.Text = litDeparture.Text;
        litSeat.Text = hdlSeat.Value;// chkSeat.SelectedItem.Text;


        litPrices.Text = litPrice.Text;
        //hdlSeat.Value = chkSeat.SelectedItem.Text.ToString();
    }
    else if (MultiView1.ActiveViewIndex == 3)
    {
        litCustomerNameStep4.Text = txtcustomername.Text;
        litSeatStep4.Text = litSeat.Text;
        litPriceStep4.Text = litPrices.Text;
    }
    base.PreRender(e);
}

推荐答案

在页面加载事件上写下以下行:

Write the following line on your page load event:
string controlname = this.Request.Params.Get("__EVENTTARGET");



这将为您提供控件的ID这引发了回发。


This will give you the ID of control that has raised the postback.


这篇关于如何找到点击的图像按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆