My ASP.Net Page doesn't post form data to another second page

René 21 Reputation points
2022-07-08T12:49:04.577+00:00
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Registration.aspx.vb" Inherits="Test.Registration" %>  
  
<!DOCTYPE html>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
    <title></title>  
  
  <!-- Google Fonts -->  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Roboto:300,300i,400,400i,500,500i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">  
  
  <!-- Vendor CSS Files -->  
  <link href="assets/vendor/aos/aos.css" rel="stylesheet" />  
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet" />  
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet" />  
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet" />  
  <link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet" />  
  <link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet" />  
  
  <!-- Template Main CSS File -->  
  <link href="assets/css/style.css" rel="stylesheet" />  
                            <script>  
                                function check() {  
                                    //For dem Absenden des Formulars sollen einige Variablen  
                                    //überprüft werden.  
                                    //Zuerst herausfinden, ob ein Textfeld leer ist.  
                                    var x;  
                                    x = 0;  
                                    var f = document.getElementById("frmRegistration")  
                                    for (x; x < f.length; x++) {  
                                        if (f[x].name == 'txtAnzahlLizenzen') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie die Anzahl Lizenzen an.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'cmbEdition') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie die gewünschte Edition an.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'txtAnzahlLizenzen') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie die gewünschte Anzahl Lizenzen an.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'txtVorname') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie Ihren Vornamen ein.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'txtNachname') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie Ihren Nachnamen ein.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'txtStrasse') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie Ihre Strasse ein.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'txtPLZ') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie die PLZ ein.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'txtOrt') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie Ihren Ort ein.');  
                                                return (false);  
                                            }  
                                        }  
  
                                        if (f[x].name == 'txtMail') {  
                                            if (f[x].value == '') {  
                                                alert('Bitte geben Sie Ihre Mailaadresse ein.');  
                                                return (false);  
                                            }  
                                            //Jetzt wird die EMail-Adresse nach Leerzeichen durchsucht.  
                                            //-1 bedeutet, dass kein Leerzeichen gefunden wurde.  
                                            if (f[x].value.search(/ /) != '-1') {  
                                                alert('Die E-Mail Adresse darf keine Leerzeichen enthalten!');  
                                                return (false);  
                                            }  
                                            //Am Schluss soll noch einmal das Format ueberprüft werden.  
                                            s = f[x].value.split(/@/);  
                                            if ((s.length != 2) || s[0] == '' || s[1] == '') {  
                                                alert('E-Mail Adressen haben das Format "Benutzer@Domain",\n'  
                                                    + 'z.B.: "Peter.Mustermann@provider.ch"');  
                                                return (false);  
                                            }  
                                        }  
                                    }  
                                }  
                            </script>  
