开发者

Adding TextBocex, CheckBoxes on Request C#

I will be starting a project soon at my company and the client would like to have the option in the por开发者_Go百科tal to add textboxes or checkboxes as an administrator,,, so for instance initially I may have something like

Name [textBox] Phone [textBox]


So the client would like to log in as an admin and be able to add

Name [textBox] Phone [textBox] Receive Brochure [checkBox] //added by client.

Forget about the portal and the admin part.. what I would like to know is what would be the best way to design this (the user to be able to add elements)

Any ideas would be much appreciated

Regards


You could create an additional aspx-form in which the User (or the Admin) is able to define and create his/her own forms, you supply the Variable names and they choose to add the controls, save it in a specific scheme in the Database, e.g.

UserForm: UserID FormID

Form: FormID FormName

FormElement: FormID VariableName ControlType Index

Of course this could also be done by an administrator and be visible by everyone.

To view the specific forms you could add yet another aspx-page containing the following code:

protected void Page_Load(object sender, EventArgs e)
    {
        //you saved the FormName or ID to Session when you accessed it
        string formName = Session["FormName"].ToString();
        //this handles getting the elements for this Form from DB
        List<FormElement> elementList = FormElement.GetForForm(formName);
        this.renderForm(elementList);
    }

    private void renderForm(List<FormElement> eList)
    {
        foreach(FormElement fe in eList)
        {
            //Labels left, Controls right, of course this is just a design decision
            if(fe.Index%2==1)
            {
                Label lbl = new Label();
                lbl.Text = fe.Variable;
                lbl.ID = fe.ControlType + fe.Variable;
                divLeft.Controls.Add(lbl);
            }
            else
            {
                dynamic ctrl = null;
                switch (fe.ControlType)
                {
                    case "TextBox":
                        ctrl = new TextBox();
                        break;
                    case "CheckBox":
                        ctrl = new CheckBox();
                        break;
                    default:
                        break;
                }
                ctrl.ID = fe.ControlType + fe.Variable;
                divRight.Controls.Add(ctrl);
            }
        }
    }

Later on after the User hitting submit you'd be able to receive the values entered into those Controls by accessing divRight.FindControl(fe.ControlType + fe.Variable) since that should be unique per Form.

This approach assumes you're using .NET 4.0 (because of dynamic), but of course you can do this just fine without it, it'll just be more code.

Please let me know if this is what you searched for or if it was helpful.

Thanks,

Dennis


From what I have done in the past where I had to create a dynamic survey based on a client needs. I had a database that contained the following tables:

  1. Survey - stores list of client surveys.
  2. Controls - listed the type of controls that would be needed. For example, textbox, checkbox etc.
  3. Survey Controls - links all the controls required for a survey.
  4. User values - stores the values entered in a controll based on the survey used.

I then added some logic to dynamically create the controls based on a survey selected by reading the values from my database.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