如何在用户控件的onchange事件中保留文本框的先前值
本文关键字:文本 保留 事件 用户 控件 onchange | 更新日期: 2023-09-27 18:04:31
我有一个用户控件,其中有几个文本框,如果在OnTextChanged事件中不满足txtA, txtB, txtC文本框的总和,我如何保留文本框txtOne的先前值。
变量"one"被认为是文本框的前一个值。我在usercontrol中添加了下面的代码。
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。它应该存储以前的值。
下面的代码没有以任何方式优化或一般化。它尽可能接近您的示例代码&旨在根据您的原始代码向您显示答案。我建议使用组合框而不是文本框,和/或使用验证来确保所有条目都是数字。下面的代码没有这么远——它只是根据你提供的代码回答你的问题:
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;
}
但是您希望使用文本框并允许用户键入条目,并且您希望MAX值为110。如果用户在所有三个框中输入的值都低于110,则需要得到总数。如果用户为任何框输入>110,您希望将值重置为100吗?对吗?