</head>  
<body>  
    <form id="frmRegistration" runat="server" method="post" enctype="multipart/form-data" onsubmit="return check();">  
        <main id="main">  
            <!-- ======= News Section ======= -->  
            <section id="programmdetails" class="news section-bg">   
              <div class="container" data-aos="fade-up">  
  
                    <div class="section-title">   
                        <h3>PW-Soft, die ultimative Fahrzeugverwaltung <br /><span>Registrierung- und Lizenzierung</span></h3>  
                        <p>   
                        Bitte tragen Sie Ihre Daten mindestens in den mit Sternchen markierten Pflichtfelder ein, damit wir Ihre Bestellung bearbeiten können.  
                        </p>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                     <div class="col-lg-6 col-md-5">  
                                <asp:Label ID="lblKey" runat="server" Text="Registrations-Schlüssel"></asp:Label>  
                        </div>  
                        <div class="col-lg-5 col-md-5">  
                                <asp:TextBox ID="txtKey" Enabled="false" runat="server"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblVersion" runat="server" Text="Version" style="margin-right:10px;"></asp:Label>  
                        </div>  
                        <div class="col-lg-2 col-md-2">  
                            <asp:TextBox ID="txtVersion" Enabled="true" runat="server" Width="100px"></asp:TextBox>  
                        </div>  
                        <div class="col-lg-2 col-md-2">  
                            <asp:Label ID="lblEdition" runat="server" Text="Edition" style="margin-right:10px;"></asp:Label>  
                            <asp:Label ID="lblRequired1" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-2 col-md-2">  
                            <asp:DropDownList ID="cmbEdition" runat="server">  
                                <asp:ListItem Selected="True"></asp:ListItem>  
                                <asp:ListItem Value="Standard"></asp:ListItem>  
                                <asp:ListItem Value="Professional"></asp:ListItem>  
                                <asp:ListItem Value="Enterprise"></asp:ListItem>  
                                <asp:ListItem Value="Full"></asp:ListItem>  
                            </asp:DropDownList>  
  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblAnzLizenzen" runat="server" Text="Anzahl Lizenzen"></asp:Label>  
                            <asp:Label ID="lblRequired2" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:TextBox ID="txtAnzahlLizenzen" Enabled="true" runat="server" Width="50px"></asp:TextBox>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblUpgradeVon" runat="server" Text="Upgrade von" style="margin-right:10px;"></asp:Label>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:TextBox ID="txtUpgradeVon" Enabled="true" runat="server" Width="100px"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblGutscheincode" runat="server" Text="Gutscheincode" style="margin-right:10px;"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                            <asp:TextBox ID="txtGutscheincode" Enabled="true" runat="server" Width="150px"></asp:TextBox>  
                        </div>  
                    </div>  
              </div>  
            </section>               
              
            <!-- ======= News Section ======= -->  
            <section id="Userdaten" class="news section-bg">   
                <div class="container" data-aos="fade-up">  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblFirma" runat="server" Text="Firma"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                            <asp:TextBox ID="txtFirma" Enabled="true" runat="server" Width="250px"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblAnrede" runat="server" Text="Anrede"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                            <asp:DropDownList ID="cmbAnrede" runat="server">  
                                <asp:ListItem Selected="True"></asp:ListItem>  
                                <asp:ListItem Value="Herr"></asp:ListItem>  
                                <asp:ListItem Value="Frau"></asp:ListItem>  
                                <asp:ListItem Value="Firma"></asp:ListItem>  
                            </asp:DropDownList>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblVorname" runat="server" Text="Vorname"></asp:Label>  
                            <asp:Label ID="lblRequired3" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                            <asp:TextBox ID="txtVorname" Enabled="true" runat="server" Width="150px"></asp:TextBox>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblNachname" runat="server" Text="Nachname"></asp:Label>  
                            <asp:Label ID="lblRequired4" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:TextBox ID="txtNachname" Enabled="true" runat="server" Width="150px"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblStrasse" runat="server" Text="Strasse"></asp:Label>  
                            <asp:Label ID="lblRequired5" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:TextBox ID="txtStrasse" Enabled="true" runat="server" Width="350px"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblPLZ" runat="server" Text="PLZ"></asp:Label>  
                            <asp:Label ID="lblRequired6" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                            <asp:TextBox ID="txtPLZ" Enabled="true" runat="server" Width="70px"></asp:TextBox>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblOrt" runat="server" Text="Ort"></asp:Label>  
                             <asp:Label ID="lblRequired7" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:TextBox ID="txtOrt" Enabled="true" runat="server" Width="250px"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblLand" runat="server" Text="Land"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                                <asp:DropDownList ID="cmbLand" runat="server">  
                                <asp:ListItem Selected="True"></asp:ListItem>  
                                <asp:ListItem Value="Schweiz"></asp:ListItem>  
                                <asp:ListItem Value="Deutschland"></asp:ListItem>  
                                <asp:ListItem Value="Österreich"></asp:ListItem>  
                                <asp:ListItem Value="Frankreich"></asp:ListItem>  
                                <asp:ListItem Value="Belgien"></asp:ListItem>  
                                <asp:ListItem Value="Niederlande"></asp:ListItem>  
                                <asp:ListItem Value="Dänemark"></asp:ListItem>  
                                <asp:ListItem Value="Schweden"></asp:ListItem>  
                                <asp:ListItem Value="Norwegen"></asp:ListItem>  
                            </asp:DropDownList>  
                        </div>  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblTelefon" runat="server" Text="Telefon"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                            <asp:TextBox ID="txtTelefon" Enabled="true" runat="server" Width="150px"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblMail" runat="server" Text="E-Mail"></asp:Label>  
                            <asp:Label ID="lblRequired8" runat="server" Text="*" style="color:red;"></asp:Label>  
                        </div>  
                        <div class="col-lg-3 col-md-5">  
                            <asp:TextBox ID="txtMail" Enabled="true" runat="server" Width="350px"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
                            <asp:Label ID="lblKommentar" runat="server" Text="Kommentar"></asp:Label>  
                        </div>  
  
                        <div class="col-lg-2 col-md-5">  
                            <asp:TextBox ID="txtKommentar" Enabled="true" runat="server" Width="350px" TextMode="MultiLine" Rows="10" style="min-height:100px;"></asp:TextBox>  
                        </div>  
                    </div>  
                    <div class="row" style="margin-bottom:20px;">  
                        <div class="col-lg-2 col-md-5">  
  
                            <asp:Button ID="cmdBestellen" runat="server" Text="Bestellung speichern" PostBackUrl="Zahlung.aspx" />  
  
                        </div>  
                    </div>  
                </div>  
            </section>  
        </main>  
    </form>  
      <!-- Vendor JS Files -->  
  <script src="assets/vendor/aos/aos.js"></script>  
  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>  
  <script src="assets/vendor/glightbox/js/glightbox.min.js"></script>  
  <script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>  
  <script src="assets/vendor/purecounter/purecounter.js"></script>  
  <script src="assets/vendor/swiper/swiper-bundle.min.js"></script>  
  <script src="assets/vendor/waypoints/noframework.waypoints.js"></script>  
  
  <!-- Template Main JS File -->  
  <script src="assets/js/main.js"></script>  
  <script src="assets/js/jquery.min.js"></script>  
