1

我正在尝试从数据库中获取字符串列表。

对于列表中的每个字符串,我想在页面中添加一个标签和文本框。

在按钮提交上,我想收集文本框值以及相应的标签值,然后将其保存到数据库中。

我需要帮助从文本框中检索值。

到目前为止我所拥有的:

Panel1 在 aspx 页面上

    protected List<string> items = MyClass.GetItems();


    protected void Page_Load(object sender, EventArgs e)
    {
        GenerateItemsTable();
    }

    private void GenerateItemsTable()
    {
        Table table = new Table();
        table.ID = "Table1";
        //PlaceHolder1.Controls.Add(table);
        Panel1.Controls.Add(table);

        foreach (var x in items)
        {
            TableRow row = new TableRow();
            for (int y = 0; y < 1; y++)
            {
                TableCell labelCell = new TableCell();
                labelCell.Controls.Add(CreateLabel(x));
                labelCell.CssClass = "tdLabel";
                row.Cells.Add(labelCell);

                TableCell txbCell = new TableCell();
                txbCell.Controls.Add(CreateRadNumericTextBox(x));
                txbCell.Width = 30;
                row.Cells.Add(txbCell);

                TableCell dataTypeCell = new TableCell();
                dataTypeCell.Text = "<span style='font-size: 10px; color: #777'>(student count)</span>";
                dataTypeCell.Width = 100;
                row.Cells.Add(dataTypeCell);

                TableCell fourthCell = new TableCell();
                if (x == items[items.Count - 1])
                {
                    RadButton rb = new RadButton();
                    rb.ID = "submit";
                    rb.Text = "Submit Guidance";
                    rb.Skin = "Forest";
                    rb.Click += new EventHandler(submit_Click);
                    rb.AutoPostBack = true;
                    fourthCell.Controls.Add(rb);
                    row.Cells.Add(fourthCell);
                }
                else
                {
                    row.Cells.Add(fourthCell);
                }

            }
            table.Rows.Add(row);
        }
    }

    private RadNumericTextBox CreateRadNumericTextBox(string x)
    {
        RadNumericTextBox rntb = new RadNumericTextBox();
        rntb.ID = x;
        rntb.Width = 40;
        return rntb;
    }

    private Label CreateLabel(string x)
    {
        Label l = new Label();
        l.ID = "label_" + x;
        l.Text = "<label>" + x + "</label>";
        return l;
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
        {
            if (x is RadNumericTextBox)
            {
                //how to get the data??????/
            }
        }
    }

(感谢那些真正阅读整篇文章的人)

-----------------更新的解决方案----------------------------- -------------

我决定更改它并将数据库中的列表存储在 page_load 中。存储列表后,我遍历列表并使用 FindControl() 访问文本框。像这样的东西。。

//a couple containers
protected class ItemVal
{
    public int Value { get; set; }
    public string Name { get; set; }
}
protected List<ItemVal> items = new List<ItemVal>(); 


//get the list from that database
protected void GetItems()
{
    foreach (var x in MyClass.GetItems())
    {
        ItemVal i = new ItemVal();
        i.Name = x;
        items.Add(i);
    }
}

//submit
protected void submit_Click(object sender, EventArgs e)
{
    foreach (var x in items)
    {
        RadNumericTextBox rntb = FindControl(x.Name) as RadNumericTextBox;
        x.Value = (int)rntb.Value;
    }
}
4

1 回答 1

0

您需要将 x 转换为 a RadNumericTextBox,然后提取所需的属性值,如下所示:

RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
string val = theRadNumericTextBox.Text;

然后对于您想要的其他控件,您需要if为其类型设置条件,如下所示:

if (x is Label)
{
    Label theLabel = x as Label;
    string valLabel = theLabel.Text;
}

以下是该方法的完整代码:

protected void submit_Click(object sender, EventArgs e)
{
    foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
    {
        Label theLabel;
        RadNumericTextBox theRadNumericTextBox;

        if (x is RadNumericTextBox)
        {
            RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
            string val = theRadNumericTextBox.Text;
        }

        if (x is Label)
        {
            Label theLabel = x as Label;
            string valLabel = theLabel.Text;
        }

        // Either store up in a list or save to the database on each loop; it is recommended to store a list and send all the changes at once for a database save, but that is your choice
    }
}
于 2013-08-09T21:03:24.117 回答