Click here to Skip to main content
15,884,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a dataList with an image button as template field. and a label holds a name.
I trying to make the button fire up a new page and pass the value of the label to a session.

I tried this code but it keep saying "Object reference not set to an instance of an object".

C#
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    Label lk = (Label)DataList1.FindControl("APP01Label");
    Session["testName"] = lk.Text;
    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Session["testName"].ToString() + "');", true);
}


here I want to test it by showing the Session value in a message box.

thanks
Posted

1 solution

I suspect that the error is happening on this line: Session["testName"] = lk.Text because the previous line may not find a control with the name APP01Label.
I would suggest that you change the code like this: if( lk != null ){ Session["testName"] = lk.Text; }
 
Share this answer
 

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