简体   繁体   中英

Is there a better way of doing this? ASP.NET/C#/Code Behind

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);
}
  1. Use a StringBuilder to build a string. Use AppendLine to create a new line.
  2. Put your if statements on single lines. It will be easier to read for short if statements like this.
  3. 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

Yes,you can set the Text that you want to append to your string in on of the CheckBox's properties, like ToolTip, if you don't use it. Then you can use this loop :

string s = "";
        foreach (Control item in this.Controls)
        {
            if (item is CheckBox)
            {
                CheckBox temp = item as CheckBox;
                if (temp.Checked)
                {
                    s += temp.ToolTip + ", ";
                }
            }
        }
s = s.Substring(0,s.length-3);

The 's' has the string that you want;

Try the CheckBoxList control in asp.net. Example here : http://www.w3schools.com/aspnet/showasp.asp?filename=demo_checkboxlist

You can achieve the same result using less line code. I enjoy working with List this way because I dont have to handle the trailing separator.

var checkBoxes = new CheckBox[] {chkBend, chkDryDusty, chkHearing, ...}
var values = new string[] {"Bend/Stoop", "Dry/Dusty", "Hearing", ...}

var selectedEntries = new List<string>();

for(var i = 0; i < checkBoxes.Length; i++)
{
    if (checkBoxes[i].Checked)
        selectedEntries.Add(values[i]);
}

if (TextBox1.Text != "")
    selectedEntries.Add(TextBox1.Text);

LblLimits.Text = string.Join(", ", selectedEntries.ToArray());

As proposed in comments, I think the multiple checkbox is the best solution.

If you need a line break:

LblLimits.Text = string.Join("<br/>", selectedEntries.ToArray());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM