繁体   English   中英

如何在 C# 中获取图像按钮 ID?

[英]How do I get an imagebutton ID in C#?

我知道我可以得到一个 imagebutton 的 X&Y,但是我如何得到它的 ID?

我想首先将它打印到 label 上。 稍后我想在 switch 案例中使用它 - 任何不同的案例都会将 imagebutton.imageurl 更改为不同的图像,但特别是为我刚刚单击的 imagebutton 执行此操作。

我试过了

Label1.Text = Convert.ToString((ImageButton)sender);

但这就是结果

 System.Web.UI.WebControls.ImageButton 

结果这没什么帮助,因为我需要特定控件的 ID。

谢谢!

你是这个意思吗?

  Label1.Text = ((ImageButton)sender).ID

更新(根据您的评论):

要更改 ImageURL,您可以使用:

((ImageButton)sender).ImageUrl ="correct_quiz.gif";

或者,如果您想将这两件事结合起来,我建议您这样做:

ImageButton button = ((ImageButton)sender);
Label1.Text = button.ID;
button.ImageUrl = "correct_quiz.gif";
ImageButton b = sender as ImageButton;
if (b != null) {
    string theId = b.ClientID;

    // Change the URL
    b.ImageUrl = "/whatever/you/like.png";
}
((ImageButton)sender).ClientID

或者如果你只想要 ID

((ImageButton)sender).ID

尝试使用 ImageButton 的 ID 属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM