如何使用文本框执行添加

时间:2014-01-08 18:54:40

标签: c# asp.net

我有一个文本框。在按钮单击事件上,文本框的值应作为结果显示在标签中。 再次单击该按钮并在“文本”框中输入一些输入时,应将输入添加到结果中。 我的意思是重复按钮点击并提供输入应该添加结果。 如何执行此操作?

我仍然收到错误

"System.FormatException: Input string was not in a correct format."
 Error is regarding: lblconsumed.Text = (int.Parse(lblconsumed.Text) + userValue).ToString();

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.ServRef;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        ServRef.ProteinServiceClient sc = new ServRef.ProteinServiceClient();
        UserDetail userinfo = new UserDetail();

        protected void Page_Load(object sender, EventArgs e)
        {
            // ddlUser.DataSource = sc.AddUser(userinfo);
            BindUserDetails();
        }

        protected void BindUserDetails()
        {
            IList<UserDetail> objUserDetails = new List<UserDetail>();
            //objUserDetails = objService.GetUserDetails("");

            objUserDetails = sc.GetAllUser("");

            ddlUser.DataSource = objUserDetails;
            ddlUser.DataBind();
        }




        protected void btnAddUser_Click(object sender, EventArgs e)
        {

            userinfo.user_name = tbname.Text.Trim();
            userinfo.user_goal = Convert.ToInt32(tbgoal.Text.Trim());
            string result = sc.AddUser(userinfo);

            lblgoal.Text = Convert.ToString(userinfo.user_goal);

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int userValue;
            if (int.TryParse(tbamount.Text, out userValue))
            {
                lblconsumed.Text = (int.Parse(lblconsumed.Text) + userValue).ToString();
            }
        }


    }
}`

我的aspx页面:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Style.css" rel="stylesheet" />

</head>
<body>
    <form id="form1" runat="server">
        <div id="main">

            <h2>Protein Tracker</h2>

            <div id="selectuser">
                <label for="select-users">Select a user&nbsp: </label>
                <asp:DropDownList ID="ddlUser" runat="server"
                     Width="60px" AutoPostBack="True" 
                     DataTextField="user_name" 
                    DataValueField="user_name" ></asp:DropDownList>

            </div>
            <hr />
            <div id="adduser">
                <h2>Add new user</h2>
                <label for="name">User Name&nbsp:</label>

                <asp:TextBox ID="tbname" runat="server" style="margin-left: 10px"></asp:TextBox>
               <br /><br />
                 <label for="goal">User Goal&nbsp:</label>
                <asp:TextBox ID="tbgoal" runat="server" style="margin-left: 20px"></asp:TextBox>

                <br /><br />

                <asp:Button ID="btnAddUser" runat="server" Text="Add" style="margin-left: 210px" OnClick="btnAddUser_Click" />

            </div>
            <hr />
            <div id="addprotein">
                <h2>Add protein</h2>
                <label for="amount">Amount&nbsp:</label>
                <asp:TextBox ID="tbamount" runat="server"></asp:TextBox>
               <br />
                <br />
                <asp:Button ID="Button2" runat="server" Text="Add" style="margin-left: 180px" OnClick="Button2_Click"/>

            </div>
            <hr />
            <div>
                <p>Total Consumed&nbsp:&nbsp <asp:Label ID="lblconsumed" runat="server" Text=""></asp:Label></p>
                <p>Goal Set&nbsp:&nbsp  <asp:Label ID="lblgoal" runat="server" Text=""></asp:Label></p>
            </div>

        </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

@ user3174595:我已经输入了简单的代码。它是最简单的操作。

form1.aspx

<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
    <asp:Label ID="Label1" runat="server" Text="0" ></asp:Label>
</div>
</form>

form1.aspx.cs

public partial class addition : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        double userValue = 0;
        if (double.TryParse(TextBox1.Text,out userValue))
        {
            Label1.Text = (double.Parse(Label1.Text) + userValue).ToString();
        }
    }
}