Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
someone tell me way to pass the value of button click event to class



C#
public partial class Login : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public void Button1_Click(object sender, EventArgs e)
    {
        String End_Address = TextBox2.Text; 

// i'm passing textbox value into string... in c#


so i want to access the string in class file....

C#
public class harversine {
        Login instance = new Login();
        String valueEnd_Address = instance.End_Address;



is this correct or not?
Posted

You can easily do it using Properties in C#[^].
Declare a Property in harversine class.
C#
private string _End_Address;
public string End_Address
{
    get { return _End_Address; }
    set { _End_Address = value; }
}

Set it from login page. See this:
C#
public void Button1_Click(object sender, EventArgs e)
{
    harversine obj = new harversine();
    obj.End_Address = TextBox2.Text;
}

You can access it in your harversine class:
C#
public void SomeFunction()
{
    if(End_Address != null)
        string EndAdd = End_Address;
}



Refer the below link for more details:
C# Property[^]


--Amit
 
Share this answer
 
v2
Logically its correct but you will not get info as instance will be a differnt object and not the same what is rendered to the client. Instead you can try one of the following options.

1. Pass end_address as a paramater to the constructor to harversine class.
ex: In login code behind
harversine inst = new harversine(TextBox2.Text);

2. Declare a property EndAddress in harversine class and assign the vlaue in code behind click event
ex:
harversine inst = new harversine(); inst.EndAddress =TextBox2.Text;  
 
Share this answer
 
Comments
promod madushan 1-Jul-13 0:02am    
still not working bro!!!!
it says "Login.TextBox2 is a field" but is use like a type....
ArunRajendra 1-Jul-13 0:29am    
Can you post the actual code.
You could do this:

C#
public partial class Login : System.Web.UI.Page
{
    public String End_Address = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public void Button1_Click(object sender, EventArgs e)
    {
        End_Address = TextBox2.Text; 


Then to access it use exactly what you wrote.
 
Share this answer
 
Comments
promod madushan 30-Jun-13 23:46pm    
so then i can now access my End_Address value within the class
Ron Beyer 30-Jun-13 23:47pm    
Yes
promod madushan 1-Jul-13 0:03am    
public class harversine {

String valueEnd_Address = Login.End_Address;
}
Ron is this correct?...........
promod madushan 30-Jun-13 23:54pm    
in my question access value in the class not working still

i think its should be like this?

public class harversine {
//Login instance = new Login();

String valueEnd_Address = Login.End_Address;

am i correct................

so using this i can get value of that textbox using that End_Address string? am i correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900