Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

REQUEST.FORM QUESTION ASP.NET

Hi Im new in ASP.NET , I have problem validating with request.form syntax , it seem that if i send it to another page , all variable gone , I try to catch it using request.form.haskeys ... and return false


this is my code....

TEST.aspx....

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="test.aspx.vb" Inherits="WebApplication1.test"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>test</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
            <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:TextBox id="txtname" style="Z-INDEX: 102; LEFT: 260px; POSITION: absolute; TOP: 65px" runat="server"></asp:TextBox>
                  <asp:TextBox id="txtaddress" style="Z-INDEX: 101; LEFT: 260px; POSITION: absolute; TOP: 92px" runat="server"></asp:TextBox>
                  <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 202px; POSITION: absolute; TOP: 73px" runat="server">Name</asp:Label>
                  <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 201px; POSITION: absolute; TOP: 97px" runat="server">Address</asp:Label>
                  <asp:Button id="Button1" style="Z-INDEX: 105; LEFT: 369px; POSITION: absolute; TOP: 119px" runat="server" Text="Send"></asp:Button>
            </form>
      </body>
</HTML>


TEST.ASPX.VB   (code behind)
Public Class test
    Inherits System.Web.UI.Page
    Protected WithEvents txtaddress As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtname As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("result.aspx")
    End Sub
End Class


Another page for retrieving value........

RESULT.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="result.aspx.vb" Inherits="WebApplication1.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm2</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
            <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 145px; POSITION: absolute; TOP: 72px" runat="server">Your Name</asp:Label>
                  <asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 146px; POSITION: absolute; TOP: 98px" runat="server">Your Address</asp:Label>
                  <asp:Label id="lblname" style="Z-INDEX: 103; LEFT: 254px; POSITION: absolute; TOP: 72px" runat="server"></asp:Label>
                  <asp:Label id="lbladdress" style="Z-INDEX: 104; LEFT: 255px; POSITION: absolute; TOP: 95px" runat="server"></asp:Label>
            </form>
      </body>
</HTML>


Code Behind

Result.aspx.vb


Public Class WebForm2
    Inherits System.Web.UI.Page
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblname As System.Web.UI.WebControls.Label
    Protected WithEvents lbladdress As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblname.Text = Request.Form("txtname")
        lbladdress.Text = Request.Form("txtaddress")
    End Sub


End Class







0
teddysetiawan
Asked:
teddysetiawan
  • 3
1 Solution
 
David H.H.LeeCommented:
teddysetiawan,

-In .NET environment, i'm afraid you can't do it in that way like in classical ASP. ASP.NET is support PostBack features. Why you want to do it in classical ASP method?
But, there still had some others way to get it work.

Work for request.QueryString() method
==============================================
You still can append the value inside the URL and retrieve it using request.queryString() just like what you can done in classical ASP.
eg:
To set the value
--------------------
Response.redirect('process.aspx?txtName=' & txtName.Text)

To get the value[others page]
---------------------------------------
<script runat="server">
Sub Page_Load
  Response.Write(Request.QueryString("txtName"))      
End Sub
</script>

However, you may not want to expose your data to others people inside your querystring and prefer request.Form() method.

Work for request.Form() method - use some javascript method inside asp.net page
===================================================================================

to set the value
------------------------------------
jscriptForm.aspx
-----------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="jscriptForm.aspx.vb" Inherits="JavascriptdotNetForm.jscriptForm"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>Post Data</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
            <script language="javascript">
<!--
function noPostBack(FormAction)
{
    if(document.layers) //support Netscape
    {
        document.layers['Content'].document.forms[0].__VIEWSTATE.name =
                                                           'NOVIEWSTATE';
        document.layers['Content'].document.forms[0].action = FormAction;
    }
    else //support DOM
    {
        document.forms[0].action = FormAction;
        document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
    }
}
-->
            </script>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <div id="Content" runat="server">
                  <form method="post" id="Form1" runat="server">
            <asp:TextBox id="txtName" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
            <asp:Button id="btnSubmit" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 104px" runat="server"
                  Text="Submit"></asp:Button>
                  </form>
            </div>
      </body>
</HTML>

jscriptForm.aspx.vb
=======================================================
Public Class jscriptForm
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Content As System.Web.UI.HtmlControls.HtmlGenericControl
    Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
    Protected WithEvents Submit As System.Web.UI.WebControls.Button
    Protected WithEvents txtName As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        btnSubmit.Attributes.Add("onclick", "noPostBack('process.aspx')")

    End Sub

End Class


To get the value
-----------------------
process.aspx
-------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="process.aspx.vb" Inherits="JavascriptdotNetForm.process"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>process</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:Label id="lblMsg" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 48px" runat="server"></asp:Label>
            </form>
      </body>
</HTML>

process.aspx.vb
----------------
Public Class process
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblMsg As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        lblMsg.Text = Request.Form("txtName") 'get value
    End Sub

End Class

-But, i'm still suggest you to use PostBack features that provided by .net itself. You can post back that same page and get the result before proceed to others page. That's will more easy and appropriate way to solve the problems.

Regards
x_com
0
 
David H.H.LeeCommented:
teddysetiawan,
why Gred B? I'd include explanations and sample coding. It's should be 'A' because i answer your answer completely.
Please Refer http://www.experts-exchange.com/help/closing.jsp#7

Regards
x_com
0
 
teddysetiawanAuthor Commented:
I'm sorry , I do not understand the grading policy here in EE ,
Thanks for the help x_com , but  how can I change the grade  ?
0
 
David H.H.LeeCommented:
teddysetiawan ,
It's ok. welcome to EE as well.
Admin will handle it. Next time, if you had questions about EE, you can always post the helps at CS area, admin will handle all the request from EE members :
http://oldlook.experts-exchange.com/Community_Support

Regards
x_com
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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