如何使用c#将值从文本框传递到asp.net中的标签 [英] how to pass value from textbox to label in asp.net using c#

查看:64
本文介绍了如何使用c#将值从文本框传递到asp.net中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像当我在一个页面中登录时,相同名称的文本框在另一个网页中标记

like when i login in one page text box that same name come to label in another web page

推荐答案

1。很简单。只需将其缓存(进入用户会话),或者将输入的值(通过查询字符串)发送到文本框中在其他网页的 Page_Load 事件中使用它来初始化其标签。



2.例如:< br $> b $ b

1. It is simple. Just cache it (into user session), or send (by using query string) the value input into the textbox and used it in the Page_Load event of the other web page to init its label.

2.Example:

//In Page1.aspx.cs
protected void SaveButton_Click(object sender, EventArgs e)
{
 string userName = _userNameTextBox.Text.Trim();
 Session["UserName"] = userName;  //Cache the value!
 //...
}

//In Page2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
  //Get the user name value from the cache!
  string userName = (Session["UserName"] == null ? string.Empty : (string)Session["UserName"]);
  //
  //Use it for your label.
  _userNameLabel.Text = userName;
} 
//...
}






在asp.net中有不同的技术可以实现。所有这些都是:



(1)会话

(2)查询字符串

(2)隐藏字段。



现在,使用查询字符串



写入Default1。按钮点击事件中的aspx.cs:



Response.Redirect(Default2.aspx?UserId =+ txtUserId.Text);



然后在要显示的Default2.aspx.cs页面中写入:



protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

this.label1.Text = Request.QueryString [ UserId];

}

}
Hi,

There are different techniques to implement this in asp.net. All these are:

(1) Session
(2) Query string
(2) Hidden field.

Now, using query string:

write in Default1.aspx.cs in button click event :

Response.Redirect("Default2.aspx?UserId="+txtUserId.Text);

Then write in the Default2.aspx.cs page where you want to display :

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.label1.Text = Request.QueryString["UserId"];
}
}


这篇关于如何使用c#将值从文本框传递到asp.net中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