How to Access web page TextBox from another class? - c#

I have a web page called Default.aspx and a textbox called textBox1
In the Default.aspx.cs, I can set the text by typing:
TextBox1.text = "change text";
Now I have created another class. How do I call textBox1 in this class? so I want to change the text for textBox1 in this class.
So far i tried like this it is working fine in Mymethod but it is not working in 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";
}
}
}
}

I have a web page called Default.aspx and a textbox called textBox1
In the Default.aspx.cs, I can set the text by typing:
TextBox1.text = "change text";
Now I have created another class. How do I call textBox1 in this class? so I want to change the text for textBox1 in this class.
So far i tried like this it is working fine in Mymethod but it is not working in 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()
{
Myclass myClass=new Myclass ();
myClass.mymethod2(TextBox1);
}
class Myclass
{
public void mymethod2(TextBox textBox)
{
textBox.Text = "some text";
}
}
}
}

Use Session[] for this.
e.g.
TextBox1.Text="abc";
Session["TextBox_Text"]=TextBox1.Text;
and in other class use this Session[] to assigning text to another TextBox by using,
TextBox2.Text=Session["TextBox_Text"].ToString();
Hope this will help you
Thank You.

You cannot access TextBox Text like this in class. You can access it like this as well:
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
}

create another class
public class DataAccess
{
private string _value;
public string value
{
get
{
return this._value;
}
set
{
this._value = value.Trim();
}
}
}
use these code in your aspx.cs page and create object DataAccess
class. so you can access textbox value.
public partial class Default : System.Web.UI.Page
{
DataAccess objDaAccess = new DataAccess();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitEventMethod2(object sender, EventArgs e)
{
this.Mymethod();
}
public void Mymethod()
{
TextBox1.Text = "some text";
objDaAccess.value=TextBox1.Text;
}
class Myclass
{
TextBox1.Text = objDaAccess.value;
}
}

Related

How do I make a variable accessible from every method of class?

I have an int named "mode". I want to make every function be able to access it.
Here is my code.
namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int wow = mode - 1;
}
private void Form5_Load(object sender, EventArgs e)
{
int mode = 4;
}
}
}
Just make it a property of the class.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
public int mode {get; set;}
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int wow = mode - 1;
}
private void Form5_Load(object sender, EventArgs e)
{
mode = 4;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
public int mode;
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int wow = mode - 1;
}
private void Form5_Load(object sender, EventArgs e)
{
mode = 4;
}
}
}
However, I'd be surprised if there wasn't a SO page about this. Also I'd recommend looking at MSDN and other programming c#.net resources.

No Extension Method for Client Name in C#

Given the code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetworksApi.TCP.CLIENT;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form1 client;
public Form1()
{
InitializeComponent();
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text!= "" &&textBox4.Text!="")
{
client = new Form1();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
}
private void button3_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
}
I get the following error message whenever I try to compile:
'WindowsFormsApplication1.Form1' does not contain a definition for
ClientName and no extension method 'ClientName' accepting a first
argument of type.
Do you have any idea on how to fix this?
There is no ClientName property on a Windows Form class. However, since you are inheriting from Form, you can add one. But that doesn't make sense either. Are you sure you want a variable of type Form1 to have properties for ClientName, ServerIP, and a method for Connect()? Much more likely you want either some other pre-existing class or to make your own.
public class ClientService
{
public string ClientName {get; set;}
public string ServerIp {get; set;}
public void Connect()
{
//logic here
}
}
And change your UI logic to
if (!String.IsNullOrEmpty(textBox3.Text) && !String.IsNullOrEmpty(textBox4.Text))
{
var client = new ClientService();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
This is the documentation for the Form class in .NET: https://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx
Notice there's no member for ClientName listed. You cannot reference it because it doesn't exist.

An object reference is required for the non-static field, why?

I did some research about this error, and all awnsers i found include removing static from the method or the property, but in my code there isnt any static, so i dont know whats happening, thanks for your help.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class textoTitular : Form
{
public textoTitular()
{
InitializeComponent();
}
private void textoTitular_Load(object sender, EventArgs e)
{
textoTitular.Text = "testing"; /// prints testing on the textbox
}
}
}
Your problem is in
private void textoTitular_Load(object sender, EventArgs e)
{
textoTitular.Text = "testing"; /// prints testing on the textbox
}
You are referencing the form class in a static way.
Rather try using this. Something like
private void textoTitular_Load(object sender, EventArgs e)
{
this.Text = "testing"; /// prints testing on the textbox
}
Added bonus, you can omit the this and use the object property
private void textoTitular_Load(object sender, EventArgs e)
{
Text = "testing"; /// prints testing on the textbox
}

