【问题标题】:How to update dynamic table rows in .net updatepanel?如何更新.net updatepanel中的动态表行?
【发布时间】:2012-01-18 22:22:27
【问题描述】:

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

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);


    }

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

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

新鲜出炉。

【问题讨论】:

    标签: asp.net dynamic updatepanel


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      动态添加控件的问题是必须在每次回发时重新添加,否则它们将无法在服务器端访问。

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

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

      【讨论】:

        猜你喜欢
        • 2010-09-26
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-16
        • 1970-01-01
        相关资源
        最近更新 更多