2

I am trying to save all the controls from one WebForm using a serialized Dictionary (controlId - string, controlValue - string). Next i want to deserialize that Dictionary and dynamically fill the controls with their values.

The problem is that some of my controls have AutoPostBack true and also event handlers. Is there a way to dynamically call these event handlers? I want to avoid another switch by control's id.

Ex:

foreach (KeyValuePair<string, string> kvp in dict)
{
    Control c = findControlRecursive(Page, kvp.Key);
    if (c != null)
    {
        switch (c.GetType().Name)
        {
            case "TextBox":
                ((TextBox)c).Text = kvp.Value;
                if (((TextBox)c).AutoPostBack) .......

EDIT:

Let's say that I have 10 different forms. each has about 50 controls. I want to add/edit a set of data. I try to avoid tables in database with columns for each control, that's why I want to use serialization.

2
  • All these controls and their values are available to you on the postback anyway? Commented May 31, 2012 at 12:59
  • Yes... I retrieve the serialized dictionary from database and i deserialize it... Commented May 31, 2012 at 13:01

2 Answers 2

1

From my point of view you are trying to reinvent a wheel. ASP.NET provides the WebUserControl class which can be used on different pages and works as a container for a set of controls. You can define event handlers for these controls and they will be called appropriately regardless the page you are using them on.

So, I would suggest that you use the WebUserControl based approach without serializing / deserializing controls. This is an easy and effective solution.

Sign up to request clarification or add additional context in comments.

5 Comments

this form/page has lots of controls... i want to save their values in a database and then to load them back, but without creating tables with columns for each field...
OK, if so, UserControl is a way to go. You can set control values manually and events will work properly :). You should just keep all your controls on the UserControl.
i don't think UserControl is what i need... i need to have these values in a database
Page.aspx?Id=125 should fill my form with values from database. I try to avoid creating a table(Id, Control1, Control2, Control3... Control50), that's why i was thinking at dynamic serialize the entire form, dynamic deserialize it and fill the form. I managed to fill the controls with their values, but some of those controls have eventhandlers that should fire once they have values. (sorry for my english)
...can you tell me how to obtain information about event handlers associated with controls? for example, the event name a DropDownList should fire on SelectedIndexChanged
0

I found it later... i was looking for:

if (((TextBox)c).AutoPostBack) (c as IPostBackDataHandler).RaisePostDataChangedEvent();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.