Search code examples
c#asp.netviewstate

ViewState is not working


i am using viewState to bind data to list box.when i add click on add stop button listbox does not populate anything. what i am doing here. Help me out please. viewstate is not working. i had try googling my problem i didn't find and suitable solution. Anybody had any idea about my problem hlep me out please.

 <%@ Page Title="Register Stop" Language="C#" MasterPageFile="~/TransportManager.Master" ViewStateMode="Enabled" AutoEventWireup="true" CodeBehind="RegisterStop.aspx.cs" Inherits="TransportManagementSystemFYP.RegisterStop" %>

<div class="contact w3l-2">
    <div class="container">
        <h2 class="w3ls_head">Stop <span>Registration</span></h2>
            <div class="contact-grids">
                <div class="col-md-6 contact-grid agileinfo-5">                     
                    <label>Select Route</label>
                    <asp:DropDownList ID="RouteDropDown" CssClass="form-control" runat="server">
                        <asp:ListItem Text="Wapda town" Value="1"></asp:ListItem>
                    </asp:DropDownList>
                    <label>Stop ID</label>
                    <asp:TextBox ID="StopID" placeholder="Stop ID..." required="" runat="server"></asp:TextBox>
                    <label>Stop Name</label>
                    <asp:TextBox ID="StopName" placeholder="Stop name..." required="" runat="server"></asp:TextBox>
                    <label>Stop Location</label>
                    <asp:TextBox ID="StopLocation" placeholder="Stop location..." required="" runat="server"></asp:TextBox>                      
                    <asp:Button ID="AddStopToList" type="submit" runat="server" Text="Add Stop" OnClick="AddStopToList_Click" />

                </div>
            <div class="col-lg-6 contact-grid agileinfo-5">
                <label>Stops List</label>
                <asp:ListBox ID="StopListBox" CssClass="form-control" ViewStateMode="Enabled" runat="server"></asp:ListBox>
                <asp:Button ID="StopRegistration" type="submit" runat="server" Text="Register Stop" />
            </div>
                    <div class="clearfix"></div>
            </div>
    </div>
</div>

Here is the code behind file

    public partial class RegisterStop : System.Web.UI.Page
{

    DataTable DT = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["Stop"] == null)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("name");
                dt.Columns.Add("location");
                dt.Columns.Add("routeId");
                ViewState["Stop"] = dt;
            }
        }
    }
    protected void AddStopToList_Click(object sender, EventArgs e)
    {
        DT =(DataTable)ViewState["Stop"];
        String id = StopID.Text;
        String name = StopName.Text;
        String location = StopLocation.Text;
        String Rid = RouteDropDown.SelectedItem.Value.ToString().Trim();

        StopListBox.DataSource = DT;
        StopListBox.DataTextField = "id";
        StopListBox.DataValueField = "id";
        StopListBox.DataBind();

        StopID.Text = "";
        StopName.Text = "";
        StopLocation.Text = "";


    }
}

Solution

  • It is just all about that you forgot to add your item to your datatable.

    At the end please dont forget to register viewState = DT again. Otherwise, your listbox will contain only 1 item even if you add new items.

    You should edit your code as below:

            DT = (DataTable)ViewState["Stop"];
            String id = StopID.Text;
            String name = StopName.Text;
            String location = StopLocation.Text;
            String Rid = RouteDropDown.SelectedItem.Value.ToString().Trim();
    
            DataRow row = DT.NewRow();
            row["id"] = id;
            row["name"] = name;
            row["location"] = location;
            row["routeId"] = Rid;
    
            DT.Rows.Add(row);
    
            StopListBox.DataSource = DT;
            StopListBox.DataTextField = "id";
            StopListBox.DataValueField = "id";
            StopListBox.DataBind();
    
            StopID.Text = "";
            StopName.Text = "";
            StopLocation.Text = "";
    
            ViewState["Stop"] = DT;