WorkflowGen.com | Knowledge Base | Documentation | Downloads | Support | RSS

« Disable the Home Page counters to increase performance | Main | How can I build a sql query in to retrieve data from an external database? »

How to set the stored value to a dropdown list when the dropdown has a databinding happening during page load?

Posted on Sunday, March 16, 2008 at 16:47 by Registered CommenterWFG Team in | CommentsPost a Comment

Problem

WorkflowGen assigns values to your form fields BEFORE the Page_Load event occurs. So any databinding of a dropdown list (e.g. read values from database) that happens in your Page_load section will reset the selected value.


Solution

After you have re-"bound" your combo box (meaning your combo box has been re-populated with the options you retrieved from an external source), you must set the originally selected value manually.

After your dropdownlist databinding method is called, run the below method by passing the target dropdownlist as a parameter.

Add this method in your code:

C#

private void SetDropDownListSelectedValue(DropDownList TargetDDL)

{

     if (TargetDDL != null)

     {

          string WFGFieldName = TargetDDL.ID;

          string SelectedValue = FormData.Tables["Table1"].Rows[0][ WFGFieldName].ToString();

          ListItem li = null;

          li = TargetDDL.Items.FindByValue(SelectedValue);

          if (li != null)

          {

               int index = TargetDDL.Items.IndexOf(li);

               TargetDDL.SelectedIndex = index;

          }

     }

}

VB

Private Sub SetDropDownListSelectedValue(ByVal TargetDDL As DropDownList)

     If ((Not (TargetDDL) Is Nothing)) Then

          Dim WFGFieldName As String = TargetDDL.ID          
          Dim
SelectedValue As String = FormData.Tables("Table1").Rows(0)(WFGFieldName).ToString
         
          Dim
li As ListItem = Nothing
         
li = TargetDDL.Items.FindByValue(SelectedValue)

          If (Not (li) Is Nothing) Then

               Dim index As Integer = TargetDDL.Items.IndexOf(li) 
               TargetDDL.SelectedIndex = index

          End If

     End If

End Sub

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.
Member Account Required
You must have a member account on this website in order to post comments. Log in to your account to enable posting.