[英]ASP.NET (C#) - click event for dynamically generated ImageButtons
in my website I'm generating seven ImageButtons from which only one (random) is enabled. 在我的网站上,我正在生成7个ImageButtons,其中只启用了一个(随机)。 I want to generate for that button a click event that is triggered only when a specific key combination is pressed (eg: when pressing E+Click). 我想为该按钮生成仅在按下特定组合键时触发的单击事件(例如:按下E +单击时)。
Thanks for the help. 谢谢您的帮助。
protected void Page_Load(object sender, EventArgs e)
{
//Generates a <int, string> dictionary
LoginHelper.CreateDictionary(images);
//'buttons' is an int list
while (buttons.Count < 7)
{
//generates a random number from 1 to 7
int number = LoginHelper.GenerateNumber();
if (buttons.Contains(number) == false)
{
buttons.Add(number);
ImageButton btn = new ImageButton();
btn.CssClass = "loginButtons";
btn.ImageUrl = (from x in images
where x.Key == number
select x.Value).First();
//gets the link string according to the randomized number
btn.PostBackUrl = LoginHelper.GetLink(number);
if (btn.PostBackUrl == string.Empty)
{
btn.Enabled = false;
}
btn.Click += new ImageClickEventHandler(btn_Click);
footer.Controls.Add(btn);
}
}
}
//The event is not triggered
void btn_Click(object sender, ImageClickEventArgs e)
{
ImageButton button = sender as ImageButton;
ConsoleKeyInfo cki = Console.ReadKey();
if (cki.Key == ConsoleKey.E)
{
button.PostBackUrl = "~/about.aspx";
}
}
first you'll need to add the buttons on the Page_Init rather than Page_Load, where it is to late in the page cycle for the event to be registered. 首先,您需要在Page_Init而不是Page_Load上添加按钮,在页面循环的后期,才能注册事件。
about the specific key combination is pressed, I'm pretty sure you'll only be able to accomplish this using Javascript on the client side, sorry I can't help more. 关于特定的键组合被按下,我很确定你只能在客户端使用Javascript完成此操作,抱歉我无能为力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.