我创建了一个动态显示公共汽车座位布局的函数。现在,在将所有这些动态创建的表格和单元格添加到标签中之后,我想知道单击了哪个图像按钮。我还提到了单元格中每个图像按钮的正确 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();
}
另外在这里我已经在预渲染中复制了代码
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);
}
此外,如果我单击任何图像按钮,则会创建两次布局。