You can create an additional aspx form in which the User (or Administrator) can define and create your own forms, you provide variable names and choose to add controls, save them in a specific scheme in the database, for example
UserForm: UserID FormID
Form: FormID FormName
FormElement: FormID VariableName IndexType Index
Of course, the administrator can also do this and be visible to everyone.
To view specific forms, you can add 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, after the user clicked the submit button, you can get the values entered into these controls by accessing divRight.FindControl (fe.ControlType + fe.Variable), as this should be unique for each form.
This approach assumes that you are using .NET 4.0 (because of the dynamic), but of course you can do it perfectly, without it, it will just be more code.
Please let me know if this is what you were looking for, or if it was helpful.
Thanks,
Dennis
source share