本文介绍了有什么不好用的器WebControls“使用”块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用code使用分块 TableHeaderCell LiteralControl 超链接 GridViewRow try..finally )。在code正在为缩进。是否有任何问题/缺陷在使用块,如下图所示处置控制?如果是的话,你能不能提供任何参考MSDN,显示陷阱的详细信息?

 保护无效grdTransactions_RowCreated(对象发件人,GridViewRowEventArgs E)
    {

        如果(E!= NULL)
        {
            如果(e.Row.RowType == DataControlRowType.Header)
            {
                GridViewRow newHeaderRow = NULL;
                尝试
                {

                    newHeaderRow =新GridViewRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal);

                    使用(TableHeaderCell cellFirst =新TableHeaderCell())
                    {
                        cellFirst.ColumnSpan = 1;
                        cellFirst.Text =第一;
                        newHeaderRow.Cells.Add(cellFirst);
                    }


                    使用(TableHeaderCell cellAssociate =新TableHeaderCell())
                    {
                        GetTableCell(cellAssociate,tableColGroupAssociate; 4,associateHide,关联交易信息);
                        newHeaderRow.Cells.Add(cellAssociate);
                    }

                    newHeaderRow.Cells.Add(cellAssociate);
                    ((GridView的)发件人).Controls [0] .Controls.AddAt(0,newHeaderRow);

                }
                最后
                {
                    如果(newHeaderRow!= NULL)
                    {
                        newHeaderRow.Dispose();
                        newHeaderRow = NULL;
                    }
                }

            }
        }




    }
 

helper方法

 私有静态无效GetTableCell(TableHeaderCell cellAssociate,串cssClassName,诠释COLSPAN,串hideClassName,字符串显示名)
    {
        cellAssociate.ColumnSpan = COLSPAN;
        cellAssociate.CssClass = cssClassName;

        使用(LiteralControl ltlText =新LiteralControl())
        {
            ltlText.Text =显示名;
            cellAssociate.Controls.Add(ltlText);
        }

        使用(超链接lnkHide =新的超链接())
        {
            lnkHide.Text = SupportToolUIResource.HideLinkText;
            lnkHide.CssClass = hideClassName;
            lnkHide.Target = SupportToolUIResource.HideLinkTarget;
            cellAssociate.Controls.Add(lnkHide);
        }


    }
 

参考

  1. Why我需要调用Dispose ASP.NET控件?
解决方案

感谢@Tim梅多拉的说明和链接Why我需要调用Dispose ASP.NET控件?。

一些相关的要点是:

  1. 我们需要做的是确保新的控件添加在控件集合这样,当页面设置也将被全部销毁。
  2. 在控制对象实现了IDisposable接口。每个家长控制可以调用Dispose所有子
  3. 在任何实现IDisposable,并具有实际清理在处置过程中应该抛出的ObjectDisposedException 如果有公共/受保护/内部状态数据的正确书写物体属性方法它已被释放后访问。 (假设无效状态后处置被调用。)某些类型将忽略这条规则,如果他们实际上没有任何东西进行清理,并不必担心无效状态。

结论

没有必要使用使用块器WebControls。此外,如果使用块用于器WebControls它可能会导致问题。

I have following code that uses "using" block on TableHeaderCell, LiteralControl , HyperLink and GridViewRow (try..finally). The code is working as indented. Is there any issue/pitfall in disposing the controls with "using" block as shown below? If yes, can you provide any msdn reference that shows details of the pitfall?

    protected void grdTransactions_RowCreated(object sender, GridViewRowEventArgs e)
    {

        if (e != null)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                GridViewRow newHeaderRow = null;
                try
                {

                    newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

                    using (TableHeaderCell cellFirst = new TableHeaderCell())
                    {
                        cellFirst.ColumnSpan = 1;
                        cellFirst.Text = "FIRST";
                        newHeaderRow.Cells.Add(cellFirst);
                    }


                    using (TableHeaderCell cellAssociate = new TableHeaderCell())
                    {
                        GetTableCell(cellAssociate,"tableColGroupAssociate", 4, "associateHide", "Associate Transaction Info");
                        newHeaderRow.Cells.Add(cellAssociate);
                    }

                    newHeaderRow.Cells.Add(cellAssociate);
                    ((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);

                }
                finally
                {
                    if (newHeaderRow != null)
                    {
                        newHeaderRow.Dispose();
                        newHeaderRow = null;
                    }
                }

            }
        }




    }

Helper Method

    private static void GetTableCell(TableHeaderCell cellAssociate, string cssClassName, int colSpan, string hideClassName, string displayName)
    {
        cellAssociate.ColumnSpan = colSpan;
        cellAssociate.CssClass = cssClassName;

        using (LiteralControl ltlText = new LiteralControl())
        {
            ltlText.Text = displayName;
            cellAssociate.Controls.Add(ltlText);
        }

        using (HyperLink lnkHide = new HyperLink())
        {
            lnkHide.Text = SupportToolUIResource.HideLinkText;
            lnkHide.CssClass = hideClassName;
            lnkHide.Target = SupportToolUIResource.HideLinkTarget;
            cellAssociate.Controls.Add(lnkHide);
        }


    }

Reference:

  1. Why would I need to call dispose on ASP.NET Controls?
解决方案

Thanks to @Tim Medora for the explanation and the link Why would I need to call dispose on ASP.NET Controls? .

Some points of interest are:

  1. What we need to do is make sure that new controls are added is in the Controls’ collection so that it will be disposed when the Page is disposed.
  2. Control objects implement the IDisposable interface. Each parent control can call Dispose on all of its children
  3. Any properly-written object that implements IDisposable and has state data that is actually cleaned up during the dispose process should throw an ObjectDisposedException if any of its public/protected/internal properties or methods are accessed after it has been disposed. (Assume invalid state after Dispose has been called.) Some types will ignore this rule if they don't actually have anything to clean up, and don't have to worry about invalid state.

Conclusion

No need to use "using" block on webcontrols. Also, it may cause issues if "using" block is used on webcontrols.

这篇关于有什么不好用的器WebControls“使用”块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 09:50