ASP.NET C# 将文本框中的值写入标签:初学者简单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12424648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:14:12  来源:igfitidea点击:

ASP.NET C# write value in text box to a label: beginner simple

c#asp.nettextboxdocument.write

提问by Justin

I am trying to find a very simple example for the syntax to simple write the value entered into a text box, and place that value into a label onclick. As basic as it can get I suppose. I only mentioned asp.net because I would like to do that with an aspx page. Thanks!

我试图找到一个非常简单的语法示例,将输入的值简单地写入文本框中,然后将该值放入标签 onclick 中。我想尽可能基本。我只提到了 asp.net,因为我想用一个 aspx 页面来做到这一点。谢谢!

采纳答案by Mutu Yolbulan

in the aspx page

在aspx页面中

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

and in the cs file

并在cs文件中

protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = TextBox1.Text;
        }

回答by Alessandro

Labeldoes not have a server OnClickevent.

Label没有服务器OnClick事件。

You could use a Button:

你可以使用一个按钮:

void [ButtonID]_OnClick(object sender, EventArgs e) 
{    
   [LabelID].Text = [TextBoxID].Text; 
}

Replace the values inside square brackets with the real IDs.

用真实 ID 替换方括号内的值。

Or you could use Javascript, but that's another story! :)

或者您可以使用 Javascript,但那是另一回事了!:)

回答by Jduv

The page:

这一页:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="MyLabel" runat="server">Hello World</asp:Label>
<br />
<asp:TextBox ID="MyTextBox" runat="server" />
<br />
<asp:Button ID="MyBtn" runat="server" Text="Click Me" OnClick="MyBtn_Clicked" />
</asp:Content>

The code behind:

背后的代码:

namespace WebApplication1
{
   using System;

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

       }

       protected void MyBtn_Clicked(object sender, EventArgs args)
       {
           this.MyLabel.Text = this.MyTextBox.Text;
       }
   }
}

回答by raehtytr

namespace WebApplication1
{
   using System;

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

       }

       protected void MyBtn_Clicked(object sender, EventArgs args)
       {
           this.MyLabel.Text = this.MyTextBox.Text;
       }
   }
}

回答by Ike Harrison

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = TextBox1.Text;
    }
}