I created this method so it would update a LABEL on the page. What we have here is a bunch of checkboxes indicating if the person has any limitations. When the user logs on and sees the checkboxes, I want the label to display the ones that are checked making it easier for the user to see what limitations the person has.
I'm new to C# and ASP.NET and what I want to know, is there a better/easier/more efficient way of going about this? The first thing I found was String.Concat which is why I used it in the code below. Also, I can't seem to get a line return (a (br /)) to work in the code behind (I do understand that (br /) won't work in the code behind). I've tried many different methods and none seem to work which is why I threw a COMMA in the string. Any suggestions?
protected void LblLimitsLabel()
{
String strBend = "", strDryDust = "", strStanding = "", strHearing = "", strHeights = "", strHepatitis = "", strLifting = "", strMachines = "", strPulling = "", strPushing = "", strReaching = "", strSeizures = "", strSuddenTemp = "", strVisual = "", strWalking = "", strWheelchair = "", strHelmet = "", strOther = "";
if (chkBend.Checked)
strBend = "Bend/Stoop, ";
if (chkDryDusty.Checked)
strDryDust = "Dry/Dusty, ";
if (chkHearing.Checked)
strHearing = "Hearing, ";
if (chkHeights.Checked)
strHeights = "Heights, ";
if (chkHepatitis.Checked)
strHepatitis = "Hepatitis, ";
if (chkLifting.Checked)
strLifting = "Lifting, ";
if (chkMachines.Checked)
strMachines = "Machines, ";
if (chkPulling.Checked)
strPulling = "Pulling, ";
if (chkPushing.Checked)
strPushing = "Pushing, ";
if (chkReaching.Checked)
strReaching = "Reaching, ";
if (chkSeizures.Checked)
strSeizures = "Seizures, ";
if (chkStanding.Checked)
strStanding = "Standing, ";
if (chkSuddenTemp.Checked)
strSuddenTemp = "Sudden Temp. Changes, ";
if (chkVisual.Checked)
strVisual = "Visual, ";
if (chkWalking.Checked)
strWalking = "Walking, ";
if (chkWheelchair.Checked)
strWheelchair = "Wheelchair, ";
if (chkHelmet.Checked)
strHelmet = "Helmet, ";
strOther = TextBox1.Text;
LblLimits.Text = String.Concat(strBend, strDryDust, strStanding, strHearing, strHeights, strHepatitis, strLifting, strMachines, strPulling, strPushing, strReaching, strSeizures, strSuddenTemp, strVisual, strWalking, strWheelchair, strHelmet, strOther);
}
If you need new lines to appear in in an ASP.NET Label, you will need to embed <br/>
tags.
StringBuilder SB = new StringBuilder();
if (chkBend.Checked) SB.AppendLine("Bend/Stoop, ");
if (chkDryDusty.Checked) SB.AppendLine("Dry/Dusty, ");
// and so on
SB.Append(TextBox1.Text);
LblLimits.Text = SB.ToString();
When you get more comfortable with C# and ASP.NET, I suggest moving on to a databound control like CheckBoxList
References