Storing and retreiving information from Dictionary

Last post 06-03-2008 5:13 AM by Ralph Lawrence. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-02-2008 9:13 PM

    Storing and retreiving information from Dictionary

    Hi,


    I have a requirement of storing all the control information for the page as a collection of strings in a Dictionary object. Each item will be indexed by string using the Control ID. The final Dictionary object will be stored in the view state for the page. When the user will click the Button, the dictionary will be retrieved and all the information it contains will be displayed in a Label. Any idea about this implementation?

    Please help me if anyone has got the idea on implementing this functionality.

    Filed under:
  •  Advertisement

    Featured Advertisement

     
  • 06-03-2008 12:38 AM In reply to

    • Asirul
    • Top 200 Contributor
    • Joined on 02-10-2008
    • Dhaka,Bangladesh
    • Wannabe Slacker
    • Points 37

    Re: Storing and retreiving information from Dictionary

            Dictionary<string,string> controlInfo=new Dictionary<string,string>();
            foreach (Control control in Page.Controls)
            {
                /* storing client id in view state
                 * if you want to put more information of control, use custom object instead of string
                 */
                if(!string.IsNullOrEmpty(control.ID))
                    controlInfo.Add(control.ID,control.ClientID);
            }
            ViewState["controlInfo"]=controlInfo;

    You can use above code block to store the control information in view state.If you have controls in control, you need to build a recursive algorithm to get all the controll information.If you want to retrive control information from the view state, use the following code block.

            Dictionary<string, string> controlInfo = ViewState["controlInfo"] as Dictionary<string, string>;
            Dictionary<string,string>.Enumerator enumerator=controlInfo.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Label1.Text += enumerator.Current.Value;
            }

  • 06-03-2008 5:13 AM In reply to

    Re: Storing and retreiving information from Dictionary

    Thanks a lot for helping like this. Thank you very much.

    Filed under:
Page 1 of 1 (3 items)