如何在FormView控件的EditItemTemplate中绑定dropdownlist?

发布于 2024-09-13 17:01:12 字数 2044 浏览 7 评论 0原文

将 Visual Web Developer Express 2010 与 ASP.NET 4.0 结合使用。

我有一个 FormView,我想从数据库设置默认值。我无法将其绑定到数据库中的值。我的 FormView 看起来像这样:

<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>    

当我单击 FormView 控件上的编辑按钮时,EditItemTemplate 甚至不会加载,但也没有收到错误消息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2024-09-20 17:01:12

您需要执行以下操作将 DropDownList 绑定到 EditItemTemplate

  1. DropDownList 及其 DataSource 添加到编辑项目模板
  2. 设置DropDownList参数:

    DataSourceID="SqlDataSourceDropDownlist" SelectedValue=<%# Bind("ValueToBind") %>
    DataTextField="ValueToDisplay" DataValueField="ValueToBind"
    
  3. 设置 ListView / FormView 数据源:

You need to do the following to bind a DropDownList to the EditItemTemplate:

  1. Add the DropDownList and its DataSource to the EditItemTemplate.
  2. Set the DropDownList parameters:

    DataSourceID="SqlDataSourceDropDownlist" SelectedValue=<%# Bind("ValueToBind") %>
    DataTextField="ValueToDisplay" DataValueField="ValueToBind"
    
  3. Set the ListView / FormView DataSource <UdateParameters>: <asp:Parameter Name="RankID" Type="Int32" />

隱形的亼 2024-09-20 17:01:12

您需要使用 formview Databound 事件,例如

 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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文