Save and retrieve any name in web form using view state

In this blog we will know how to save and retrieve any name in web form using view state.

 

 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:TextBox runat="server" id="Name" />

        <asp:Button runat="server" id="setname" onclick="setname_Click"

            text="Set name" />

        <asp:Button runat="server" id="Retrivename" text="Retrive Name"

            onclick="Retrive_Click" />

        <br /><br />

        Name retrieved from ViewState: <asp:Label runat="server" id="lblname" />

   

    </div>

    </form>

</body>

</html>

 

 

 

Partial Class _Default

    Inherits System.Web.UI.Page

 

    Protected Sub setname_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles setname.Click

        ViewState("UserName") = Name.Text

    End Sub

 

    Protected Sub Retrive_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Retrivename.Click

        If (ViewState("UserName") <> Nothing) Then

            lblname.Text = ViewState("UserName").ToString()

        Else

            lblname.Text = "Notset"

        End If

        Name.Text = ""

    End Sub

End Class

 

Ebook Download
View all
Learn
View all