开发者

C# Restoring Values to Controls Iterativley

I have a situation here where the user has about 100 controls mostly multi select listboxes but some other stuff peppered in (drop downs checkboxes) and they narrow down complex search criteria for screenings. When they go back later they want the act of pulling up a record to reset the controls to the values that they had used to match the criteria.

So I made a table that has a column for each control and iterativley stores the values - comma deliminated for listboxes - when the user locks in the search criteria to move to the next step.

Other then using a switch to say if value is x: set control x to value(s) so and so i开发者_运维百科s there a good way to iterate through this, seeing as the name of the column is the name of the control ? I'm stumped at the moment ...

C# Restoring Values to Controls Iterativley

    var CParam = QueryFnc.RstrCntrls(Jnum, Qnum);
    foreach(var a in CParam)
    {
        //Map Values to Matching Named Control


    }


As long as your control tree remains constant, you can use control indices to map values. However better bet would be to use control IDs (if its .NET4 then go for predictable ids or manual id assignment for better controls) to map values.

Instead of storing values across columns in one row, I will prefer a table that will store values across rows. For example,

UserId  ColumnName  ControlID  Value
------  ----------  ---------  --------
1       ABC         ddlAbc     52
1       XYZ         ddlXyz     102, 32
...
2       XYZ         ddlXyz     23   

This will make things a lot simpler - get rows for the given user id and then iterate over rows. For each row, you can find the control using FindControl methods (you may have to roll up a recursive implementation in case you have naming containers in your control hierarchy) and then write simple switch statement to assign value to control based on control type.


You probably want to use reflection, and each control should have a factory method that inheits from a single interface, to allow for passing in the stored values.

With reflection you can find (and then instantiate) a control via it's name (as a string)

EDIT: just thinking out loud here... You might also be able to use the chain of responsibility pattern, passing the name of the column along through your list of classes, and it is each objects responsibility to catch the name it is responsible for, and return an instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