Application of viewState in asp.net

Source: Internet
Author: User

Let's take a look at the online Q &
Private int x;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
X = 1;
}
}
Protected void button#click (object sender, EventArgs e)
{
X ++;
Response. Write (x. ToString ());
}

Here, 1 is output for each call. Why not increase progressively? If I want to define the variables on the current page.

This is because x is only a local variable. In a webpage request, it will be recycled when the webpage is executed. At this time, x will no longer exist, the next access will be a new x variable.
 
In addition to Session objects in traditional Asp, Asp.net provides a better ViewState object. ViewState object is used to save various variables on the page, or even objects. The usage method is similar to HashTable. As long as the variable name is used as an index, such as ViewState ["Var"], the value of the variable Var can be accessed, regardless of whether Var is a common variable or an object, even a able in the memory is too convenient. Why can ViewState be used instead of static variables? The reason is that the server creates a ViewState for each user connected to the page, so ViewState is equivalent to a page-level Session. Now we can safely use ViewState to access the variables and objects to be saved.
 
 
Typical application: bind after Query



Key aspx code

<Tr>
<Td align = "center" style = "padding-bottom: 20px; font-weight: bold; padding-top: 20px">
Department <asp: DropDownList ID = "drpCollege" runat = "server" Height = "22px" Width = "140px">
</Asp: DropDownList>
& Nbsp; Student name <asp: TextBox ID = "txtName" runat = "server"> </asp: TextBox>
& Nbsp; <asp: Button ID = "btnQuery" runat = "server" Text = "query"
Onclick = "btnQuery_Click"/>
</Td>
</Tr>
<Tr>
<Td align = "center" style = "padding-bottom: 20px; padding-top: 20px">
<Br/>

<Asp: GridView ID = "GridView1" runat = "server" DataKeyNames = "Num" AllowSorting = "true"
AutoGenerateColumns = "false" CellPadding = "5" GridLines = "Both" BorderColor = "Black"
Width = "90%">

</Asp>

 

 

DAL key code:

Public IEnumerable <M_Student> ReadStuByCollegeAndName (String collnum, String name)
{
Return from s in dc. M_Student
Where
(! String. IsNullOrEmpty (collnum )? S. CollegeNum. Equals (collnum): true )&&
(! String. IsNullOrEmpty (name )? S. Name. Contains (name): true)
Select s;
}


 

 

Key code of aspx. cs

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using OnlineTest. Datalinq;

Namespace OnlineTest. Manager
{
Public partial class StudentInfoMaintain: BasePage
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Bind ();

.......................................
}
}

Public void BindGridView (String cnum, String name)
{
IEnumerable <M_Student> models = BLL. M_StudentBLL.ReadStuByCollegeAndName (cnum, name );
If (null! = Models & 0! = Models. Count ())
{
// Number of start entries
Int startRecord = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex-1 );

// Per page
Int maxRecords = AspNetPager1.PageSize;

// Total number
This. AspNetPager1.RecordCount = models. Count ();
This. AspNetPager1.AlwaysShow = true;
This. GridView1.DataSource = models. Skip (startRecord). Take (maxRecords );
This. GridView1.DataBind ();
}
Else
{
This. GridView1.DataSource = null;
This. GridView1.DataBind ();
}
}

 

Public void Bind ()
{
If (null = ViewState ["num"])
{
ViewState ["num"] = "";
}

If (null = ViewState ["name"])
{
ViewState ["name"] = "";
}
BindGridView (ViewState ["num"]. ToString (), ViewState ["name"]. ToString ());
}

 

// Paging events
Protected void AspNetPager1_PageChanging (object src, Wuqi. Webdiyer. PageChangingEventArgs e)
{
This. AspNetPager1.CurrentPageIndex = e. NewPageIndex;
Bind ();
}

 

/// <Summary>
/// Query
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnQuery_Click (object sender, EventArgs e)
{
If (! This. drpCollege. SelectedValue. Equals ("0 "))
{
ViewState ["num"] = this. drpCollege. SelectedValue;
}
If (! String.IsNullOrEmpty(this.txt Name. Text ))
{
ViewState ["name"] = this.txt Name. Text;
}
Bind ();
}
}
}

 

From Xu Yue's column
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.