</body>  
</html>  

The Page is under this URL avalaible
Registration.aspx

The CodeBehind Code from the second page fires, but have nothing in the Response.Form Object.

Public Class Zahlung
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
    Dim Key As String = Request.Form("txtKey")  
    Response.Write(Key)  
End Sub  

End Class

Whats the problem of postbackurl with some CSS Styesheets? In Zahlung.aspx there is nothing in the Request.Form.

Thanks a lot for your help or any suggestions

Greetings René

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,287 questions
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,071 Reputation points
    2022-07-11T05:55:34.693+00:00

    Hi @René ,
    You find the actual path when you are using friendly url. So, I suggest you could use Server.Transfer. You could try to test use this demo.

    Page1,named Test2:

                <asp:Label ID="Label1" runat="server" Text="Company Name"></asp:Label>  
                <asp:TextBox ID="txtkey" runat="server"></asp:TextBox>  
                <asp:Button ID="cmdspichern" runat="server" Text="spichern" O nClick="cmdspichern_Click" />  
    

    Code-behind:

     Protected Sub cmdspichern_C lick(sender As Object, e As EventArgs)  
            S erver.Transfer("~/Test3.aspx")  
        End Sub  
    

    Page2,named Test3:

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
    

    Code-behind:

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
            If Not IsPostBack Then  
                Dim txtBoxtest As TextBox = CType(Page.PreviousPage.FindControl("txtKey"), TextBox)  
                TextBox1.Text = txtBoxtest.Text  
            End If  
        End Sub  
    

    You could check the microsoft article.

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Michael Taylor 48,826 Reputation points
    2022-07-08T14:16:26.78+00:00

    In general that isn't going to work properly. Cross page posting is an advanced use case. There are lots of things at play here but things that don't normally work right include anything stored in viewstate (because the new page doesn't have access to it). The recommended approach for cross page posting is to use the Page.PreviousPage property to get the previous page with all the controls (that were posted) filled in. You can read about it here. This helps resolve issues with viewstate I believe but honestly it has been a really long time since I've needed a cross page post.

       //New page  
       void HandlePost ()  
       {  
          var page = PreviousPage as Registration;  
          if (page != null)  
          {  
             //Get a value from the other page  
             string txtOrt = page.txtOrt.Text;  
          };  
       }  
    
    0 comments No comments

  2. Albert Kallal 4,806 Reputation points
    2022-07-09T00:40:58.39+00:00

    Ok, a lot of markup. The ONLY part we care is HOW we jump to the 2nd page.

    If you use code behind for that button click - then previoius page is null and void.

    So, say we have this markup - and good to see you used postbackURL.

    So, assuming this markup:

    WebFrom1.aspx

    219057-image.png

    So, now on the target page, we want to get that value in txtName.

    So, you MUST use the !IsPostBack event, and ONLY on first page load can you get previous values.

    So, you have ONE try and grab at this.

    So, the page load code on the 2nd target page, (WebForm2.aspx) can look like this:

    markup:

    218999-image.png

    And code behind can be this:

            protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!IsPostBack)  
                {  
                    // you MUST use the first page post-back - after that, previous page is null and void  
      
                    // get value of text box txtName  
      
                    TextBox txtName = Page.PreviousPage.FindControl("txtName") as TextBox;  
                    TextBox1.Text = txtName.Text;  
                }  
            }  
      
    

    So, as a general rule:

    You have ONE try + grab - first page load ONLY - use !IsPostBack check as per above.

    You can thus use find control for the previous page.

    so, on first page, I have this:

    219085-image.png

    We enter a value, click on button, and now on 2nd page, we get/see this;

    219058-image.png

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

  3. René 21 Reputation points
    2022-07-09T08:43:40.78+00:00

    Dear Albert Kallal and cooldadtx

    Thanks for your examples

    I'm used this code
    219141-code.jpg

    The error in English
    The object reference was not set to an object instance.

    When I view the Previuspage object in the Object Explorer, the object is practically empty.

    219131-code-2.jpg

    0 comments No comments

  4. René 21 Reputation points
    2022-07-09T11:21:14.647+00:00

    Dear all

    I have used the following code.
    This failure translated to english
    The object reference was not set to an object instance

    How can it be achieved that the Form Object has content?
    Thanks for sugestions
    René
    219019-code.jpg