Setting Form Values Automatically with SubSonic
Tedious coding sucks. SubSonic saves lots of time. But I want it to save me more. So I've started working on a method that will fill out the controls for me so I don't have to. It's simple really. I just new() up a the model I will use and pass it to my method along with the UserControl I have my form elements in.
1: public void SetupForm<T>(Control form, T activeRecord )
2: where T : AbstractRecord<T> ,new()
3: { 4: TableSchema.TableColumnSettingCollection settings = activeRecord.GetColumnSettings();5: foreach (TableSchema.TableColumnSetting setting in settings)
6: { 7: Control settingControl = form.FindControl(setting.ColumnName);8: if (settingControl is TextBox)
9: ((TextBox)settingControl).Text = 10: activeRecord.GetColumnValue<string>(setting.ColumnName);
11: if (settingControl is DropDownList)
12: ((DropDownList)settingControl).SelectedValue = 13: activeRecord.GetColumnValue<string>(setting.ColumnName);
14: } 15: }The key thing is to make sure you have called EnsureChildControls() before calling this method.
So in my control the code looks like
1: int issueId = SubSonic.Sugar.Web.QueryString<int>("issue");
2: if (issueId != 0)
3: { 4: EnsureChildControls();5: Issue issue = new Issue(issueId);
6: //BasePage inherits from System.Web.UI.Page
7: BasePage.SetupForm(this, issue);
8: } Now this is pretty rough and more than anything a proof of concept so your mileage may vary. But I figure this is a good place to start