Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am creating online examination project in asp.net & c#.
I want to display buttons for each question at the bottom of my .aspx web page, so that whenever student clicks button, question for that button will be displayed.
I am creating buttons dynamically as :
C#
void CreateButtons()
{
    for (int i = 1; i < 70; i++)
    {

        Button btn = new Button();
        btn.Text = i.ToString();

        btn.Font.Bold = true;
        btn.ForeColor = System.Drawing.Color.Black; ;
        btn.BackColor = System.Drawing.Color.LightSkyBlue;
        btn.Font.Size = 10;
        btn.Width = Unit.Pixel(50);
        pnlButtons.Controls.Add(btn);
        pnlButtons.Visible = true;
        btn.Click += new EventHandler(btn_Click);

    }
}

and getting the questions in
C#
void btn_Click(object sender, EventArgs e)
{
}


Now i want to change color of each button as soon as answer is selected in radiobutton list.
On selectedindex of radiobutton list, i m not able to access dynamically created buttons.

Can anyone let me know how to change colour of Dynamically created button on radiobuttonlist selected event.


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 9-Sep-11 19:26pm
v2

It's a little complicated, but not too bad:
1) Add this line to your CreateButtons method:

C#
btn.Width = Unit.Pixel(50);
btn.ID = string.Format("butDynamic{0}", i);    // Give the button a unique ID
pnlButtons.Controls.Add(btn);

2) Add a Hidden field to your Page:
ASP.NET
<asp:HiddenField ID="PressedButton" runat="server" />

3) Add this code to your Button handler:
C#
void btn_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        // Save the last pressed ID for later
        PressedButton.Value = b.ID;
        }
    }

4) Add this code to your SelectedIndexChanged event:
C#
if (PressedButton.Value != null)
    {
    Button b = (Button) (pnlButtons.FindControl((string) PressedButton.Value));
    if (b != null)
        {
        b.BackColor = System.Drawing.Color.Red;
        }
    }

Do bear in mind that you only get the Selected Index changed event when the RadioListBox loses the focus - NOT when the user makes a selection.
 
Share this answer
 
Comments
[no name] 10-Sep-11 2:14am    
good one :) my 5
akkanshagupta 10-Sep-11 3:23am    
I tried the above Solution(i.e Solution 1) but, this is working only when i click button to display question.
I also have Next and previous Buttons, and student can answer by clicking next or previous buttons also.
In that case..PressButton value and button ID is null.
So i want, as soon as student click radiobutton list, it will change the colour for tht button.
OriginalGriff 10-Sep-11 4:06am    
You can't - as I said: "you only get the Selected Index changed event when the RadioListBox loses the focus - NOT when the user makes a selection"
This means that the user has to select an option and then press somewhere else than the RadioListBox before you are notified. You might want to consider using separate RadioButtons, but again, you will only get a server side event when the button looses the focus.
You may want to consider using a client side handler instead: http://blog.js-development.com/2008/06/attaching-client-side-event-handler-to.html
If u got answer please mark up
 
Share this answer
 
The problem is in the event handler.

Try this code:

private void button1_Click(object sender, EventArgs e)
{
    int distance = 0;
    EventHandler evh = new EventHandler(btn_Click);

    for (int i = 0; i < 5; i++)
    {
        Button btn = new Button();
        btn.Text = i.ToString();

        btn.ForeColor = System.Drawing.Color.Black; ;
        btn.BackColor = System.Drawing.Color.LightSkyBlue;
        btn.Width = 50;
        btn.Location = new Point(distance, 0);
        distance = distance + 50;
        this.Controls.Add(btn);
        btn.Click += evh;
    }

}

void btn_Click(object sender, EventArgs e)
{
    string a = string.Empty;
}


Cheers
 
Share this answer
 
Comments
venul 8-Aug-12 2:32am    
same thing i also doing ...how to change the dynamic button backgroud color when Radio button click event..
Mario Majčica 8-Aug-12 3:50am    
Well, each time you create a controls dynamically, on postback you need to recreate them, otherwise the viewstate can't be unpacked and you can't get what happened on client side. Once this is done and you got the right action, then you can change the properties and resend the page to the client again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900