3

我创建了这个方法,所以它会更新页面上的标签。我们这里有一堆复选框,指示此人是否有任何限制。当用户登录并看到复选框时,我希望标签显示已选中的复选框,以便用户更轻松地查看此人有哪些限制。

我是 C# 和 ASP.NET 的新手,我想知道,有没有更好/更简单/更有效的方法来解决这个问题?我发现的第一件事是 String.Concat,这就是我在下面的代码中使用它的原因。另外,我似乎无法在后面的代码中使用 line return (a (br /)) (我知道 (br /) 在后面的代码中不起作用)。我尝试了许多不同的方法,但似乎都没有奏效,这就是为什么我在字符串中添加了一个逗号。有什么建议么?

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);
}
4

4 回答 4

3
  1. 使用StringBuilder构建字符串。使用 AppendLine 创建一个新行。
  2. 将您的 if 语句放在单行上。像这样的简短 if 语句会更容易阅读。
  3. 如果您需要在 ASP.NET 标签中出现新行,则需要嵌入<br/>标签。

    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();
    

当您对 C# 和 ASP.NET 更加熟悉时,我建议您继续使用像CheckBoxList这样的数据绑定控件

参考

于 2013-03-01T19:09:27.440 回答
1

是的,如果您不使用它,您可以在 CheckBox 的属性(如工具提示)中设置要附加到字符串的文本。然后你可以使用这个循环:

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);

's' 有你想要的字符串;

于 2013-03-01T19:19:18.383 回答
0

试试 asp.net 中的 CheckBoxList 控件。这里的例子:http ://www.w3schools.com/aspnet/showasp.asp?filename=demo_checkboxlist

于 2013-03-01T19:11:17.587 回答
0

您可以使用更少的行代码获得相同的结果。我喜欢以这种方式使用 List,因为我不必处理尾随分隔符。

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());

正如评论中所建议的,我认为多个复选框是最好的解决方案。

如果需要换行:

LblLimits.Text = string.Join("<br/>", selectedEntries.ToArray());
于 2013-03-01T19:24:12.390 回答