Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

Postback Returning Null Value

I have two pages (default and submitted.aspx) default has a postback to submitted to do some error checking and submitted has a post back to default.

The post back from default to submitted works fine however when I try to post back from submitted back to default it is returning the "Previous Page" as Null.

Has anyone had this happen before?
0
ITHelper80
Asked:
ITHelper80
  • 6
  • 5
1 Solution
 
nmarunCommented:
0
 
ITHelper80Author Commented:
Postback URL for Default.ASPX
<asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="~/Submitted Page.aspx"/>

For Submitted Page
<asp:Button ID="btnBack" runat="server" PostBackUrl="~/Default.aspx"
                      Text="Back To Form" />

Page Reference for Default
<%@ PreviousPageType VirtualPath="~/Submitted Page.aspx"%>
 
Code Behind:
        If PreviousPage IsNot Nothing Then
            If PreviousPage.IsCrossPagePostBack = True Then
 
                txtEmpName.Text = PreviousPage.Empnum
                txtWork.Text = PreviousPage.Workcenter
                txtDept.Text = PreviousPage.Dept
                txtIncident.Text = PreviousPage.Details
                txtWitness.Text = PreviousPage.Witness
                txtCause.Text = PreviousPage.Cause
                txtInjury.Text = PreviousPage.Description
                txtDoing.Text = PreviousPage.Duty
                txtPPE.Text = PreviousPage.PPE
                txtTools.Text = PreviousPage.Tools
                txtEffort.Text = PreviousPage.Hours
                tblReturn.Visible = True
            Else
                Response.Write("EmptyPostback")
            End If
        Else
            Response.Write("Empty")
        End If
 
Code Behind for Submitted:
 
 If (PreviousPage IsNot Nothing) AndAlso (PreviousPage.IsCrossPagePostBack) Then
 
            Dim PrevPage As Page = PreviousPage
 
            Dim Type As RadioButtonList = DirectCast(PrevPage.FindControl("rbType"), RadioButtonList)
            Dim email As TextBox = DirectCast(PrevPage.FindControl("txtsEmail"), TextBox)
            Dim email1 As TextBox = DirectCast(PrevPage.FindControl("txtSupEmail"), TextBox)
            Dim ATSemail As TextBox = DirectCast(PrevPage.FindControl("txtATSEmail"), TextBox)
            Dim EmpNum As TextBox = DirectCast(PrevPage.FindControl("txtEmpName"), TextBox)
            Dim EmpName As DropDownList = DirectCast(PrevPage.FindControl("ddEmpName"), DropDownList)
            Dim SupName As TextBox = DirectCast(PrevPage.FindControl("txtSupName"), TextBox)
            Dim WorkCenter As TextBox = DirectCast(PrevPage.FindControl("txtWork"), TextBox)
            Dim Dept As TextBox = DirectCast(PrevPage.FindControl("txtDept"), TextBox)
            Dim Details As TextBox = DirectCast(PrevPage.FindControl("txtIncident"), TextBox)
            Dim Witness As TextBox = DirectCast(PrevPage.FindControl("txtWitness"), TextBox)
            Dim Cause As TextBox = DirectCast(PrevPage.FindControl("txtCause"), TextBox)
            Dim Day As TextBox = DirectCast(PrevPage.FindControl("txtIncidDate"), TextBox)
            Dim Time As TextBox = DirectCast(PrevPage.FindControl("txtTime1"), TextBox)
            Dim Time1 As TextBox = DirectCast(PrevPage.FindControl("txtTime2"), TextBox)
            Dim Time2 As DropDownList = DirectCast(PrevPage.FindControl("ddTime"), DropDownList)
            Dim Time3 As TextBox = DirectCast(PrevPage.FindControl("txtTime3"), TextBox)
            Dim Time4 As TextBox = DirectCast(PrevPage.FindControl("txtTime4"), TextBox)
            Dim Time5 As DropDownList = DirectCast(PrevPage.FindControl("ddTime0"), DropDownList)
            Dim Injury As TextBox = DirectCast(PrevPage.FindControl("txtInjury"), TextBox)
            Dim Doing As TextBox = DirectCast(PrevPage.FindControl("txtDoing"), TextBox)
            Dim PPE As TextBox = DirectCast(PrevPage.FindControl("txtPPE"), TextBox)
            Dim Tools As TextBox = DirectCast(PrevPage.FindControl("txtTools"), TextBox)
            Dim Cost As DropDownList = DirectCast(PrevPage.FindControl("ddCost"), DropDownList)
            Dim Hours As TextBox = DirectCast(PrevPage.FindControl("txtEffort"), TextBox)
            Dim JSA As RadioButtonList = DirectCast(PrevPage.FindControl("rbJSA"), RadioButtonList)
            Dim Root1 As DropDownList = DirectCast(PrevPage.FindControl("ddRoot1"), DropDownList)
            Dim Root2 As DropDownList = DirectCast(PrevPage.FindControl("ddRoot2"), DropDownList)
 
            '## Shows values from Safety Form Entry Page ##
            lblEmpnum.Text = EmpNum.Text
            lblEmpName.Text = EmpName.SelectedValue
            lblSupName.Text = SupName.Text
            lblWorkCenter.Text = WorkCenter.Text
            lblDept.Text = Dept.Text
            lblType.Text = Type.SelectedValue
            lblDetails.Text = Details.Text
            lblwitness.Text = Witness.Text
            lblCause.Text = Cause.Text
            lblDate.Text = Day.Text
            lblTime0.Text = Time.Text & ":" & Time1.Text & " " & Time2.SelectedValue
            lblTime.Text = Time3.Text & ":" & Time4.Text & " " & Time5.SelectedValue
            lblDescription.Text = Injury.Text
            lblDuty.Text = Doing.Text
            lblPPE.Text = PPE.Text
            lblEquip.Text = Tools.Text
 
            If Type.SelectedValue = "Property Damage" Then
                tdCost.Visible = True
                trCost.Visible = True
                lblDamage.Text = Cost.SelectedValue
            Else
                tdCost.Visible = False
                trCost.Visible = False
            End If
 
            lblHours.Text = Hours.Text
            lblJsa.Text = JSA.SelectedValue
            lblPrimRoot.Text = Root1.SelectedValue
            lblSecRoot.Text = Root2.SelectedValue
 
        Else
            Response.Redirect("~/Default.aspx")
        End If
 
    End Sub

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ITHelper80Author Commented:
i just discovered something else.

