我的gridview有点问题。
在Editmode中,我从数据库中加载了一个下拉列表(外键!)。
当我保存更改并在editview中再次查看时,下拉列表显示表中的第一个条目,而不显示所保存值的值。当我进入编辑模式时,如何将下拉列表中的默认值设置为数据库中设置的默认值?

我希望我的担忧是可以理解的。非常感谢您的帮助!

我的代码(简称)

<asp:DetailsView ID="dtvProjektdetails" runat="server" DataSourceID="sqlProjectDetails" AutoGenerateRows="False" DataKeyNames="ProjectID" DefaultMode="Edit" onitemupdating="dtvProjektdetails_ItemUpdating">
<Fields>
   <asp:TemplateField HeaderText="Segment" SortExpression="FK_Segment">
      <EditItemTemplate>
         <asp:DropDownList ID="ddlSegment" runat="server" DataSourceID="sqlDSSegment" DataTextField="Segment" DataValueField="SegID">
         </asp:DropDownList>
      </EditItemTemplate>
   </asp:TemplateField>
</Fields>
</asp:DetailsView>


下拉列表的数据源(已缩短)

<asp:SqlDataSource ID="sqlDSSegment" runat="server" ConnectionString="<%$ ConnectionStrings:CSLinker %>" SelectCommand="SELECT * FROM [tabSegment] ORDER BY Segment]"></asp:SqlDataSource>


Detailview的数据源:

<asp:SqlDataSource ID="sqlProjectDetails" runat="server"
        ConnectionString="<%$ ConnectionStrings:CSLinker %>"
        SelectCommand="SELECT * FROM [tabProjects] WHERE ([ProjectID] = @ProjectID)"
        UpdateCommand="UPDATE [tabProjects] SET [Name] = @Name, [ProejctNo] = @ProejctNo, [Startdate] = @Startdate, [Enddate] = @Enddate, [FK_Owner] = @FK_Owner, [FK_Segment] = @FK_Segment, [FK_ProjectCategorie] = @FK_ProjectCategorie, [FK_ProjectFiles] = @FK_ProjectFiles, [FK_Mailings] = @FK_Mailings, [Botschaft] = @Botschaft, [Ziel] = @Ziel, [Callmenge] = @Callmenge, [Quote] = @Quote, [Zielgruppe] = @Zielgruppe, [Inboundnummer] = @Inboundnummer, [Wissen] = @Wissen, [Prozess] = @Prozess, [Goodies] = @Goodies, [FK_Sprachen] = @FK_Sprachen WHERE [ProjectID] = @ProjectID">
        <SelectParameters>
            <asp:ControlParameter ControlID="lblPID" Name="ProjectID" PropertyName="Text" Type="Int64" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="ProejctNo" Type="String" />
            <%--<asp:Parameter Name="Description" Type="String" />--%>
            <asp:Parameter Name="Startdate" Type="DateTime" />
            <asp:Parameter Name="Enddate" Type="DateTime" />
            <asp:Parameter Name="FK_Owner" Type="Int64" />
            <asp:Parameter Name="FK_Segment" Type="Int64" />
            <asp:Parameter Name="FK_ProjectCategorie" Type="Int64" />
            <asp:Parameter Name="FK_ProjectFiles" Type="Int64" />
            <asp:Parameter Name="FK_Mailings" Type="Int64" />
            <asp:Parameter Name="Botschaft" Type="String" />
            <asp:Parameter Name="Ziel" Type="String" />
            <asp:Parameter Name="Callmenge" Type="String" />
            <asp:Parameter Name="Quote" Type="String" />
            <asp:Parameter Name="Zielgruppe" Type="String" />
            <asp:Parameter Name="Inboundnummer" Type="String" />
            <asp:Parameter Name="Wissen" Type="String" />
            <asp:Parameter Name="Prozess" Type="String" />
            <asp:Parameter Name="Goodies" Type="String" />
            <asp:Parameter Name="FK_Sprachen" Type="Int64" />
            <%--<asp:Parameter Name="FK_Agenten" Type="Int64" />--%>
            <asp:Parameter Name="ProjectID" Type="Int64" />
        </UpdateParameters>
    </asp:SqlDataSource>

最佳答案

设置下拉列表的selectedvalue-属性:

<asp:DropDownList ID="ddlLinkCategorie" runat="server" DataSourceID="sqlDScategorie"
                            DataTextField="Categorie" DataValueField="LinCatID" selectedvalue='<%Bind("columnName")%>'>
                        </asp:DropDownList>

关于c# - 如何在editmode(detailview)中显示外键值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10894810/

10-10 13:15