简体   繁体   中英

Check if Usercontrol previously loaded on page

Is there a way, within a .Net user control, to check if the control has been previously loaded on the page?

I thought about trying storing a variable in a .Net session variable to track each control as it loads, then disposing of the variable in the page pre-render event. It seems to me that there has to be a better way to accomplish this.

The reason I need to do this is because the usercontrols are loaded as sublayouts within sitecore, and I have no idea how many times a content author may have added the control to the page. If it is the first time the control is being called to the page I need to add a write out a div tag for a 3rd party service. If the div tag occurs more than once, the 3rd party service, gawks.

Just use HttpContext.Current.Items to keep the information about loaded control.

As the name indicates, HttpContext.Current is only for a single web request. No need to worry about disposing or clearing old data.

if (HttpContext.Current.Items["I was here"] == null) 
{
    // do custom div magic
    HttpContext.Current.Items["I was here"] = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM