【问题标题】:New to c# and asp.net My mods to aspx.cs not seen in the htmlc# 和 asp.net 的新手 我对 aspx.cs 的 mods 在 html 中看不到
【发布时间】:2016-01-30 23:33:59
【问题描述】:

我已经使用 c# 在 Web 应用程序 (webform) 中加载了一个新页面。我在设计中添加了一个文本框和一个按钮。我双击按钮并在 aspx.cs 页面中添加了以下内容:

 protected void Button1_Click(object sender, EventArgs e)
    {
        string name = "Tom";
        MessageBox.Show("My name is " + name);

    }

据我所知,这应该在按钮内添加文本“我的名字是汤姆”。但我没有在设计中看到这一点(按钮是按钮中显示的全部内容),也没有在 html 中(如果在设计中没有看到新文本,这很有意义)。

<body>
<form id="form1" runat="server">
<div>

</div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>

为什么.cs文件没有反映在html文件中?

谢谢 汤姆

【问题讨论】:

  • >>据我所知,这应该在按钮内添加文本“我的名字是汤姆”。这不会这样做。代码隐藏不会更新标记。因此,您不会在 Designer 中获得文本。另外,MessageBox.Show 是服务器代码。您的用户不会在他们的浏览器中看到它。
  • @Gosha_Fighten 你犯了一个小错误。你是对的,MessageBox 不会更新按钮的文本,但是你说后面的代码不会更新标记是错误的。
  • @miner_tom 如果要更新按钮的文本,只需在 Button1_Click 方法中使用以下代码: Button1.Text = "My name is " + name;你会看到你的按钮的文字已经改变了

标签: c# asp.net


【解决方案1】:

要将文本放在按钮旁边,请使用文字控件:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Literal ID="litMessage" runat="server" />

然后在代码中:

protected void Button1_Click(object sender, EventArgs e)
{
    string name = "Tom";
    litMessage.Text = "My name is " + name;
}

消息只会在运行时显示,在点击事件发生后。


顺便说一句,MessageBox 是在 Windows 窗体应用程序(即应用程序)中使用的东西。如果您想在网站上向用户显示弹出消息框,您需要use JavaScript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多