繁体   English   中英

将值放入文本框数组(使用C#的ASP.NET)

[英]Taking values into an array of textboxes (ASP.NET using C#)

因此,我有一个动态显示的文本框数组(文本框的数量取决于数据库中数字的数量)。 他们画到屏幕上就好了。

    i = 0;

    while (i < size)
    {
        pnlTxtBoxes.Controls.Add(labels[i]);
        pnlTxtBoxes.Controls.Add(txtBoxes[i]);
        pnlTxtBoxes.Wrap = true;
        i++;
    }

就像我说的,出现文本框,并且标签显示正确。 但是,当我从它们中检索文本时,出现错误“对象引用未设置为对象的实例”。

    i = 0;

    while (i < size)
    {
        values[i] = txtBoxes[i].Text;
        txtBoxes[i].Visible = false;
        labels[i].Visible = false;
        i++;
    } 

是否有人对我为什么会收到此错误(以及如何解决该错误)有所了解?

编辑:这是所有代码。 这只是一个开发数据库,​​所以我不必担心显示密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

public partial class dieClearanceCalc : System.Web.UI.Page
{
static string connectionString = "database=localhost;database=matedevdb;uid=dev;pwd=123;";
MySqlConnection con = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("SELECT shapeName FROM tblShapes;");
MySqlDataReader reader;
int size;
TextBox[] txtBoxes;
Label[] labels;

protected void Page_Load(object sender, EventArgs e)
{
    cmd.Connection = con;
    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            shapeSelection.Items.Add(reader.GetString(0));
        }
        reader.Close();
    }
    catch (Exception ex)
    {
        Response.Write("<p style='Color:red'>Error:<br/>" + ex + "</p>");
    }
}
protected void shapeSelected(object sender, EventArgs e)
{
    string[] labelTxt;
    int i = 0;

    //Make current elements invisable
    lblShape.Visible = false;
    shapeSelection.Visible = false;
    btnSelectShape.Visible = false;

    // find the size of the arrays
    cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        size = reader.GetInt32(0);
    }
    reader.Close();

    labelTxt = new string[size];
    labels = new Label[size];
    txtBoxes = new TextBox[size];

    // gather the labels from the db
    cmd.CommandText = "SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();

    i = 0;

    while (reader.Read())
    {
        labelTxt[i] = reader.GetString("varDesc");
        i++;
    }
    reader.Close();

    i = 0;

    while (i < size)
    {
        labels[i] = new Label();
        txtBoxes[i] = new TextBox();
        labels[i].Text = labelTxt[i];
        i++;
    }

    i = 0;

    while (i < size)
    {
        pnlTxtBoxes.Controls.Add(labels[i]);
        pnlTxtBoxes.Controls.Add(txtBoxes[i]);
        pnlTxtBoxes.Wrap = true;
        i++;
    }

    btnSendData.Visible = true;
    //Response.Write(size); test to see if the size variable is working
    Response.Write(size);

}

protected void calc(object sender, EventArgs e)
{
    //declarations  
    formula diagonal, periphery;
    string dFormula = "", pFormula = "";
    string[] variables;
    string[] values;
    int i = 0;
    //end of declarations

    // This value must be retrievd again, because somewhere size is getting a value of  0
    cmd.CommandText = "SELECT COUNT(varID) FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        size = reader.GetInt32(0);
    }
    reader.Close();


    variables = new string[size];
    values = new string[size];

    i = 0;

    while (i < size)
    {
        values[i] = txtBoxes[i].Text;
        txtBoxes[i].Visible = false;
        labels[i].Visible = false;
        i++;
    }

    btnSendData.Visible = false;

    // retrieve the diagonal formula from the db
    cmd.CommandText = "SELECT diagonalFormula, peripheryFormula FROM tblShapes WHERE shapeName='" + shapeSelection.SelectedValue + "'";
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        dFormula = reader.GetString("diagonalFormula");
        pFormula = reader.GetString("peripheryFormula");
    }
    reader.Close();

    Response.Write(size);

    // gather the variable names from the db
    cmd.CommandText = "SELECT varName FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '" + shapeSelection.SelectedValue + "')";
    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        variables[i] = reader.GetString("varName");
        i++;
    }
    reader.Close();

    con.Close();

    diagonal = new formula(dFormula, variables, values);
    periphery = new formula(pFormula, variables, values);

    txtDiagonal.Visible = true;
    txtPeriphery.Visible = true;

    txtDiagonal.Text = diagonal.getEquation();
    txtPeriphery.Text = periphery.getEquation();
}

public static double Evaluate(string expression)
{
    System.Data.DataTable table = new System.Data.DataTable();
    table.Columns.Add("expression", string.Empty.GetType(), expression);
    System.Data.DataRow row = table.NewRow();
    table.Rows.Add(row);
    return double.Parse((string)row["expression"]);
}  

}

我认为如果IsPostBack块,则初始化标签和txtBoxes,不要将其保存在ViewState或Session中。

编辑:看到您的代码标签和txtBoxes是在shapeSelected方法中初始化的。 同样的问题也会发生:它们在回发之间丢失了。

因此,在回发时它们在事件处理程序中为空,因为在回发时已重新创建了整个Page对象。 Asp.net运行时有助于从ViewState加载内容以进行控制,但是对于类成员变量则必须自己维护。

public string NavigateUrl
{
  get
  {
    string text = (string) ViewState["NavigateUrl"];
    if (text != null)
       return text;
    else
       return string.Empty;
  }
  set
  {
    ViewState["NavigateUrl"] = value;
  }
}

上面的代码来自:
了解ASP.NET视图状态
http://msdn.microsoft.com/en-us/library/ms972976.aspx
本文还介绍了视图状态和动态添加的控件

在没有看到更多代码的情况下,我只能猜测问题出在填充txtBoxes[]数组的位置。 确保该数组中没有空值。

使用ASP.NET进行编程时要记住的重要一点是,页面上的整个对象模型会一次又一次地与每个Web请求一起创建。 这意味着,如果在每次页面加载都没有发生的情况下向页面动态添加一些控件,那么即使您不以某种方式再次添加它们,它们也将无法幸免于回发。

您可以在此处阅读有关Asp.Net页面生命周期的信息: http : //msdn.microsoft.com/zh-cn/library/ms178472.aspx

我认为没有解决问题的标准方法,但是您可以通过多种方法来实现。 您已经提到了一个-ViewState的用法。 另一种方法是在每个回发页面上打数据库,并确保每次提供页面时都重新创建控件。 另一种方法是对重新创建控件所需的数据进行编码,并确保每次回发时都传递它们。 后者本质上是ViewState的功能,但是如果您想减小ViewState的大小,从而减小回发和页面的字节大小,则可以更有效地做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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