gpt4 book ai didi

c# - 在 Webcontrols 上使用 “Using” block 有什么问题?

转载 作者:太空狗 更新时间:2023-10-30 00:42:51 25 4
gpt4 key购买 nike

我有以下代码在 TableHeaderCellLiteralControlHyperLink 上使用“using” block >GridViewRow(try..finally)。代码按缩进方式工作。如下所示,使用“using” block 处理控件是否存在任何问题/陷阱?如果是,您能否提供任何显示陷阱详细信息的 msdn 引用?

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

}
}




}

辅助方法

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


}

引用:

  1. Why would I need to call dispose on ASP.NET Controls?

最佳答案

即使您“完成”了控件,页面也会在呈现时访问该控件,因此在构造期间处理它们没有多大意义。在您的一个示例中,您正在使用您刚刚处理的控件之一,这也没有意义。

Call Dispose when you are finished using the Control. The Dispose method leaves the Control in an unusable state. After calling this method, you must release all references to the control so the memory it was occupying can be reclaimed by garbage collection.

控件引发的 Disposed 事件的描述也暗示了预期的用途:

Occurs when a server control is released from memory, which is the last stage of the server control lifecycle when an ASP.NET page is requested.

来源:http://msdn.microsoft.com/en-us/library/system.web.ui.control.dispose.aspx
来源:http://msdn.microsoft.com/en-us/library/system.web.ui.control.disposed.aspx
另请参阅:https://stackoverflow.com/a/3151072/453277

所以理论上:

  1. 建立控制树。
  2. 页面开始呈现。
  3. Page 查看添加到树中的控件。
  4. 控件已被处置。
  5. 潜在问题。

这里是 IDisposable 的实现(来自 Control)。请注意它如何改变容器和事件相关的值。

public virtual void Dispose()
{
if (this.Site != null)
{
IContainer container = (IContainer)this.Site.GetService(typeof(IContainer));
if (container != null)
{
container.Remove(this);
EventHandler eventHandler = this.Events[Control.EventDisposed] as EventHandler;
if (eventHandler != null)
{
eventHandler(this, EventArgs.Empty);
}
}
}
if (this._occasionalFields != null)
{
this._occasionalFields.Dispose();
}
if (this._events != null)
{
this._events.Dispose();
this._events = null;
}
}

何时处置

这并不是说您不应该处置您的资源;如果一个控件需要释放资源,那么它当然是自由的。也许一个控件访问一个数据库;我会将数据库代码包装在 using block inside 控件中。

在实践中,这种风格为一些可以更简单地表达的东西创建了一大块代码。

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

// could become
cellAssociate.Controls.Add( new LiteralControl { Text = displayName } );

关于c# - 在 Webcontrols 上使用 “Using” block 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13448986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com