简体   繁体   中英

Textbox array textchanged doesn't fire properly

In my code I need to make some textboxes[] that will get some information from the database however its working for 50%

    public static Table tableinfo = new Table();
    public static TableRow rowinfo = new TableRow();
    public static TextBox[] information = new TextBox[1000];
    public static Label[] information1 = new Label[1000];
    public static Label[] information2 = new Label[1000];
    public static Label[] information3 = new Label[1000];
    public static Label[] information4 = new Label[1000];
    public static string[] gettext = new string[1000];
    public static int textboxes;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            textboxes = 1;
            for (int i = 0; i < 1000; i++)
            {
                Array.Clear(gettext, 0, gettext.Length);
                Array.Clear(information, 0, information.Length);
            }
        }
        else
        {

        }
        tableinfo.Controls.Clear();
        tableinfo.BorderWidth = 1;
        addtextboxes();
        placeholder1.Controls.Add(tableinfo);
        makeandfilltextboxes();
    }

    public static void makeandfilltextboxes()
    {
        for (int i = 0; i < textboxes; i++)
        {
            gettext[i] = "";
        }
        for (int i = 0; i < textboxes; i++)
        {
            gettext[i] = information[i].Text;
            //go to database and fetch information
            information1[i].text = databaseinformation1[i];
            information2[i].text = databaseinformation2[i];
            information3[i].text = databaseinformation3[i];
            information4[i].text = databaseinformation4[i];
        }
    }

    static void TestForm_TextChanged(object sender, EventArgs e)
    {
        makeandfilltextboxes();
        tableinfo.Controls.Clear();
        tableinfo.BorderWidth = 1;
        textboxes++;
        addtextboxes();
    }
    public static void addtextboxes()
    {
        for (int i = 0; i < textboxes; i++)
        {
            rowinfo = new TableRow();
            TableCell cellinfo = new TableCell();
            information[i] = new TextBox();
            information[i].AutoPostBack = true;
            information[i].CausesValidation = false;
            information[i].EnableViewState = true;
            information[i].Text = gettext[i];
            information[i].TextChanged += new EventHandler(TestForm_TextChanged);
            rowinfo.Cells.Add(cellinfo);
            cellinfo.Controls.Add(information[i]);
            tableinfo.Controls.Add(rowinfo);

            TableCell cellinfo1 = new TableCell();
            information1[i] = new Label();
            information1[i].EnableViewState = true;
            information1[i].Text = databaseinformation1[i];
            rowinfo.Cells.Add(cellinfo1);
            cellinfo.Controls.Add(information1[i]);
            tableinfo.Controls.Add(rowinfo);

            TableCell cellinfo2 = new TableCell();
            information2[i] = new Label();
            information2[i].EnableViewState = true;
            information2[i].Text = databaseinformation2[i];
            rowinfo.Cells.Add(cellinfo2);
            cellinfo.Controls.Add(information2[i]);
            tableinfo.Controls.Add(rowinfo);

            TableCell cellinfo3 = new TableCell();
            information3[i] = new Label();
            information3[i].EnableViewState = true;
            information3[i].Text = datbaseinformation3[i];
            rowinfo.Cells.Add(cellinfo3);
            cellinfo.Controls.Add(information3[i]);
            tableinfo.Controls.Add(rowinfo);

            TableCell cellinfo4 = new TableCell();
            information4[i] = new Label();
            information4[i].EnableViewState = true;
            information4[i].Text = databaseinformation4[i];
            rowinfo.Cells.Add(cellinfo4);
            cellinfo.Controls.Add(information4[i]);
            tableinfo.Controls.Add(rowinfo);
        }
    }

So as you can see, I'm filling the array's with the value you filled in the information[i] on the value in there i'm looking in the database if i can find something. this is all working perfectly. the only problem is that the information[i].TextChanged += new EventHandler(TestForm_TextChanged); is called in a strange manner.

the 1st time you enter a textbox is fires the 2nd time it isn't fired the 3rd time it fires the 4th time it isn't fired and so on

If I can fire the TestForm_TextChanged everytime the problem will be fixed.

I also have some checkboxes who are having the eventhandler of checkedchanged who are showing the information on the hand of there are checked or not is this causing some problems im working in a updatepanel and have a scriptmanager on my aspx desinger. what is causing this problem?

With Google chrome I can use the ENTER and it will work all the time. But the enter doesn't work in Internet Explorer.

NOTE: THE CODE THAT I HAVE SEND HERE IS JUST A REPLICA NOT THE REAL CODE. THIS IS DUE THE POLICY OF MY INTERNSHIP.

If you need more information about the code be free to ask ill try to be as much detailed as can.

I think the problem is that all of your textboxes and labels are static. Make them part of your Page instance.

I fixed the problem apparently the problem was that there wasn't a Id

so i added information[i].ID = "information" + i; and that fixed the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM