views:

11

answers:

0

What is the best method or best practice for associating state information with an ASP.NET control?

The context is that I'm dynamically generating a user poll. I may have several polls to choose from that the user could answer. I will dynamically generate the label prompt, radio selections, validation, and submit button. Currently I am storing the ID as part of the control name.

In other words, I use the control name to relate the dynamically generated content to the proper database object. I do not like 2 aspects of this method:

  1. It feels like a hack, I am storing ID's as part the control name, a form of data mangling.
  2. It exposes some of the database structure.

I am more concerned with issue #1. Is there built-in control state-bag that is suitable for this? I know there is control state but expanding every control to hold my IDs is overkill. Is there anyway that I can use that control state to store my IDS without having to do sub-classing?

Perhaps, using Viewstate in this fashion would be the best option (from 4guysfromrolla):

  public int PollID
  {
    get 
    {
        if (ViewState["PollID"] == null)
            return -1;
        else
            return (int)ViewState["PollID"];
    }
    set { ViewState["PollID"] = value; }
}