如何在.net updatepanel中更新动态表行?

时间:2011-12-12 23:42:51

标签: asp.net dynamic updatepanel

我有一个存储表的updatepanel。该表为各种属性字段动态生成输入元素行:

foreach (DataRow propertyRowToDraw in temporaryTableOfProperties.Rows)
    {
        // Create the property row
        TableRow propertyRow = new TableRow();

        // Create the first cell, and give it a value of the property ID, but don't display it
        TableCell propertyIDColumn = new TableCell();
        propertyIDColumn.Text = propertyRowToDraw["PropertyId"].ToString();
        propertyIDColumn.Visible = false;
        propertyRow.Cells.Add(propertyIDColumn);

        // Create the second cell and give it a value of the text, or prompt, for that property
        TableCell propertyNameColumn = new TableCell();
        propertyNameColumn.ID = "propertyName" + propertyRowToDraw["PropertyId"].ToString();
        propertyNameColumn.Text = propertyRowToDraw["Prompt"].ToString();
        propertyNameColumn.Width = Unit.Percentage(15);
        propertyRow.Cells.Add(propertyNameColumn);


        // Not sure what this does
        TableCell propertyHiddenValuesColumn = new TableCell();
        propertyHiddenValuesColumn.ID = "hiddenValues" + propertyRowToDraw["PropertyId"].ToString();
        propertyHiddenValuesColumn.Attributes.CssStyle.Add("display", "none");
        HiddenField hiddenPropertyDataType = new HiddenField();
        hiddenPropertyDataType.Value = propertyRowToDraw["DataType"].ToString();
        propertyHiddenValuesColumn.Controls.Add(hiddenPropertyDataType);
        propertyRow.Cells.Add(propertyHiddenValuesColumn);

        // Create a new cell for the property data type
        TableCell propertyDataTypeColumn = new TableCell();
        propertyDataTypeColumn.ID = "propertyDataType" + propertyRowToDraw["PropertyId"].ToString();

        // Create a dropdown list for the property data type for this cell
        DropDownList inquiryTypeSelection = new DropDownList();
        inquiryTypeSelection.Width = Unit.Percentage(100);

        // Cast it to the propertyDataType enum and do a switch to determine what items to add to the dropdown
        switch ((Altec.Framework.PropertyDataType)Convert.ToInt32(propertyRowToDraw["DataType"]))
        {
            case PropertyDataType.Date:
                inquiryTypeSelection.Items.Add(new ListItem("Exactly", "2"));
                inquiryTypeSelection.Items.Add(new ListItem("Greater Then", "3"));
                inquiryTypeSelection.Items.Add(new ListItem("Less Then", "4"));
                inquiryTypeSelection.Items.Add(new ListItem("Range", "5"));
                break;

            case PropertyDataType.Boolean:
                inquiryTypeSelection.Items.Add(new ListItem("Exactly", "2"));
                break;

            case PropertyDataType.Currency:
                inquiryTypeSelection.Items.Add(new ListItem("Any", "1"));
                inquiryTypeSelection.Items.Add(new ListItem("All", "0"));
                inquiryTypeSelection.Items.Add(new ListItem("Greater Then", "3"));
                inquiryTypeSelection.Items.Add(new ListItem("Less Then", "4"));
                inquiryTypeSelection.Items.Add(new ListItem("Range", "5"));
                break;

            case PropertyDataType.Double:
                inquiryTypeSelection.Items.Add(new ListItem("Any", "1"));
                inquiryTypeSelection.Items.Add(new ListItem("All", "0"));
                inquiryTypeSelection.Items.Add(new ListItem("Greater Then", "3"));
                inquiryTypeSelection.Items.Add(new ListItem("Less Then", "4"));
                inquiryTypeSelection.Items.Add(new ListItem("Range", "5"));
                break;

            case PropertyDataType.String:
                inquiryTypeSelection.Items.Add(new ListItem("Any", "1"));
                inquiryTypeSelection.Items.Add(new ListItem("All", "0"));
                break;
        }

        // Add the dropdown to the cell and then add the cell to the row
        propertyDataTypeColumn.Width = Unit.Percentage(15);
        propertyDataTypeColumn.Controls.Add(inquiryTypeSelection);
        propertyRow.Cells.Add(propertyDataTypeColumn);


        // Create the cell that will hold the input box
        propertyIDColumn = new TableCell();
        propertyIDColumn.ID = "propertyInputColumn" + propertyRowToDraw["PropertyId"].ToString();

        // Create the textbox input that will hold the search value for that property row
        TextBox propertyTextInput = new TextBox();
        propertyTextInput.ID = "propertyInputText" + propertyRowToDraw["PropertyId"].ToString();
        propertyIDColumn.Controls.Add(propertyTextInput);
        propertyTextInput.Width = Unit.Percentage(92);
        // Add it to the row
        propertyRow.Cells.Add(propertyIDColumn);

        // Add the row to the overall table
        docTypePropertiesTable.Rows.Add(propertyRow);


    }

如何通过updatepanel将值输入到服务器端的那些文本框(propertyTextInput)?出于某种原因,该表在回发时不会显示在视图状态中 - 即使我强制viewstatemode = enabled。

我必须动态生成行,因为根据页面上的其他输入元素有可变数量的行。

完全没有想法。

2 个答案:

答案 0 :(得分:0)

动态添加控件的是每次回发都必须读取,否则无法在服务器端访问它们。

您是否只添加一次控件!IsPostBack?如果是,则删除!IsPostBack检查并在每次回发时读取控件。

如果不是这样,那么请发布更新面板的aspx代码。

答案 1 :(得分:0)

如果您有一行有限的元素,例如10,您可以在设计时在表格中创建10个空白行,然后从页面动态填充它们。如果您需要动态行数,我相信您需要将日期存储在viewstate中 - 我有相同的问题,所以应该能够在一段时间内发布一些示例代码。