0

我在这里阅读了许多关于从动态添加的数据网格列中回发数据的主题。我读过的所有内容都表明,由于这些列是在 Init 阶段之后添加的,因此它们在回发时在控件集合中不可用,因此这些动态添加的列回发的任何数据都不可用,并且无法从 ViewState 恢复,除非它们是在 Page_Init 中重新创建。我已经读过,只要使用相同的 IDS 重新添加控件(即 IDS 应该与回发的控件 id 匹配),它们就会从 ViewState 中恢复。

如果我不是动态添加网格列而是在后面的代码中删除一些网格列怎么办。因此,remmin 的列的 ID 应该与控件集合中的内容相匹配,因此任何回发的数据都应该正确恢复,但这不是我所看到的。假设以下是我的 ASPX 页面中的列列表。如果在我后面的代码中,我删除了“测试”列,但选中了第一列中的复选框。回发时,不会保留复选框选择。我本来希望它基于我所读的内容。有人可以纠正我对此的理解吗?

<Columns>
               <asp:TemplateColumn headertext="" headerstyle-width="1%" itemstyle-horizontalalign="center" FooterStyle-CssClass="DataGridFooter" FooterStyle-HorizontalAlign="Center" FooterStyle-VerticalAlign="Top">
                    <HeaderTemplate>
                        <input id="checkAll" type="checkbox" onclick="CheckAllDataGridCheckBoxes('checkboxIsSelected',this.checked)"/>
                    </HeaderTemplate>
                    <itemtemplate>
                        <asp:CheckBox runat="server" id="checkboxIsSelected" checked='false' />
                    </itemtemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="Subject" HeaderStyle-Width="10%" Visible="TRUE" ItemStyle-HorizontalAlign="Center" FooterStyle-CssClass="DataGridFooter" FooterStyle-HorizontalAlign="Center" FooterStyle-VerticalAlign="Top" >
                    <ItemTemplate>
                        <asp:Label id="labelSubjectID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SubjectID") %>' />
                    </ItemTemplate> 
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="Test"  HeaderStyle-Width="5%" Visible="TRUE" ItemStyle-HorizontalAlign="Center" FooterStyle-CssClass="DataGridFooter" FooterStyle-HorizontalAlign="Center" FooterStyle-VerticalAlign="Top" >
                    <ItemTemplate>
                        <asp:Label id="labelTestCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TestCode") %>' />
                    </ItemTemplate>             
                </asp:TemplateColumn>
</Columns>

添加了 BindGrid() 和事件处理程序 buttonApprove_click。SetGridColumns 方法是删除网格列的方法。也包括在内。

private void BindGrid()
    {
        try
        {
            SetMasterDetailControlStates(IsPageInDetailsMode());

            if (!ValidateArgs())
            {
                gridBias.Visible = false;
                return;
            }

            foreach (ListItem displayOption in chkboxDisplay.Items)
            {
                if (displayOption.Selected)
                {
                    if (displayOption.Value == "Highlight") { _showHighlighting = true; }
                }
            }

            /*
              <Snipped>...Make procedure call to get dataset
            */

            DataSet ds = sp.GetDataSet();

            if (sp.RC != (int)ReturnCode.SUCCESS)
            {
                if ((sp.RC == (int)ReturnCode.NO_RECORDS_RETURNED))
                {
                    labelMessage.Text = "No data found for the specified filter";
                }
                else if ((sp.RC == (int)ReturnCode.UNHANDLED_EXCEPTION) || (ds == null))
                {
                    labelMessage.Text = sp.ErrorMessage;
                }
                ds = null;
                gridBias.DataSource = ds;
                EnableControls(false);
                SetGridColumns();
            }
            else
            {
                gridBias.TitleText = BuildGridTitleString();
                gridBias.FilterText = BuildGridFilterString();
                gridBias.EnableRowsPerPageEdit = true;
                SetGridColumns();

                //Since grid columns are dynamically added and can change between postbacks
                //make sure sort column is valid, if not unset
                if (gridBias.SortColumn != null && gridBias.SortColumn.Length > 0)
                {
                    bool foundCol = false;
                    foreach (DataGridColumn dg in gridBias.Columns)
                    {
                        if (gridBias.SortColumn == dg.SortExpression)
                        {
                            foundCol = true;
                            break;
                        }
                    }
                    if (!foundCol)
                    {
                        gridBias.SortColumn = "";
                        gridBias.SortOrder = "";
                    }
                }

                if (gridBias.SortColumn.Length == 0) gridBias.SortColumn = "TestCode";
                if (gridBias.SortOrder.Length == 0) gridBias.SortOrder = "ASC";
                if (gridBias.PageIndex.Length != 0 &&
                            !gridBias.GridViewMode.Equals(CustomDataGrid.GridViewType.SHOWALL.ToString()))
                    gridBias.CurrentPageIndex = Convert.ToInt32(gridBias.PageIndex);
                ds.Tables[0].DefaultView.Sort = gridBias.SortColumn + " " + gridBias.SortOrder;
                gridBias.DataSource = ds.Tables[0].DefaultView;

                EnableControls(true);
            }
            gridBias.HideEmptyColumns = true;
            ArrayList cols = new ArrayList();
            cols.AddRange(_alRecheckColumns);
            gridBias.HideEmptyColumnsList = cols;
            gridBias.DataBind();
        }
        catch (Exception ex)
        {
            CodeLibrary.ErrorHandling.SetLabel("Error(" + CodeLibrary.ErrorHandling.GetMethodName() + ")", ex.Message, labelMessage, true, "ErrorMsg");
        }
    }

    private void SetGridColumns()
    {
        ArrayList alCols = new ArrayList();
        alCols.Add(""); //Checkbox column
        alCols.Add("Subject");

        ArrayList alColumnsToBeDeleted = new ArrayList();
        Boolean boolColExists = false;
        foreach (ListItem chart in chkboxChartType.Items)
        {
            if (chart.Value == "%Bias")                 
            {
                if (rblBiasOptions.SelectedValue == "Subject")
                {
                    foreach (DataGridColumn dg in gridBias.Columns)
                    {
                        boolColExists = false;
                        foreach (string gridColumn in alCols)
                        {
                            if (dg.HeaderText == gridColumn)
                            {
                                boolColExists = true;
                                break;
                            }
                        }

                        if (!boolColExists)
                        {
                            alColumnsToBeDeleted.Add(gridBias.Columns.IndexOf(dg));
                        }                        
                    }
                }
           }
        // Loop thru the array list and delete columns from the grid.
        int count = 0;
        foreach (int i in alColumnsToBeDeleted)
        {
            gridBias.Columns.RemoveAt(i - count); //Subtract index by columns already     deleted.
            count++;
        }

    }

protected void buttonApprove_Click(object sender, EventArgs e)
    {
        try
        {
            CheckPermission(this.ModuleId, this.GetType().BaseType.ToString(), "buttonApprove_Click");
            foreach (DataGridItem i in gridBias.Items)
            {
                //Loop thru each row and determien values to be saved to DB
                if (i.ItemType == ListItemType.Item || i.ItemType == ListItemType.AlternatingItem)
                {
                    bool cbSelected = ((CheckBox)i.FindControl("checkboxIsSelected")).Checked;

                    if (cbSelected)
                    {
                        bool IsApproved = true;
                        string strSubjectID = ((System.Web.UI.WebControls.Label)i.FindControl("labelSubjectID")).Text;
                        string strTestCode = ((System.Web.UI.WebControls.Label)i.FindControl("labelTestCode")).Text;
                        string strSampleType = ((System.Web.UI.WebControls.Label)i.FindControl("labelSampleType")).Text;
                        string strTestCodeAndType = ((System.Web.UI.WebControls.Label)i.FindControl("labelTestCodeAndType")).Text;
                        if (((System.Web.UI.WebControls.Label)i.FindControl("labelRecheckNeedsApproval")).Text == "1")
                            IsApproved = true;
                        else
                            IsApproved = false;

                        SaveToDB(strSubjectID, strTestCode, strSampleType, strTestCodeAndType,IsApproved);
                    }
                }
            }

            BindGrid();
        }
        catch (Exception exceptionObject)
        {
            string strExceptionMessage = exceptionObject.Message + "-" + exceptionObject.InnerException.Message;
            CodeLibrary.ErrorHandling.SetLabel("", strExceptionMessage, labelMessage, false, "ErrorMsg");   //--- Set label text
        }
    }
4

0 回答 0