如何找到点击的图像按钮?

时间:2013-01-01 13:19:10

标签: asp.net

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

public void DisplaySeatLayout(ListBus _listBus) {
        //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);
            }

}

建议后这是完整的代码。 显示动态表的函数在Page_load

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

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

      }
      DisplaySeatLayout();
    }

此外,我在prerender中复制了代码

     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();
    }
    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);
}

此外,如果我点击任何图像按钮,布局将被创建两次。

1 个答案:

答案 0 :(得分:2)

您所要做的就是获取已放置在“id”属性中的值。

即使您创建了Imagebutton_Click,也可以执行此操作:

 protected void Imagebutton_Click(object sender, EventArgs e)
 {
        ImageButton b = sender as ImageButton;
        string id = b.Attributes["id"]; // Returns the id
 }