如何格式化列表框中的文本

时间:2017-02-11 19:14:36

标签: asp.net

我必须在ASP.NEt中创建一个程序。它需要一个文本框,一个按钮和一个列表框。当我填写文本框并点击按钮时,文本框中的文本将添加到列表框中,第一个字母为大写,其余为小写。例如,当我输入“hELLO my friend”时,它会显示“Hello My Friend”。我知道如何做的逻辑,但我不知道为什么按钮不起作用。 这是我的代码:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script runat="server">
        public void Button1_Click(Object sender, EventArgs e)
        {
              ListBox1.Items.Add(TextBox1.Text);
           string s = TextBox1.Text;
            char[] a = s.ToLower().ToCharArray();
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = i == 0 || a[i - 1] == ' ' ? char.ToUpper(a[i]) : a[i];
            }

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server"  OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script runat="server">
  void Page_Load(Object sender, EventArgs e)
    {

        Button1.Click += new EventHandler(this.Button1_Click);
    }
            public void Button1_Click(Object sender, EventArgs e)
            {
                  ListBox1.Items.Add(TextBox1.Text);
               string s = TextBox1.Text;
                char[] a = s.ToLower().ToCharArray();
                for (int i = 0; i < a.Length; i++)
                {
                    a[i] = i == 0 || a[i - 1] == ' ' ? char.ToUpper(a[i]) : a[i];
                }

            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
            <br />
            <br />
            <asp:ListBox ID="ListBox1" runat="server"  OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
        </div>
        </form>
    </body>
    </html>