Cross Page Postback - Getting Control
Cross page postbacks are a great feature introduced with ASP.NET 2.0 that allows a developer to specify another page/form to submit the form. This gives us more control over our site and helps us get away from nasty data manangement coding on the backend side. This new feature howerver introduces the need to consistently access the controls from the previous page. I decided to write some wrappers to help with this functionality.
I found that this process is a pretty consistent format and thus can be replicated for any control.
Public Function GetPreviousPageControl(ByVal sPPC As String) As Control
If Not IsNothing(PreviousPage) Then
If Not IsNothing(PreviousPage.Master) Then
Dim content As ContentPlaceHolder
content = PreviousPage.Master.FindControl("ContentPlaceHolder1")
Return content.FindControl(sPPC)
Else
Return PreviousPage.FindControl(sPPC)
End If
End If
Return Nothing
End Function
For us to use this you need to create a method for each type of control you may use. These are examples for a TextBox and a Drop-Down List box. I do have some wrapper methods for some Infragistics controls, so it works with any control that can be found with the FindControl method of the PreviousPage object.
Public Function GetPreviousPageTextBox(ByVal tbName As String) As TextBox
If Not IsNothing(PreviousPage) Then
If Not IsNothing(PreviousPage.Master) Then
Dim content As ContentPlaceHolder
content = PreviousPage.Master.FindControl("ContentPlaceHolder1")
Return content.FindControl(tbName)
Else
Return PreviousPage.FindControl(tbName)
End If
End If
Return Nothing
End Function
Public Function GetPreviousPageDropDownList(ByVal ddlName As String, cph as string = "ContentPlaceHolder1") As DropDownList
If Not IsNothing(PreviousPage) Then
If Not IsNothing(PreviousPage.Master) Then
Dim content As ContentPlaceHolder
content = PreviousPage.Master.FindControl(cph)
Return content.FindControl(ddlName)
Else
Return PreviousPage.FindControl(ddlName)
End If
End If
Return Nothing
End Function
Notice the format of the function, I checkt o see if there is a Master Page defined or not. If there is a Master Page, then you need to grab an instance of the ContentPlaceHolder. You will notice that I have hard coded the id of the ContentHolder, but you might want to make that a variable that can be defined as a parameter in your method definition. I demonstrated this for the DropDownListBox Method.
An important note is that when you get the instance of your control you will want to make sure the control actually exist before using it.
dim tbSample as TextBox = GetPreviousPageTextBox("tbSample")
if not isnothing(tbSample) then
'Process the data as needed.
end if