ViewState无法正常工作

时间:2017-05-25 08:27:29

标签: c# asp.net viewstate

我正在使用viewState将数据绑定到列表框。当我添加点击添加停止按钮列表框不会填充任何内容。我在这做什么请帮帮我。 viewstate无法正常工作。我曾尝试谷歌搜索我没有找到合适解决方案的问题。任何人都对我的问题有任何想法,请将我赶出去。

 <%@ 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>

以下是文件背后的代码

    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 = "";


    }
}

2 个答案:

答案 0 :(得分:0)

问题与ViewState无关。 您只需在AddStopToList_Click上创建一行,然后在绑定前将其添加到DataTable

protected void AddStopToList_Click(object sender, EventArgs e)
{
    DT = (DataTable)ViewState["Stop"];
    var newRow = DT.NewRow();
    newRow["id"] = StopID.Text;
    newRow["name"] = StopName.Text;
    newRow["location"] = StopLocation.Text;
    newRow["routeId"] = RouteDropDown.SelectedItem.Value.ToString().Trim();
    DT.Rows.Add(newRow);
    ViewState["Stop"] = DT; //Save DataTable back to ViewState

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

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

作为旁注,Session是比使用ViewState更好的方法,因为viewstate值将在隐藏字段中的每次往返服务器上传递,从而增加请求和响应大小。

答案 1 :(得分:0)

只是忘记了将项目添加到数据表中。

最后请不要忘记再次注册viewState = DT。否则,即使添加新项目,列表框也只包含1个项目。

您应该按如下方式编辑代码:

        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;