Click to See Complete Forum and Search --> : [RESOLVED] should a WebControl be disposed or not?


dannystommen
August 28th, 2009, 04:33 AM
In my gridview rowdatabound event I have the following code


if (e.Row.RowType == DataControlRowType.DataRow) {
line_item item = e.Row.DataItem as line_item;

if (!item.active)
(e.Row.FindControl("linkName") as HyperLink).CssClass = "inactive";
(e.Row.FindControl("linkName") as HyperLink).NavigateUrl = "someUrl.aspx?id=" +
item.id;
//do some more stuff with linkName
}

To improve its readablity in changed it to

if (e.Row.RowType == DataControlRowType.DataRow) {
line_item item = e.Row.DataItem as line_item;

HyperLink linkName = (e.Row.FindControl("linkName") as HyperLink);
if (!item.active)
linkName.CssClass = "inactive";
linkName.NavigateUrl = "someUrl.aspx?id=" + item.id;
//do some more stuff with linkName

// linkName.Dispose(); ????
}

My question, should the created Hyperlink 'linkName' be disposed or not?

Shuja Ali
August 28th, 2009, 03:40 PM
Not required. You are just creating a reference to an object that has already been created and you don't need to dispose off an object that is used in the page.

HyperLink linkName = (e.Row.FindControl("linkName") as HyperLink);
, isn't this statement same as string s1 = "It's me";
//s2 is actually referring to s1 :)
string s2 = s1;

dannystommen
August 31st, 2009, 10:12 AM
Ok clear. Thanks