Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all

In my asp.net application I use one master page for all pages. In some page I want to get a control from the previous page and get a value from it, I did it using this:

C#
rblOperation.SelectedIndex = ((RadioButtonList)Page.PreviousPage.FindControl("rblOperation")).SelectedIndex;


rblOperation is a radio button list exists in all pages. Regardless of what I want to do, the previous statement throws NullReferenceException. Is this the right way to do it ?
Posted

1 solution

Instead of accessing the control from previous page you should store the selection in session variable or URL and read it again on the new page.

Also, you should never use the object without checking for its existence and validity:
RadioButtonList rblOperation = (RadioButtonList)Page.PreviousPage.FindControl("rblOperation")
if (rblOperation != null)
{
// continue working
else
// handle not found condition
}
 
Share this answer
 
Comments
Abdallah Al-Dalleh 9-Sep-14 6:28am    
Btw, I think previous page or the find control always returns null, maybe due to master page, since I'm not accessing the page directly. I'll try the session variable, thanks!
Sinisa Hajnal 9-Sep-14 6:34am    
Accept the solution if it helped. Thank you

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