2017-05-25 49 views
0

我使用viewState將數據綁定到列表box.when我添加單擊添加停止按鈕列表框不填充任何東西。我在這裏做什麼。請幫助我。 viewstate不起作用。我試着用Google搜索我的問題,但沒有找到合適的解決方案。任何人有任何關於我的問題的想法請我出來。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 = ""; 


    } 
} 
+0

你的表具有0行。嘗試通過添加一個新的'DataRow'來填充它。 –

回答

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; 
+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一個更好的方法,由於視圖狀態值將在每一個往返於服務器在一個隱藏字段被傳遞,從而增加了請求和響應大小。