Home
 Index > ASP.NET Web Applications > How to DataBind a Wizard control to an ObjectDa...

ASP.NET Web Applications:  Databinding , SqlDataSource , FormView , ObjectDataSource , Wizard

How to DataBind a Wizard control to an ObjectDataSource

Although the Wizard ASP.NET web control cannot be bound directly to an ObjectDataSource, you can use the Wizard in a FormView, thereby databinding the Wizard indirectly.

Added on 5 Aug 2008

General Solutions:

Solution Summary:
Add a wizard to a user control and insert the user control into a FormView control.
Solution Details:

If you have tried to use the Wizard ASP.NET web control for more than just inserting data, you were probably shocked to find out that it's not bindable. Why shouldn't we be able to use the Wizard the same way we use FormView? If we use a Wizard to insert data, wouldn't we want to view that data and edit it the same way? How could Microsoft have overlooked this? Well, who knows?

The best workaround I could come up with is not as simple as I'd like (after all the whole purpose of binding controls to an ObjectDataSource is to simplify matters); but it does work to a degree:

  1. Create a User Control 
  2. Add the Wizard to the User Control 
  3. Define properties for the User Control to expose the web controls that need to be bound 
  4. Create an ASPX page and add a Formview and an ObjectDataSource 
  5. Drag the User Control (from step a above) into the FormView's EditItemTemplate 
  6. Bind the properties of the User Control to the item data (e.g. Name='Bind("name")')

You should also set the FormView's InsertItemTemplate to the EditItemTemplate in the page load event (to avoid redundancy):

FormView1.InsertItemTemplate = FormView1.EditItemTemplate

Sample ASPX (ASP.NET Page):

<asp:FormView ID="FormView1" Width="100%" runat="server" DataSourceID="ObjectDataSource1" DefaultMode="Insert">
        <EditItemTemplate>
            <uc1:UcWizard ID="UcWizard1" runat="server" 
                
Title='<%#Bind("Name")%>' 
                
Details='<%#Bind("Description")%>'
                Category='<%#Bind("CategoryId")%>' 
                
AddressLine1='<%# Bind("AddressLine1")%>'
                AddressLine2='<%#Bind("AddressLine2")%>'
                City='<%#Bind("City")%>' 
                
State='<%#Bind("State")%>' 
                
ZipCode='<%#Bind("ZipCode")%>'
                 />
        </EditItemTemplate>
    </asp:FormView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        
SelectMethod="GetAsset" InsertMethod="InsertAsset" TypeName="MyApp.DataAccess.Db_Assets" 
        
UpdateMethod="UpdateAsset">
        </asp:ObjectDataSource>

 

Sample ASCX (User Control):

 

 <asp:Wizard ID="Wizard1" runat="server" ActiveStepIndex="0"  
        
BackColor="#F7F6F3"  
        
Font-Names="Verdana" CellPadding="10" Height="100%">
        <StepStyle BorderWidth="0px" ForeColor="#5D7B9D" />
        <NavigationStyle HorizontalAlign="Left" />
        <WizardSteps>
            <asp:WizardStep runat="server" Title="Enter Location" ID="step_location">
            <table>
                <tr>
                    <td></td>
                    <td>Business Name:</td>
                    <td><asp:TextBox ID="tbBusiness" runat="server" Width="182px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Address Line 1:</td>
                    <td><asp:TextBox ID="tbAddress1" runat="server" Width="310px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Address Line 2:</td>
                    <td><asp:TextBox ID="tbAddress2" runat="server" Width="310px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>City:</td>
                    <td><asp:TextBox ID="tbCity" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>State:</td>
                    <td><asp:DropDownList ID="ddlStates" runat="server" DataSourceID="sds_States" DataTextField="StateName" DataValueField="StateId"></asp:DropDownList>
                        <asp:SqlDataSource ID="sds_States" runat="server" ConnectionString="<%$ ConnectionStrings:connString %>" 
                            
SelectCommand="SELECT [StateId], [StateName] FROM [app_States] ORDER BY [StateName]">
                        </asp:SqlDataSource>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Zip Code:</td>
                    <td><asp:TextBox ID="tbZipCode" runat="server" MaxLength="5" Width="87px"></asp:TextBox></td>
                </tr>
            </table>    
            
</asp:WizardStep>
        </WizardSteps>
    </asp:Wizard>

 

ASCX.VB (Set Properties):

 


    Public Property Business() As String
        Get
            Return tbBusiness.Text
        
End Get
        Set(ByVal value As String)
            tbBusiness.Text = value
        
End Set
    End Property

    Public Property AddressLine1() As String
        Get
            Return tbAddress1.Text
        
End Get
        Set(ByVal value As String)
            tbAddress1.Text = value
        
End Set
    End Property

    Public Property AddressLine2() As String
        Get
            Return tbAddress2.Text
        
End Get
        Set(ByVal value As String)
            tbAddress2.Text = value
        
End Set
    End Property
Was this solution useful? Yes No Added on 5 Aug 2008
Rating: 

Copyright 2010 © E-Centric, Inc. | Terms of Use