Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want from the page of Default.aspx's textbox1 value will show in a label of another page Default2.aspx.For this there is a button in Default.aspx page and in the click event of the button I wrote Response.Redirect("Default2.aspx") I have written the following codes under button's click event in Default2.aspx :

C#
protected void Button1_Click(object sender, EventArgs e)
    {
       
        TextBox tb = (TextBox)PreviousPage.FindControl("TextBox1"); if (tb != null)
        {
            Label1.Text = tb.Text;
        }
    }


It arises an error. The error message is : "Object reference not set to an instance of an object" Please help me.
Posted
Updated 29-May-13 22:58pm
v3
Comments
Mahesh Bailwal 30-May-13 3:38am    
are you using Server.Transfer?
Sumon562 30-May-13 4:03am    
I have used response.redirect

http://msdn.microsoft.com/tr-tr/library/6c3yckfw(v=vs.100).aspx[^]

I think you did somethign wrong. At the bottom of this page you can find your ways. Also I recommend you to get values from public properties.
 
Share this answer
 
Hi,

Please try this...

protected void Button1_Click(object sender, EventArgs e)
{

TextBox tb = (TextBox)PreviousPage.FindControl(TextBox1;).text; if (tb != null)
{
Label1.Text = tb.Text;
}
}
 
Share this answer
 
Comments
Sumon562 30-May-13 5:16am    
No it does not work.
hii

so want that the value of textbox in default.aspx to be displayed in label of default2.aspx???

write this on default.aspx
//storing values from textbox
protected void Button1_Click(object sender, EventArgs e)
{
 Response.Redirect("default2.aspx?Name="+txtName.Text);
}



write this on default2.aspx
// calling values to label
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.QueryString["Name"]!= null)
    Label.Text = Request.QueryString["Name"];
}


you can write the second code i.e. default2.aspx in page_load(). it will automatically throw values into the label

It will work :)

Happy coding
 
Share this answer
 
v2
Comments
Swayam231 30-May-13 5:43am    
if there's any error let me know
Sumon562 30-May-13 6:00am    
Thank you for your response. Actually I don't want to use query string. Give me another solution please
Swayam231 30-May-13 6:10am    
u can use session??
Swayam231 30-May-13 6:12am    
or application state or cookies??
Sumon562 30-May-13 6:24am    
I did it using session. You can help me how to use Application state and cookies. Thank you
hi,

you are right in your solution may be the previous page not getting so check below with your oage name '

Login LoginControl = (Login)PreviousPage.FindControl("Login1");
if (LoginControl != null)
{
TextBox UserName = (TextBox)LoginControl.FindControl("UserName");
if (UserName != null)
{
Label1.Text = UserName.Text;
}
}
else
{
Label1.Text = "Cannot find user name in Login control.";
}



happy coding any query please comment.. happy coding

:)


--Napster
 
Share this answer
 
On user's request:

so here's how to use cookies

//storing value of textboxin default.aspx
//in button click event write this
{
HttpCookie sName = new HttpCookie("Name");
sName.Value = txtName.Text; 
Response.Cookies.Add(sName); 
Response.Redirect("default2.aspx");
}


we create a cookie named "sName". Since one cookie instance can hold many values, we tell the compiler that this cookie will hold "Name" value.
here's how to call it in default2.aspx

if (Request.Cookies["Name"] != null )
    Label3.Text = Request.Cookies["Name"].Value;



Application state


// This sets the value of the Application Variable
Application["Name"] = txtName.Text; 
Response.Redirect("Default2.aspx"); 

// This is how we retrieve the value of the Application Variable
if( Application["Name"] != null ) 
    Label3.Text = Application["Name"].ToString();


:)
 
Share this answer
 
Comments
Sumon562 30-May-13 6:44am    
wow Thank you.
Swayam231 30-May-13 6:47am    
you are most welcome.. And you can accept the answer
Sumon562 30-May-13 7:15am    
Now is it possible to solve the same problem using view state? I wish I will get your help.
I have tested above solution of you and these works well.
Swayam231 30-May-13 7:19am    
here use this link it will help for ur view state
http://www.w3schools.com/aspnet/aspnet_viewstate.asp

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