将Class作为参数传递给图像单击事件处理程序

时间:2015-08-11 02:37:11

标签: c# asp.net webforms

我有一个Trip对象,我从列表中获取TripsByTripIds,我想将其作为参数传递给下面的图像点击事件处理程序。我怎么通过它?

HTTP Error - 400 Bad Request

2 个答案:

答案 0 :(得分:1)

我在前几天发现了一个堆栈溢出的帖子的答案。虽然我没有保存该链接。但下面的解决方案解决了我的问题

  foreach (Tripclass Trip in TripsByTripIds )
  {
      ImageButton imageButton = new ImageButton();
      imageButton.ImageUrl = "~/" + Trip.CorridorName + "/" + Trip.Time + "/" + Trip.ImgFileName;
      imageButton.Height = Unit.Pixel(100);
      imageButton.Style.Add("padding", "5px");
      imageButton.Width = Unit.Pixel(100);
      imageButton.Click += new ImageClickEventHandler((a, b) => imageButton_Click(a, b,Trip));
      AMSPanel1.Controls.Add(imageButton);
      AMSPanel1.Height = Unit.Pixel(860);
  }

  protected void imageButton_Click(object sender, ImageClickEventArgs e, Tripclass Trip)
  {
      testimage.ImageUrl = ((ImageButton)sender).ImageUrl;
      lblTime.Text = Trip.Time; 
      lblLocation.Text = Trip.Location; //I can access trip object here
  }   

答案 1 :(得分:0)

如果可以将其表示为文字,则可以将您的值添加到CommandArgument属性