ASP.NET Web Pages

Most important topic when we think about ASP.NET pages is how to retain the information when passing from one page to another. ASP.NET provides us several alternatives to that. Few of them are listed below with VB.Net examples.
Query String:
It’s one of the many choices that ASP.NET gives us to pass the content between the web pages. Its easy and straight forward approach, but with some serious drawbacks which should be kept in mind while using query string to pass sensitive data.
Some issues that you may have are:
·         Firstly, since it’s visible right on your browser so “Big No” when you are sending sensitive information across the pages.
·         Secondly, there is only limited information that you can send using query string.
·         Lastly query string cannot be used to send space and & characters
I will start with the example and then explain some possible ways to work around the query string disadvantages
Create a new web application and add this code to the default.aspx
<form id="form1" runat="server">
    <div>   
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />   
    </div>
</form>
Code Behind
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        Response.Redirect("Default1.aspx?Name=" + Me.txtFirstName.Text + "&LastName=" + Me.txtLastName.Text)
End Sub
Now create another form Default1.aspx and add this code
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
    </div>
   
    </form>
</body>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.txtFirstName.Text = Request.QueryString("Name")
        Me.txtLastName.Text = Request.QueryString("LastName")
End Sub
Run the application. Tadddddaaaa… here you are. You can see your name in carried over to the next web page.


No comments:

Post a Comment