CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    [RESOLVED] should a WebControl be disposed or not?

    In my gridview rowdatabound event I have the following code

    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
    Code:
             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?

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: should a WebControl be disposed or not?

    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
    Code:
    string s1 = "It's me";
    //s2 is actually referring to s1 :)
    string s2 = s1;

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: should a WebControl be disposed or not?

    Ok clear. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured