views:

103

answers:

1

Hi there,

Using Visual Web Developer Express 2010 with ASP.NET 4.0.

I have a FormView and I want to set a default value from the database. I can't get it to bind to the value in the database. My FormView looks like this:

<asp:FormView 
ID="frmOrderDetails" 
DataSourceID="sdsFormOrderDetails" 
runat="server" 
DataKeyNames="orderId">

<EditItemTemplate>
    <h3>Edit Order Details</h3>

    <asp:Label ID="lblStrategy" Text="Strategy:" AssociatedControlID="ddlStrategies" runat="server"  />
    <asp:DropDownList SelectedValue='<%# Bind("strategyId") %>'
    ID="ddlStrategies" 
    runat="server" 
    DataTextField="strategy" 
    DataValueField="strategyId" 
    DataSourceID="sdsStrategies"
     />

    <asp:LinkButton
    id="lnkUpdate"
    Text="Update Order"
    CommandName="Update"
    Runat="server" />
    |
    <asp:LinkButton
    id="lnkCancel"
    Text="Cancel"
    CommandName="Cancel"
    Runat="server" />

</EditItemTemplate>
</asp:FormView>

<asp:SqlDataSource ID="sdsFormOrderDetails" runat="server" 
    ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>" 
    ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"  
    SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure" 
    UpdateCommand="usp_UpdateOrder" UpdateCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
    </SelectParameters>
    <UpdateParameters>
        <asp:ControlParameter Name="orderId" ControlID="grdOrders" />
    </UpdateParameters>
</asp:SqlDataSource>                     

<asp:SqlDataSource ID="sdsStrategies" runat="server"
    ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>" 
    ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"  
    SelectCommand="usp_GetStrategiesDropDown">   
</asp:SqlDataSource>    

The EditItemTemplate does not even load when I click the edit button on my FormView control, but I don't get an error message either.

A: 

you need to use formview Databound event like

 protected void frmOrderDetails_DataBound(object sender, EventArgs e)
{
    if (frmOrderDetails.CurrentMode == FormViewMode.Edit)
    {
        DropDownList ddlStrategies = (DropDownList)frmOrderDetails.FindControl("ddlStrategies");
        ddlStrategies.SelectedValue = Your DB Value Goes here;
    }
}
Muhammad Akhtar
Thanks for the reply. Is there a way to do this without using code behind?
Mark Allison
nop, that's what I have told you, otherwise SelectedValue='<%# Bind("strategyId") %>' this method will work when you bind that particular value from same source
Muhammad Akhtar
Ah ok, thanks. How do I get the DB value? I just want the current strategyId that was displayed in the ItemTemplate before the Edit button was clicked. I'm quite new to ASP.NET - appreciate any help.
Mark Allison