如何在usercontrol中的onchange事件中保留文本框的先前值

时间:2016-08-27 11:44:57

标签: c# asp.net user-controls

我有一个带有几个文本框的用户控件,如果在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.它应该存储以前的值。能否请您告诉我在“一个”变量中存储值的位置。

2 个答案:

答案 0 :(得分:1)

以下代码不以任何方式优化或推广。它尽可能接近您的示例代码&旨在根据您的原始代码向您显示答案。我建议使用组合框而不是文本框,和/或使用验证来确保条目都是数字。下面的代码没有那么远 - 它只根据您提供的代码回答您的问题:

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;
    }

答案 1 :(得分:0)

好的 - 我很困惑。它可能是一种语言的东西。这是你想要完成的,但是使用文本框而不是下拉列表?

前端:

<%@ 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;
    }

但您想使用texbox并允许用户输入条目,并且您希望MAX值为110.如果用户为所有三个框输入的值低于110,则需要总数。如果用户为任何框输入&gt; 110,您希望将值重置为100吗?这是对的吗?