ViewState 不工作
ViewState is not working
我正在使用 viewState 将数据绑定到列表 box.when 我添加了单击添加停止按钮列表框未填充任何内容。我在这里做什么。请帮帮我。视图状态不工作。我曾尝试用谷歌搜索我没有找到的问题和合适的解决方案。任何人对我的问题有任何想法请帮助我。
<%@ 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 = "";
}
}
问题与 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 = 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;
我正在使用 viewState 将数据绑定到列表 box.when 我添加了单击添加停止按钮列表框未填充任何内容。我在这里做什么。请帮帮我。视图状态不工作。我曾尝试用谷歌搜索我没有找到的问题和合适的解决方案。任何人对我的问题有任何想法请帮助我。
<%@ 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 = "";
}
}
问题与 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 = 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;