将 Class 作为参数传递给图像点击事件处理程序
Passing Class as an argument to an image click event Handler
我有一个 Trip 对象,它是从列表 TripsByTripIds 中获取的,我想将其作为参数传递给下方的图像点击事件处理程序。我该如何通过?
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(imageButton_Click);
AMSPanel1.Controls.Add(imageButton);
AMSPanel1.Height = Unit.Pixel(860);
}
protected void imageButton_Click(object sender, ImageClickEventArgs fi)
{
testimage.ImageUrl = ((ImageButton)sender).ImageUrl;
lblTime.Text = Trip.Time;
lblLocation.Text = Trip.Location; //can't access trip object here
}
如果它可以表示为文本,您可以将您的值添加到 CommandArgument 属性
我在 stack overflow 中的一个帖子中找到了答案,但另一个 day.i 还没有保存 link。但下面的解决方案解决了我的问题
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
}
我有一个 Trip 对象,它是从列表 TripsByTripIds 中获取的,我想将其作为参数传递给下方的图像点击事件处理程序。我该如何通过?
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(imageButton_Click);
AMSPanel1.Controls.Add(imageButton);
AMSPanel1.Height = Unit.Pixel(860);
}
protected void imageButton_Click(object sender, ImageClickEventArgs fi)
{
testimage.ImageUrl = ((ImageButton)sender).ImageUrl;
lblTime.Text = Trip.Time;
lblLocation.Text = Trip.Location; //can't access trip object here
}
如果它可以表示为文本,您可以将您的值添加到 CommandArgument 属性
我在 stack overflow 中的一个帖子中找到了答案,但另一个 day.i 还没有保存 link。但下面的解决方案解决了我的问题
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
}