GUI输出与本地与生产不同

时间:2010-04-12 19:21:24

标签: c# .net-3.5 asp.net-3.5

我有一种奇怪的经历。当用户单击按钮时,我在运行时动态创建一行文本框。

但是,在我的本地计算机上,文本框正确显示(例如

[TextBox1] [TextBox2] [TextBox3] [TextBox4] [TextBox5]
[TextBox1] [TextBox2] [TextBox3] [TextBox4] [TextBox5]
[TextBox1] [TextBox2] [TextBox3] [TextBox4] [TextBox5]

当我在制作中运行此应用程序时,输出并排是:

[TextBox1][TextBox1]  [TextBox2][TextBox2] [TextBox3][TextBox3] [TextBox4][TextBox4] 

输出应该是一行文本框,然后是第二行的5个文本框等。

构建文本框的代码是:

        protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        dyntxtCar = new TextBox[myCount];
        dyntxtMake = new TextBox[myCount];
        dyntxtMileage = new TextBox[myCount];
        dyntxtVIN = new TextBox[myCount];
        dyntxtSLIC = new TextBox[myCount];
        dyntxtPlateNumber = new TextBox[myCount];

        for (i = 0; i < myCount; i += 1)
        {
            TextBox txtCar = new TextBox();
            TextBox txtMake = new TextBox();
            TextBox txtMileage = new TextBox();
            TextBox txtVIN = new TextBox();
            TextBox txtSLIC = new TextBox();
            TextBox txtPlateNumber = new TextBox();

            txtCar.ID = "txtVehCar" + i.ToString();
            txtMake.ID = "txtVehMake" + i.ToString();
            txtMileage.ID = "txtVehMilage" + i.ToString();
            txtVIN.ID = "txtVehVIN" + i.ToString();
            txtSLIC.ID = "txtVehSLIC" + i.ToString();
            txtPlateNumber.ID = "txtVehPlate" + i.ToString();

            //Set tabIndex values for dynamic text fields;
            txtCar.TabIndex = System.Convert.ToInt16(i * 10 + 1);
            txtMake.TabIndex = System.Convert.ToInt16(i * 10 + 2);
            txtMileage.TabIndex = System.Convert.ToInt16(i * 10 + 3);
            txtVIN.TabIndex = System.Convert.ToInt16(i * 10 + 4);
            txtSLIC.TabIndex = System.Convert.ToInt16(i * 10 + 5);
            txtPlateNumber.TabIndex = System.Convert.ToInt16(i * 10 + 6);

            //Set maxlength for dynamic fields;
            txtCar.MaxLength = System.Convert.ToInt16(7);
            txtVIN.MaxLength = System.Convert.ToInt16(17);
            txtSLIC.MaxLength = System.Convert.ToInt16(4);

            //Set width of text boxes
            txtCar.Width = System.Convert.ToInt16("65");
            txtMileage.Width = System.Convert.ToInt16("50");
            txtVIN.Width = System.Convert.ToInt16("220");
            txtSLIC.Width = System.Convert.ToInt16("45");
            //txtPlateNumber.Width = System.Convert.ToInt16("35");

            phCar.Controls.Add(txtCar);
            phMake.Controls.Add(txtMake);
            phMileage.Controls.Add(txtMileage);
            phVIN.Controls.Add(txtVIN);
            phSLIC.Controls.Add(txtSLIC);
            phPlateNumber.Controls.Add(txtPlateNumber);

            dyntxtCar[i] = txtCar;
            dyntxtMake[i] = txtMake;
            dyntxtMileage[i] = txtMileage;
            dyntxtVIN[i] = txtVIN;
            dyntxtSLIC[i] = txtSLIC;
            dyntxtPlateNumber[i] = txtPlateNumber;

            LiteralControl literalBreak = new LiteralControl("<br />");

            phCar.Controls.Add(literalBreak);
            phMake.Controls.Add(literalBreak);
            phMileage.Controls.Add(literalBreak);
            phVIN.Controls.Add(literalBreak);
            phSLIC.Controls.Add(literalBreak);
            phPlateNumber.Controls.Add(literalBreak);
        }
    }


    protected void Page_PreInit(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if ((myControl != null))
        {
            if ((myControl.ClientID.ToString() == "btnAddTextBox"))
            {
                myCount = myCount + 1;
            }
        }
    }


    public static Control GetPostBackControl(Page thePage)
    {
        Control myControl = null;
        string ctrlName = thePage.Request.Params.Get("__EVENTTARGET");
        if (((ctrlName != null) & (ctrlName != string.Empty)))
        {
            myControl = thePage.FindControl(ctrlName);
        }
        else
        {
            foreach (string Item in thePage.Request.Form)
            {
                Control c = thePage.FindControl(Item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    myControl = c;
                }
            }
        }
        return myControl;
    }

有人经历过这个吗?

0 个答案:

没有答案