Issue
How can I default a drop-down value from WorkflowGen if the list is coming from a database?
Background
When a drop-down control is bound to a database to provide a list of selectable items, the list population occurs after WorkflowGen attempts to pre-select an item from that list. This is due to the .NET web form page lifecycle. Therefore, at the point when WorkflowGen attempts to select an item, the list does not yet exist. The result is a list of items, none of which are pre-selected.
There are two solutions proposed. The first is explained in the How to set the stored value to a dropdown list when the dropdown has a databinding happening during page load? article.
The second solution (described in this article) is to populate the drop-down list before the form is fully loaded, before WorkflowGen attempts to pre-select one of the list items. In essence, jump into the middle of the .NET web form page life-cycle.
Pros and cons
Each solution has its uses. The method in the linked article above is useful if you need to pre-select an item from a bound list that is dependent on another item on the web form. As well, this solution has access to the Form Data used and generated by WorkflowGen (all of the IN, OUT, INOUT parameters from WorkflowGen).
The second solution shown below is useful if you have lists that are bound but not dependent on other controls on the form since the .NET web form page life-cycle has not reached the point where the Form Data is initialized.
Example
To generate a drop-down list from a database before WorkflowGen gains access to the form, insert the following code to build the page constructor (this is assuming the pages class is called _Default
):
-
Add an event handler to the Initialization of the page:
/// <summary> /// Default constructor to set sessionless and post-back page position and any databound dropdown lists /// </summary> public _Default() { // Add event handler to the page this.Init += new EventHandler(_Default_Init); }
-
Add the method called
_Default_Init
(from the above step). -
Add the code to connect to a database and populate your drop-down list within this method:
void _Default_Init(object sender, EventArgs e) { // Add your code to connect to a Database and Populate the drop-downs required // Create Database connection object(s) // Execute the Database connection within a Try/Catch Statement for Error Trapping try { using (SqlConnection sqlConn = new SqlConnection(sConnStr)) { SqlCommand sqlCmd = new SqlCommand(sSql, sqlConn); sqlCmd.Connection.Open(); SqlDataReader sqlReader = sqlCmd.ExecuteReader(); while (sqlReader.Read()) { this.DropDownList1.Items.Add(sqlReader["columnname"].ToString()); } } } catch (Exception ex) { Response.Write("<br>ex = " + ex.Message); } }