Protector one Protector one - 4 months ago 17
ASP.NET (C#) Question

Create a variable in the scope of a databind

I'm databinding a Repeater and I would like to have access to a variable in the design code templates. The variable will contain some information for later use in the databinding action.

Ideally, the scope of the variable would be limited to the "databinding action". I could simply use a global variable, but hey, that would be cheating!

I tried using a script block to define the variable, but that doesn't work; the variable is created outside of the scope of the Repeater's templates.

<% bool x; %>
<asp:Repeater ID="suggestionRepeater" runat="server">
<ItemTemplate>
<%# x = false %> // x does not exist in current context
</ItemTemplate>
</asp:Repeater>


<asp:Repeater ID="suggestionRepeater" runat="server">
<HeaderTemplate>
<% bool x; %>
</HeaderTemplate>
<ItemTemplate>
<%# x = false %> // x does not exist in current context
</ItemTemplate>
</asp:Repeater>


So is this possible without taking the easy route and using a global (or protected) variable?

Answer

I think you can't. Not in the markup anyway. You can use a hiddenfield as another answer said (Update: The other guy removed it later!) and I usually when doing such set it visible false and viewstate false but hey, why the need for it at all...

You could do all sorts of fun in data binding events but this won't be much different as global variable.

But hey again, maybe you are trying to solve the wrong problem! Remember, this is .NET, not C++. Even if the variable is out of scope it'll be garbage collected only when needed. Even for a value type object, how big can it get?
Also, how much longer is the entire page life cycle compared to the repeater?

If the object is really "that" expensive and you "really" know in numbers a good reason you don't want it to life longer than the databinding, then it should also be disposable object (to guarantee proper clean-up of resource), and then I might do something like:

protected MyObjectType MyObject;

protected void BindRepeater(IEnumerable someDataSource)
{
    using(MyObject = SomeClass.SomeMethodToGetObject())
    {
        myRepeater.DataSource = someDataSource;
        myRepeater.DataBind();
    }
}

Here the using should guarantee the cleanup via calling Dispose() implicitly. Of course it's still dirty as someone can make a mistake and use MyObject in another place when it's disposed or not assigned even if I put comments and everything, and it IS a global variable after all - Yes, it is cheating, or working-around limitation.

Again, I'm not suggesting you do the code above. I just show it's possible if you really know in numbers why you want to do that (otherwise, it's called premature optimization, you might end up with same performance or even slightly less performance when trying to solve the problem, or at best get very insignificant performance increase). What I recommend here is to look at the problem again and make sure you really need this.

Another approach to working around this also is quite the opposite to minimizing lifetime, it is increasing lifetime actually, like putting the object in cache or somewe are talking about.thing so that you don't have to create it more than needed or store many identical big copies, but this of course depends on what kind of data