This article shows how to bind data in ListView controls with using ViewState variable and also shows paging on controls on linkbutton click. We will learn here how to bind controls with data with ViewState and how to handle paging on controls. This binds data according to link button text on link button click event.

Also read

Initial Chamber

Step 1: Open your Visual Studio and create an empty website then provide a suitable name such as ListViewUsingViewState.

Step 2: In Solution Explorer you will get your empty website, then add some web forms.

ListViewUsingViewState (your empty website). Right click and select Add New Item Web Form. Name it ListViewUsingViewState.aspx.

Design Chamber

Step 3: Open the ListViewUsingViewState.aspx file and write some code for the design of the application.

When you open this page you can see the following code-

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListViewUsingViewState.aspx.cs" Inherits="ListViewUsingViewState" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title> </title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. </div>
  11. </form>
  12. </body>
  13. </html>
  14. Now add control on page then you get below code-
  15. <div>
  16. <%-- Here we add data to bind in ViewState --%>
  17. <div>
  18. <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
  19. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  20. <asp:Label ID="Label2" runat="server" Text="Designation"></asp:Label>
  21. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  22. <asp:Button ID="Button1" runat="server" Text="save" OnClick="InsertData" />
  23. </div><br />
  24. <%-- end --%>
  25. <%-- listview check --%>
  26. <div>
  27. <asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
  28. <LayoutTemplate>
  29. <table border="1">
  30. <tr>
  31. <th>Name
  32. </th>
  33. <th>Designation
  34. </th>
  35. </tr>
  36. <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
  37. <tr>
  38. <td colspan="3">
  39. <%-- This part used for paging --%>
  40. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="2">
  41. <Fields>
  42. <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
  43. ShowNextPageButton="false" />
  44. <asp:NumericPagerField ButtonType="Link" />
  45. <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
  46. </Fields>
  47. </asp:DataPager>
  48. <%-- end --%>
  49. </td>
  50. </tr>
  51. </LayoutTemplate>
  52. <GroupTemplate>
  53. <tr>
  54. <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
  55. </tr>
  56. </GroupTemplate>
  57. <ItemTemplate>
  58. <td>
  59. <%# Eval("Name") %>
  60. </td>
  61. <td>
  62. <%# Eval("Designation") %>
  63. </td>
  64. </ItemTemplate>
  65. </asp:ListView>
  66. </div>
  67. <%-- end --%>
  68. </div>
In above code I have add one listview control and its attribute and I’ve also add two textbox and one button control to add data in ViewState to bind with ListView control.

Here I have designed the ListView Control and used some property as in the following:

Now design page looks like the following:

Design Page-
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListViewUsingViewState.aspx.cs" Inherits="ListViewUsingViewState" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>List View With View State - Upendra Pratap Shahi</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <%-- Here we add data to bind in ViewState --%>
  11. <div>
  12. <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
  13. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  14. <asp:Label ID="Label2" runat="server" Text="Designation"></asp:Label>
  15. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  16. <asp:Button ID="Button1" runat="server" Text="save" OnClick="InsertData" />
  17. </div><br />
  18. <%-- end --%>
  19. <%-- listview check --%>
  20. <div>
  21. <asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
  22. <LayoutTemplate>
  23. <table border="1">
  24. <tr>
  25. <th>Name
  26. </th>
  27. <th>Designation
  28. </th>
  29. </tr>
  30. <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
  31. <tr>
  32. <td colspan="3">
  33. <%-- This part used for paging --%>
  34. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="2">
  35. <Fields>
  36. <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
  37. ShowNextPageButton="false" />
  38. <asp:NumericPagerField ButtonType="Link" />
  39. <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
  40. </Fields>
  41. </asp:DataPager>
  42. <%-- end --%>
  43. </td>
  44. </tr>
  45. </LayoutTemplate>
  46. <GroupTemplate>
  47. <tr>
  48. <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
  49. </tr>
  50. </GroupTemplate>
  51. <ItemTemplate>
  52. <td>
  53. <%# Eval("Name") %>
  54. </td>
  55. <td>
  56. <%# Eval("Designation") %>
  57. </td>
  58. </ItemTemplate>
  59. </asp:ListView>
  60. </div>
  61. <%-- end --%>
  62. </div>
  63. </form>
  64. </body>
  65. </html>
Your design looks like the following figure:

design
Figure 1

Code chamber

Step 4 In the code chamber we will write some code so that our application works.

Adding the following namespaces on namespace section of your code behind page-
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
Now your page looks like –

Code behind Page-
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. public partial class ListViewUsingViewState : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. DataTable dt = new DataTable();
  15. dt.Columns.Add("Name", typeof(string));
  16. dt.Columns.Add("Designation", typeof(string));
  17. ViewState["DataEntry"] = dt;
  18. BindListView();
  19. }
  20. }
  21. private void BindListView()
  22. {
  23. DataTable dt = new DataTable();
  24. dt = (DataTable)ViewState["DataEntry"];
  25. if (dt.Rows.Count > 0 && dt != null)
  26. {
  27. ListView1.DataSource = dt;
  28. ListView1.DataBind();
  29. }
  30. else
  31. {
  32. ListView1.DataSource = null;
  33. ListView1.DataBind();
  34. }
  35. }
  36. /// <summary>
  37. /// This function is used to save the data in ViewState
  38. /// </summary>
  39. /// <param name="sender">sender</param>
  40. /// <param name="e">e</param>
  41. protected void InsertData(object sender, EventArgs e)
  42. {
  43. string sName = "";
  44. string sDesg = "";
  45. //here taking data from textbox
  46. if (TextBox1.Text.ToString() == "")
  47. {
  48. ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Enter the name')", true);
  49. return;
  50. }
  51. else
  52. {
  53. sName = TextBox1.Text.ToString();
  54. }
  55. if (TextBox2.Text.ToString() == "")
  56. {
  57. ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Enter the designation')", true);
  58. return;
  59. }
  60. else
  61. {
  62. sDesg = TextBox2.Text.ToString();
  63. }
  64. DataTable dt = new DataTable();
  65. DataRow dr;
  66. //assigning ViewState value in Data Table
  67. dt = (DataTable)ViewState["DataEntry"];
  68. //Adding value in datatable
  69. dr = dt.NewRow();
  70. dr["Name"] = sName;
  71. dr["Designation"] = sDesg;
  72. dt.Rows.Add(dr);
  73. if (dt != null)
  74. {
  75. ViewState["DataEntry"] = dt;
  76. }
  77. //here bind Listview after data save in ViewState
  78. this.BindListView();
  79. //empty the textbox control
  80. TextBox1.Text = string.Empty;
  81. TextBox2.Text = string.Empty;
  82. }
  83. //this is used for paging on ListView
  84. protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
  85. {
  86. (ListView1.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
  87. this.BindListView();
  88. }
  89. }
Output

On initial load-

On initial load
Figure 2

On blank data

On blank data
Figure 3

message
Figure 4

Enter data

Enter data
Figure 5

On first entry

On first entry
Figure 6

On Second Entry

On Second Entry
Figure 7

On third entry paging shows

On third entry paging shows
Figure 8

Now click on paging and see the following output.

click on paging
Figure 9

I hope you liked this. Have a good day. Thank you for reading this article.