文本框.text 值不反映代码隐藏 C# ASP.Net 中的用户键入的值
本文关键字:Net ASP 用户 隐藏 text 代码 文本 | 更新日期: 2023-09-27 18:36:17
>我有一个编辑按钮,可以使某些表单元素从禁用变为启用,然后进行编辑。除了某种原因外,我让它正常工作,在button_click事件中,当我尝试从 updateButton_Click() 内部引用 Textbox.Text 值时,它仍然具有旧值而不是用户编辑的值。有什么想法吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
public partial class UserControls_OrderDetailsAdmin : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Page_PreRender(sender, e);
}
// edit mode by default is false
private bool editMode = false;
// set up the form
protected void Page_PreRender(object sender, EventArgs e)
{
// check if we must display order details
if (Session["AdminOrderID"] != null)
{
//fill constituent controls with data
PopulateControls();
// set edit mode
SetEditMode(editMode);
}
else
// Hide
this.Visible = false;
}
// populate the form with data
private void PopulateControls()
{
if (Session["AdminOrderID"] != null)
{
// obtain order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// obtain order info
OrderInfo orderInfo = OrdersAccess.GetInfo(orderId);
// populate labels and text boxes with order info
orderIdLabel.Text = "Displaying Order #" + orderId;
totalAmountLabel.Text = String.Format("{0:c}", orderInfo.GrandTotal);
dateCreatedTextBox.Text = orderInfo.DateCreated;
dateShippedTextBox.Text = orderInfo.DateShipped;
statusLabel.Text = orderInfo.Status.ToString(); ;
statusLabel.Enabled = false;
completedCheck.Checked = orderInfo.Completed;
canceledCheck.Checked = orderInfo.Canceled;
commentsTextBox.Text = orderInfo.Comments;
// by default the Edit button is enabled, and the
// Update and Cancel buttons are disabled
editButton.Enabled = true;
updateButton.Enabled = false;
cancelButton.Enabled = false;
// Decide which one of the other three buttons
// should be enabled and which should be disabled
if (canceledCheck.Checked || completedCheck.Checked)
{
// if the order was canceled or completed ...
markVerifiedButton.Enabled = false;
markCompletedButton.Enabled = false;
markCanceledButton.Enabled = false;
}
else if (verifiedCheck.Checked)
{
// if the order was not canceled but is verified ...
markVerifiedButton.Enabled = false;
markCompletedButton.Enabled = true;
markCanceledButton.Enabled = true;
}
else
{
// if the order was not canceled and is not verified ...
markVerifiedButton.Enabled = true;
markCompletedButton.Enabled = false;
markCanceledButton.Enabled = true;
}
// fill the data grid with order details
grid.DataSource = OrdersAccess.GetDetails(orderId);
grid.DataBind();
}
}
// enable or disable edit mode
private void SetEditMode(bool enable)
{
dateCreatedTextBox.Enabled = false;
dateShippedTextBox.Enabled = enable;
verifiedCheck.Enabled = enable;
completedCheck.Enabled = enable;
canceledCheck.Enabled = enable;
commentsTextBox.Enabled = enable;
customerNameTextBox.Enabled = enable;
}
protected void editButton_Click(object sender, EventArgs e)
{
editMode = true;
}
protected void updateButton_Click(object sender, EventArgs e)
{
//SetEditMode(false);
//System.Threading.Thread.SpinWait(500);
// Store the new order details in an OrderInfo object
OrderInfo orderInfo = new OrderInfo();
string orderId = Session["AdminOrderID"].ToString();
orderInfo.OrderID = Int32.Parse(orderId);
orderInfo.DateCreated = dateCreatedTextBox.Text;
orderInfo.DateShipped = dateShippedTextBox.Text;
orderInfo.Verified = verifiedCheck.Checked;
orderInfo.Completed = completedCheck.Checked;
orderInfo.Canceled = canceledCheck.Checked;
orderInfo.Comments = commentsTextBox.Text;
orderInfo.CustomerName = customerNameTextBox.Text;
// try to update the order
try
{
// Update the order
OrdersAccess.Update(orderInfo);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.StackTrace);
}
// Exit edit mode
// SetEditMode(false);
// Update the form
SetEditMode(false);
PopulateControls();
}
protected void cancelButton_Click(object sender, EventArgs e)
{
// don't need to do anything, editMode will be set to false by default
}
protected void markVerifiedButton_Click(object sender, EventArgs e)
{
// obtain the order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// mark order as verified
OrdersAccess.MarkVerified(orderId);
// update the form
PopulateControls();
}
protected void markCompletedButton_Click(object sender, EventArgs e)
{
// obtain the order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// mark the order as completed
OrdersAccess.MarkCompleted(orderId);
// update the form
PopulateControls();
}
protected void markCanceledButton_Click(object sender, EventArgs e)
{
// obtain the order ID from the session
string orderId = Session["AdminOrderID"].ToString();
// mark the order as canceled
OrdersAccess.MarkCanceled(orderId);
// update the form
PopulateControls();
}
}
尝试在页面加载事件中使用页面isPostBack
属性
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack){
Page_PreRender(sender, e);
}
}