Hi,

I need some help here..
I am pretty new to doing ASP.net in C#..
and also pretty unfamiliar with javascript
Currently i am facing some problems which i hope you guys can help me in solving..

I have dynamically created a row of buttons.

for (int i = 1; i < 16; i++)
                {

                    btnList1 = new Button();
                    btnList1.Attributes.Add("onclick", "reserveSeat(this, " + i.ToString() + ");");

                       btnList1.ID = (i.ToString());

                       btnList1.Text = i;

                       Panel1.Controls.Add(btnList1);
                    }

and now i wish to actually be able to change their color to blue and retrieve their ID to store into DB. All this i want it to be done onClick on the buttons. I tried trying out with JS.. haha but so far all i can do is to make a alert box.. haha

I hope someone can help me out here.
Thanks

in javascript, you can set any background color by doing this:

document.getElementById("btnid").style.backgroundcolor = <color>;

Also, keep in mind that setting a control's ID to only an integer is bad practice as it can easily be conflicted. Also try to keep consistancy in ID's, as it is becoming a standard. So your btnList1.ID should be equal to "btn" + i.ToString();
regardless, you do not need javascript to store the values or change layout. The great thing about the buttons you are adding is that they are controls, and you can set the OnClick portion of the control that is run on the server to a function also based on the server.

So you can change the btnList1.attributes to:

btnList1.OnClick = reserveSeat(i)

And add a function in your C# script that contains one argument, which would be the seat number to reserve.

You can set the btnList1.Attributes also to:

btnList1.Attributes.Add("style", "background-color:blue;"

That is done by CSS. By adding the style attribute dynamically you can control the look and feel of the controls.

Understand?

Hmm thanks for all the help, but if i were to use

btnList1.OnClick = reserveSeat(i)

How do i go about changing that particular button color that call this event?
let's say a button call 99 was clicked.

and so reserveSeat(99)

and then how do i tell this function what's my button ID?

well if you have the researveSeat(99), with your code you should have your btn id "btn99". So within your function that's called, find the btn 99 and set the color:

Dim btn99 As Button = CType(page.FindControl("btn" & (99 or the argument name you use on the function)), Button)
btn99.Attributes.Add("style","background-color:blue;")

well if you have the researveSeat(99), with your code you should have your btn id "btn99". So within your function that's called, find the btn 99 and set the color:

Dim btn99 As Button = CType(page.FindControl("btn" & (99 or the argument name you use on the function)), Button)
btn99.Attributes.Add("style","background-color:blue;")

hmm is that VB?
hmm i am doing it in C#

This article has been dead for over six months. Start a new discussion instead.