0

in my website I'm generating seven ImageButtons from which only one (random) is enabled. I want to generate for that button a click event that is triggered only when a specific key combination is pressed (e.g: when pressing E+Click).

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";
        }
    }
4
  • What is the problem you're experiencing? Commented Sep 5, 2011 at 15:58
  • 4
    when you debug, do you enter inside the btn_click method? That could be, but I have serious issues that you read the clicked button from the Console while running an ASP.NET web application on the server, considering that the user is connected remotely with a browser. Commented Sep 5, 2011 at 15:59
  • What is footer and what is buttons?? Commented Sep 5, 2011 at 16:02
  • no, it does not enter the method. footer is the <div> where the buttons are added and 'buttons' is an int list where the button's number is saved. Commented Sep 5, 2011 at 16:10

1 Answer 1

2

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.