Set textbox of a usercontrol from another usercontrol

I have usercontrol ctrlinserimento with a textbox and I have another usercontrol ctrlricerca with a botton that should set textbox.text in ctrlinserimento to a string(let's say for example 'Hello').
How can I do?
ctrlinserimento
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class ctrlinserimento : System.Web.UI.UserControl
{
public string textbox1
{
set { TextBox1.Text = value; }
get { return TextBox1.Text; }
}
}
}
ctrlricerca
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
namespace WebApplication3
{
public partial class ctrlricerca: System.Web.UI.UserControl
{
public ctrlinserimento i
{
set; get;
}
protected void Button1_Click(object sender, EventArgs e)
{
i.textbox1 = "Hello";
}
}
}
}
I have NullReferenceException on
i.textbox1 = "Hello";
There are lot of way to do it i.e. page level, User control level. Check following solution that fit for you.
In ctrlricerca user control add property for ctrlinserimento and page level assign instance of ctrlinserimento to property of ctrlricerca
public partial class ctrlricerca: System.Web.UI.UserControl
{
private string nome;
public ctrlinserimento { set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ctrlinserimento.textbox.text="Hello";
}
}
}
}

c# event handling between two forms

I have two forms and I am trying to capture an event generated from frmEventGenerate.cs in frmEventReceive.cs.
In this example I can receive the event from frmEventGenerate.cs but not sure how I can catch this in frmEventReceive.cs? frmEventReceive.cs is my startup form which creates frmEventGenerate.cs.
Can someone point me in the right direction, I think I am being stupid!
Thank you
frmEventGenerate.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Events
{
public delegate void LinkToEventHandler();
public partial class frmEventGenerate : Form
{
public static event LinkToEventHandler Evt;
public frmEventGenerate()
{
InitializeComponent();
Evt += new LinkToEventHandler(ReceiveEvent);
SendEvent();
}
public static void SendEvent()
{
if (Evt != null)
{
Evt();
}
}
public void ReceiveEvent()
{
System.Console.WriteLine("Received Event - This works ok");
}
}
}
frmEventReceive.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Events
{
public partial class frmEventReceive : Form
{
public frmEventReceive()
{
InitializeComponent();
frmEventGenerate frmGen = new frmEventGenerate();
}
public void ReceiveEvent()
{
System.Console.WriteLine("I want to be able to receive the even here!");
}
}
}
In your constructor, after instantiating frmEventGenerate:
frmGen.Evt += ReceiveEvent;
You don't need new LinkEventHandler(...) any more - as of C# 2, there's a method group conversion available which you can use to convert from a method group (the name of a method) to a delegate type.
EDIT: I hadn't seen that your event was static. That suggests you should actually use:
frmEventGenerate.Evt += ReceiveEvent;
... and you don't need the frmGen variable at all.
However, I would strongly discourage you from this - why do you want the event to be static in the first place? (I'd also urge you to name your types more sensibly - something like "EventGenerator" would be better here, for example. Ignoring the convention that type names should be in Pascal case leads to confusing code.)
//Receiver
using System;
using System.Windows.Forms;
namespace eTest
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
public void ReceiveEvent(int i)
{
MessageBox.Show("Event Received from Form: " + i.ToString());
}
private void btnNew_Click(object sender, EventArgs e)
{
int num = 0;
int x = 0;
num = Convert.ToInt32(txtForms.Text);
for (x = 0; x < num; x++)
{
frmDL f = new frmDL();
f.Evt += ReceiveEvent;
f.iID = x;
f.Text = x.ToString();
f.Show();
f.Activate();
Application.DoEvents();
}
}
}
}
//Sender
using System;
using System.Windows.Forms;
namespace eTest
{
public delegate void myEventHandler(int i);
public partial class frmDL : Form
{
public event myEventHandler Evt;
public int iID = 0;
public frmDL()
{
InitializeComponent();
}
public void SendEvent()
{
if (Evt != null)
{
Evt(this.iID);
}
}
private void btnEvent_Click(object sender, EventArgs e)
{
SendEvent();
}
}
}

Categories

Resources