I can get the Submitted to Postback to the Default page but I have to remove the postback references where the submitted page would recieve values from the Default page.

So in short I can postback from Default to Submitted or Submitted to Default but I cannot perform the postback from Default to Submitted back to Default.

0
 
nmarunCommented:
I got it to work. Here's the flow of the pages:

User enters first and last names and presses the button on the Default page. Submitted page is loaded showing the first and the last names on the controls. User enters age in the third text field and clicks the button and the default page is loaded showing the age in the label.
0
 
nmarunCommented:
Sorry the code got lost some how.. even though I had pasted it here. Will reply back soon.
0
 
ITHelper80Author Commented:
Thanks nmarun, I hope your code will work Ive been beating my head against a wall for 2 days.

There is one slight difference with how my app words.

User enters information on Default page, clicks submit and all controls are sent to Submitted page. In the submitted page some error checking is done based on which field did/didnt come over from Default page however all values from Default as place into labels, not textboxs. The submitted page is really a confirmation page unless there is an error then they can click on "back to form" button and eidt their entry and start the process all over again. Hope that makes sense.
0
 
nmarunCommented:
I guess I got your issue and I'll word it so we're on the same page before we continue:

User enters information, clicks on the button
Some validation / error checking needs to happen
If things work out fine, show the submitted page
If not, show errors on default page

If the above is right, then you are better off doing the validation on default page itself and then redirect to submitted page only if the validation succeeds. Let me know if that'll help.
0
 
ITHelper80Author Commented:
Not exactly.
User clicks submit, all information is sent to Submitted.aspx page, error checking is performed. If there is an error a  an error message and "Return to form" button becomes viisble , when the user clicks that all the values are returned to the Default page and repopulated in the text boxes, etc. If there is not error all of the values are shown on the submitted page  with success message as confirmation.

I thought about doing the validation on the Default page but I dont know how to prevent the Postback URL command from activating since its on the submit button.
0
 
nmarunCommented:
Here's the working model that I got:


// default.aspx
<asp:TextBox ID="FirstNameTextBox" runat="server" />
<br />
<asp:TextBox ID="LastNameTextBox" runat="server" />
<br />
<asp:Label ID="AgeLabel" runat="server" />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="~/Submitted.aspx"/>
<br />
 
// submitted.aspx
<asp:TextBox ID="FirstNameTextBox" runat="server" />
<br />
<asp:TextBox ID="LastNameTextBox" runat="server" />
<br />
<asp:TextBox ID="AgeTextBox" runat="server" />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="~/Default.aspx"/>
 
// default.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If PreviousPage IsNot Nothing Then
        If PreviousPage.IsCrossPagePostBack Then
            AgeLabel.Text = DirectCast(PreviousPage.FindControl("AgeTextBox"), TextBox).Text
        End If
    End If
End Sub
 
// submitted.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
        FirstNameTextBox.Text = DirectCast(PreviousPage.FindControl("FirstNameTextBox"), TextBox).Text
        LastNameTextBox.Text = DirectCast(PreviousPage.FindControl("LastNameTextBox"), TextBox).Text
    End If
End Sub

Open in new window

0
 
nmarunCommented:
If you want to do the error checking in default.aspx this is what you need to do:

<asp:Button ID="btnSubmit" runat="server" Text="Submit"  onclick="btnSubmit_Click"/>

// default.aspx.vb

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' do error checking here
    If isValid Then
        Response.Redirect("~/Submitted.aspx")
    End If
End Sub


0
 
ITHelper80Author Commented:
Thanks nmarun, your code works but I had to make some changes.

The root cause of my problem was the  
Else
            Response.Redirect("~/Default.aspx")
        End If
Statement on the submitted page, since when the button on the page was performing a postback  it kept redirecting to the default page.

Once I added IfNot Page.IsPostback on the page load it all started working..
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now