Monday, March 26, 2012

Trouble Looping through asp:button Names

I am trying to loop through an array of button names:
<code>

//Some where else on the codebehind page
setBorderColor(e.CommandName);

publicvoid setBorderColor(string btnName)
{
string buttonName = btnName;

//create array of button names
string [] btnNames = {"btnIntro", "btnVisit", "btnVisitor", "btnVehicle"};
for(int iCount = 0; iCount < 3; iCount++)
{
if(btnNames[iCount] == btnName)
{
btnNames[iCount].BorderColor = Color.Red;
btnNames[iCount].BorderWidth = 1;
}
else
{
btnNames[iCount].BorderColor = Color.Blue;
btnNames[iCount].BorderWidth = 1;
}
}
}

</code>
The Error is: 'string' does not contain a definition for 'BorderColor'
I know why the error is happening, I just don't know how to fix it. Thanks for your help in advance.

You can do it this way:

publicvoid setBorderColor(string btnName)
{
//create array of button names
string [] btnNames = {"btnIntro", "btnVisit", "btnVisitor", "btnVehicle"};
for(int iCount = 0; iCount < 3; iCount++)
{
string name = btnNames[iCount];
Button btn =this.FindControl(name)as Button;
if(btn == null)
return;
if(name == btnName)
{
btnBorderColor = Color.Red;
btn.BorderWidth = 1;
}
else
{
btn.BorderColor = Color.Blue;
btn.BorderWidth = 1;
}
}
}

0 comments:

Post a Comment