Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从其他类访问网页文本框?_C#_Asp.net_Class_Textbox_Controls - Fatal编程技术网

C# 如何从其他类访问网页文本框?

C# 如何从其他类访问网页文本框?,c#,asp.net,class,textbox,controls,C#,Asp.net,Class,Textbox,Controls,我有一个名为Default.aspx的网页和一个名为textBox1的文本框 在Default.aspx.cs中,我可以通过键入以下内容来设置文本: TextBox1.text = "change text"; 现在我创建了另一个类。在这个类中如何调用textBox1?所以我想更改这个类中textBox1的文本 到目前为止,我试着这样做,它在Mymethod中运行良好,但在Myclass中不起作用 using System; using System.Collections.Generic;

我有一个名为Default.aspx的网页和一个名为textBox1的文本框

在Default.aspx.cs中,我可以通过键入以下内容来设置文本:

TextBox1.text = "change text";
现在我创建了另一个类。在这个类中如何调用textBox1?所以我想更改这个类中textBox1的文本

到目前为止,我试着这样做,它在Mymethod中运行良好,但在Myclass中不起作用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Drawing; 
 using System.Threading;

 namespace WebApplication1
 {
 public partial class Default : System.Web.UI.Page
 {

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void submitEventMethod2(object sender, EventArgs e)
    {
        this.Mymethod();
    }
    public void mymethod1()
    {
       TextBox1.Text = "some text";

    }
    class Myclass
    {
     public void mymethod2()
     {
       TextBox1.Text = "some text";
     }
    }
}
}

我有一个名为Default.aspx的网页和一个名为textBox1的文本框

在Default.aspx.cs中,我可以通过键入以下内容来设置文本:

TextBox1.text = "change text";
TextBox1.text=更改文本; 现在我创建了另一个类。在这个类中如何调用textBox1?所以我想更改这个类中textBox1的文本

到目前为止,我试着这样做,它在Mymethod中运行良好,但在Myclass中不起作用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Drawing; 
 using System.Threading;

 namespace WebApplication1
 {
 public partial class Default : System.Web.UI.Page
 {

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void submitEventMethod2(object sender, EventArgs e)
    {
        this.Mymethod();
    }
    public void mymethod1()
    {
       TextBox1.Text = "some text";

    }
    class Myclass
    {
     public void mymethod2()
     {
       TextBox1.Text = "some text";
     }
    }
}
}
使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControl; 使用系统文本; 使用System.Net.Sockets; Net系统; 使用系统图; 使用系统线程

 namespace WebApplication1
 {
 public partial class Default : System.Web.UI.Page
 {

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void submitEventMethod2(object sender, EventArgs e)
    {
        this.Mymethod();
    }
    public void mymethod1()
    {
      Myclass myClass=new Myclass ();
      myClass.mymethod2(TextBox1);

    }
    class Myclass
    {
     public void mymethod2(TextBox textBox)
     {
       textBox.Text = "some text";
     }
    }
}
}
你不能在课堂上访问这样的文本框文本。您也可以这样访问它:

class Myclass
{   
   private string _myText;
   public string mystring
   {
      get 
      {
         return _myText;
      }
      set 
      {
         _myText = value;
      }
   }
}
public void Mymethod()
{
   Myclass obj = new Myclass();
   obj.mystring = TextBox1.Text.Trim();
   //do what else you want
}
为此使用会话[]

e、 g

在另一个类中,使用此会话[]将文本分配给另一个文本框

TextBox2.Text=Session["TextBox_Text"].ToString();
希望这对你有帮助

谢谢。

创建另一个类

public class DataAccess
{
 private string _value;

public string value
    {
        get
        {
            return this._value;
        }
        set
        {
            this._value = value.Trim();
        }
    }

}
在aspx.cs页面中使用这些代码并创建对象DataAccess 班因此,您可以访问文本框值


嘿,我仍然得到这个错误。textBox1=一些文本;错误是名称textBox1在当前上下文中不存在。它应该立即编译,我在代码中没有看到任何错误。抱歉,我的问题发布错误。我现在正在编辑。请您重新检查。谢谢。它在此语句SessionTextBox_Text=textBox1.Text处给我一个错误;哎呀,我真的很抱歉我的会话语法是它的Session[]而不是Session。对此,我深表歉意。很抱歉,我发布了错误的问题。我现在正在编辑。请您重新检查。谢谢您的回复,但我正在尝试从mymethod2访问TextBox1,mymethod2是在Myclass中定义的。我尝试了您的解决方案,但在放置TextBox1时出错。Text=objDaAccess.value;在MyMethod2中,在类中使用session session[memberid]=TextBox1.Text和access,如string value=session[memberid].tostring;