简体   繁体   English

如何在ASP.NET C#中使用if(IsPostBack)

[英]How do you use if (IsPostBack) in ASP.NET C#

How can I used if (IsPostBack){} to display user's names from two text boxes into another text box? 如何使用if (IsPostBack){}将用户名从两个文本框中显示到另一个文本框中?

I have 3 text boxes and a button. 我有3个文本框和一个按钮。 The text boxes are named txtLastName, txtGivenName, txtOutput. 这些文本框被命名为txtLastName,txtGivenName,txtOutput。 My button is btnSubmit. 我的按钮是btnSubmit。

How can I display text from the txtLastName and txtGivenName in the txtOutput text box? 如何显示txtOutput文本框中的txtLastName和txtGivenName中的文本?

How can I display it as: First (space) Lastname or Last, Firstname in this code. 如何将其显示为:此代码中的名字(空格)姓氏或姓氏,名字。

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
    }
}

Create an event handler for the Click event on the button and then in the code behind, do like this: 在按钮上为Click事件创建一个事件处理程序,然后在后面的代码中,如下所示:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    txtOutput.Text = string.Format("{0} {1}", txtGivenName.Text, txtLastName.Text);
}

How can I display text from the txtLastName and txtGivenName in the txtOutput text box? 如何显示txtOutput文本框中的txtLastName和txtGivenName中的文本?

  1. Go to the design of your page. 转到页面的设计。
  2. Click the button. 点击按钮。
  3. Click F4 or Right click and select properties. 单击F4或右键单击并选择属性。 This will show you a window for button. 这将显示一个按钮窗口。
  4. Click the event. 单击事件。
  5. Double click the "Click" action. 双击“点击”操作。
  6. This will navigate you to the code behind. 这会将您导航到后面的代码。
  7. Write the code in this handler 在此处理程序中编写代码
  8. This is how the design will look like for your event handler 这就是事件处理程序的设计外观

在此处输入图片说明


protected void btnSubmit_Click(object sender, EventArgs e)
{
   txtOutput.Text = string.Format("{0} {1}", txtGivenName.Text, txtLastName.Text);
}

How can I display it as: First (space) Lastname or Last, Firstname in this code. 如何将其显示为:此代码中的名字(空格)姓氏或姓氏,名字。

Write down the code in the handler as below. 如下所示在处理程序中写下代码。

txtOutput.Text = txtLast.Text + ", " + txtFirst.Text;

try this 尝试这个

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        txtOutput.Text = txtLastName.Text + " " + txtLastName.Text;
}

Why don't use use the submit button method to achieve this? 为什么不使用提交按钮方法来实现此目的?

protected void btnSubmit_Click(object sender, System.EventArgs e)
{
    txtOutput.Text = txtLast.Text + ", " + txtFirst.Text;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#ASP.net DropDownList在IsPostBack下返回null - C# ASP.net DropDownList returns null under IsPostBack 与IsPostBack有一个小问题(ASP.NET,C#) - Having a small issue with IsPostBack (ASP.NET, C#) IsPostBack在ASP.NET中失败了吗? - IsPostBack failing in ASP.NET? 如果您知道起始点和停止点但不知道 asp.net C# 中的中间点,如何拆分字符串 - How do you split a string if you know the start and stop point but not the middle in asp.net C# 如何配置SWFUpload与ASP.NET C#WebService一起使用? - How do you configure SWFUpload to work with an ASP.NET C# WebService? 如何使用AjaxFileUpload.js将HTML输入文件传递到C#ASP.Net? - How do you pass HTML input files to C# ASP.Net using AjaxFileUpload.js? 如何使用asp.net和c#流式传输Excel 2007或Word 2007文件 - How do you stream an Excel 2007 or Word 2007 file using asp.net and c# 如何从ASP.NET事件调用C#函数? - How do you call a C# function from an ASP.NET event? 如何使用 ASP.NET 内核制作 Razor 页面和 C# 代码框架? - How to do you make both a Razor Page and C# code framework with ASP.NET Core? 如何在C#类(.cs文件)和ASP.NET .aspx文件中使用相同的OracleMembershipProvider? - How can you use the same OracleMembershipProvider in C# classes (.cs files) AND ASP.NET .aspx files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM