Solved

Postback Returning Null Value

Posted on 2009-05-15
12
775 Views
Last Modified: 2013-11-08
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
Comment
Question by:ITHelper80
  • 6
  • 5
12 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24397434
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24397455
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24397718
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
 
LVL 6

Author Comment

by:ITHelper80
ID: 24398144
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
 
LVL 27

Expert Comment

by:nmarun
ID: 24398212
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
 
LVL 27

Expert Comment

by:nmarun
ID: 24398228
Sorry the code got lost some how.. even though I had pasted it here. Will reply back soon.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Author Comment

by:ITHelper80
ID: 24398382
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
 
LVL 27

Expert Comment

by:nmarun
ID: 24399316
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
 
LVL 6

Author Comment

by:ITHelper80
ID: 24400591
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
 
LVL 27

Accepted Solution

by:
nmarun earned 500 total points
ID: 24402961
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
 
LVL 27

Expert Comment

by:nmarun
ID: 24402968
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
 
LVL 6

Author Comment

by:ITHelper80
ID: 24404739
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now