如何在用户控件的 onchange 事件中保留文本框的先前值
How to retain previous value for a textbox in onchange event in usercontrol
我有一个带有几个文本框的用户控件,如果在 OnTextChanged 事件中不满足 txtA、txtB、txtC 文本框总和的条件,我如何保留文本框 txtOne 的先前值。
变量"one"被认为是文本框的前一个值。我在用户控件中添加了以下代码。
protected void txtOne_TextChanged(object sender, EventArgs e)
{
total = Convert.ToInt32(txtA.Text) + Convert.ToInt32(txtB.Text) + Convert.ToInt32(txtC.Text);
if (total > Convert.ToInt32(txtOne.Text.ToString()))
{
txtOne.Text = one.ToString();
}
}
这里 "one" 变量的值为 0。它应该存储以前的值。你能给我一个关于将值存储在 "one" 变量中的位置的线索吗?
下面的代码没有以任何方式优化或推广。它尽可能接近您的示例代码,旨在根据您的原始代码向您展示答案。我建议使用组合框而不是文本框,and/or 使用验证来确保条目都是数字。下面的代码不会走那么远 - 它只会根据您提供的代码回答您的问题:
TextBox txtA = new TextBox();
TextBox txtB = new TextBox();
TextBox txtC = new TextBox();
int total = 0;
TextBox txtOne = new TextBox();
string newOne = "";
string someDefaultValue = "";
string lastOne = "";
if(txtA.Text.Length==0||txtB.Text.Length==0||txtC.Text.Length==0){
//user has not entered required fields -- abort
return;
}
bool isTextChanging = true;//CHANGE TO FALSE AT END OF PAGE_ONLOAD
protected void txtOne_TextChanged(object sender, EventArgs e)
{
if(!isTextChanging){
isTextChanging=true;
total = getTotal(new string[] { txtA.Text, txtB.Text, txtC.Text });
if (total > -1)
{
int totalTest = 0;
if (int.TryParse(txtOne.Text, out totalTest))
{
if (total > totalTest)
{
txtOne.Text = lastOne.Length > 0 ? lastOne : someDefaultValue;//default value for the first run when there is no last value
lastOne = newOne;//whatever the value of "one" is this time
}
}
else
{
MessageBox.Show("YOu must only enter numbers");
}
}
}
isTextChanging=false;
}
private int getTotal(string[] inputs)
{
int total = 0;
int subTotal = 0;
foreach(string input in inputs)
{
if(int.TryParse(input,out subTotal)){
total += subTotal;
}else{
MessageBox.Show("You must only enter numbers");
return -1;
}
}
return total;
}
好的 - 我很困惑。可能是语言的问题。这就是您要实现的目标,但使用的是文本框而不是下拉列表吗?
前端:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SO_Web.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="div_Boxes" runat="server">
</div>
</form>
</body>
</html>
后端:
protected void Page_Load(object sender, EventArgs e)
{
createBoxes();
}
string[] boxNames = { "One", "Two", "Three" };
private void createBoxes()
{
int x = 0;
int y = 10;
Panel p = new Panel();
foreach (string name in boxNames)
{
Label l = new Label();
l.ID = "lbl_" + name;
l.Text = "Select Value " + name;
l.Style.Add("float", "left");
DropDownList c = new DropDownList();
c.ID = "cbx_" + name;
c.Items.Add("Select One");
for (int i = 1; i < 101; i++)
{
c.Items.Add(i.ToString());
}
c.SelectedIndex = 0;
c.AutoPostBack = true;
c.Style.Add("display", "block");
c.SelectedIndexChanged += cbx_Changed;
p.Controls.Add(l);
p.Controls.Add(c);
}
Label lbl_Total = new Label();
lbl_Total.Text = "Your Total:";
TextBox txt_Total = new TextBox();
txt_Total.ID = "txt_Total";
txt_Total.Width = 75;
p.Controls.Add(lbl_Total);
p.Controls.Add(txt_Total);
p.Width = 300;
p.Height = 200;
div_Boxes.Controls.Add(p);
}
protected void cbx_Changed(object sender, EventArgs e)
{
bool proceed = true;
int total = 0;
foreach (string name in boxNames)
{
DropDownList c = (DropDownList)findControl(this.Page.Controls,"cbx_" + name);
if (c.SelectedIndex == 0)
{
proceed = false;
}
else
{
total += c.SelectedIndex;
}
}
if (proceed)
{
((TextBox)findControl(this.Page.Controls,"txt_Total")).Text = total.ToString("C2");
}
}
private Control findControl(ControlCollection page, string id)
{
foreach (Control c in page)
{
if (c.ID == id)
{
return c;
}
if (c.HasControls())
{
var res = findControl(c.Controls, id);
if (res != null)
{
return res;
}
}
}
return null;
}
但是您想使用文本框并允许用户键入条目,并且您希望最大值为 110。如果用户为所有三个框输入的值均小于 110,则您需要总计。如果用户在任何框中输入 >110,您希望值重置为 100?对吗?
我有一个带有几个文本框的用户控件,如果在 OnTextChanged 事件中不满足 txtA、txtB、txtC 文本框总和的条件,我如何保留文本框 txtOne 的先前值。
变量"one"被认为是文本框的前一个值。我在用户控件中添加了以下代码。
protected void txtOne_TextChanged(object sender, EventArgs e)
{
total = Convert.ToInt32(txtA.Text) + Convert.ToInt32(txtB.Text) + Convert.ToInt32(txtC.Text);
if (total > Convert.ToInt32(txtOne.Text.ToString()))
{
txtOne.Text = one.ToString();
}
}
这里 "one" 变量的值为 0。它应该存储以前的值。你能给我一个关于将值存储在 "one" 变量中的位置的线索吗?
下面的代码没有以任何方式优化或推广。它尽可能接近您的示例代码,旨在根据您的原始代码向您展示答案。我建议使用组合框而不是文本框,and/or 使用验证来确保条目都是数字。下面的代码不会走那么远 - 它只会根据您提供的代码回答您的问题:
TextBox txtA = new TextBox();
TextBox txtB = new TextBox();
TextBox txtC = new TextBox();
int total = 0;
TextBox txtOne = new TextBox();
string newOne = "";
string someDefaultValue = "";
string lastOne = "";
if(txtA.Text.Length==0||txtB.Text.Length==0||txtC.Text.Length==0){
//user has not entered required fields -- abort
return;
}
bool isTextChanging = true;//CHANGE TO FALSE AT END OF PAGE_ONLOAD
protected void txtOne_TextChanged(object sender, EventArgs e)
{
if(!isTextChanging){
isTextChanging=true;
total = getTotal(new string[] { txtA.Text, txtB.Text, txtC.Text });
if (total > -1)
{
int totalTest = 0;
if (int.TryParse(txtOne.Text, out totalTest))
{
if (total > totalTest)
{
txtOne.Text = lastOne.Length > 0 ? lastOne : someDefaultValue;//default value for the first run when there is no last value
lastOne = newOne;//whatever the value of "one" is this time
}
}
else
{
MessageBox.Show("YOu must only enter numbers");
}
}
}
isTextChanging=false;
}
private int getTotal(string[] inputs)
{
int total = 0;
int subTotal = 0;
foreach(string input in inputs)
{
if(int.TryParse(input,out subTotal)){
total += subTotal;
}else{
MessageBox.Show("You must only enter numbers");
return -1;
}
}
return total;
}
好的 - 我很困惑。可能是语言的问题。这就是您要实现的目标,但使用的是文本框而不是下拉列表吗?
前端:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SO_Web.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="div_Boxes" runat="server">
</div>
</form>
</body>
</html>
后端:
protected void Page_Load(object sender, EventArgs e)
{
createBoxes();
}
string[] boxNames = { "One", "Two", "Three" };
private void createBoxes()
{
int x = 0;
int y = 10;
Panel p = new Panel();
foreach (string name in boxNames)
{
Label l = new Label();
l.ID = "lbl_" + name;
l.Text = "Select Value " + name;
l.Style.Add("float", "left");
DropDownList c = new DropDownList();
c.ID = "cbx_" + name;
c.Items.Add("Select One");
for (int i = 1; i < 101; i++)
{
c.Items.Add(i.ToString());
}
c.SelectedIndex = 0;
c.AutoPostBack = true;
c.Style.Add("display", "block");
c.SelectedIndexChanged += cbx_Changed;
p.Controls.Add(l);
p.Controls.Add(c);
}
Label lbl_Total = new Label();
lbl_Total.Text = "Your Total:";
TextBox txt_Total = new TextBox();
txt_Total.ID = "txt_Total";
txt_Total.Width = 75;
p.Controls.Add(lbl_Total);
p.Controls.Add(txt_Total);
p.Width = 300;
p.Height = 200;
div_Boxes.Controls.Add(p);
}
protected void cbx_Changed(object sender, EventArgs e)
{
bool proceed = true;
int total = 0;
foreach (string name in boxNames)
{
DropDownList c = (DropDownList)findControl(this.Page.Controls,"cbx_" + name);
if (c.SelectedIndex == 0)
{
proceed = false;
}
else
{
total += c.SelectedIndex;
}
}
if (proceed)
{
((TextBox)findControl(this.Page.Controls,"txt_Total")).Text = total.ToString("C2");
}
}
private Control findControl(ControlCollection page, string id)
{
foreach (Control c in page)
{
if (c.ID == id)
{
return c;
}
if (c.HasControls())
{
var res = findControl(c.Controls, id);
if (res != null)
{
return res;
}
}
}
return null;
}
但是您想使用文本框并允许用户键入条目,并且您希望最大值为 110。如果用户为所有三个框输入的值均小于 110,则您需要总计。如果用户在任何框中输入 >110,您希望值重置为 100?对吗?