c# - 如何在GridView EditTemplate中设置DropDownList的SelectedValue

标签 c# asp.net data-binding sqldatasource

我正在尝试做 this如前所述。我发现的唯一区别是上面代码中包含的额外列表项。

我尝试使用 AppendDataBoundItems=true但它仍然无法正常工作。我还想将其默认值设置为显示在项目模板标签中的值,即 DropDownList 的 SelectedValue='<%# Eval("DepartmentName") %>'但是我在下拉列表中无法使用该属性。 可能是什么原因。 ??

<EditItemTemplate>
    <asp:DropDownList ID="ddlDepartment_Edit" runat="server" 
        DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName" 
        DataValueField="PK_DepartmentId">
    </asp:DropDownList>
    <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"  
        ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" 
        SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>                                 
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
    </asp:Label>
</ItemTemplate>   

我正在使用 GridView

最佳答案

DataValueField似乎是错误的-不应该是DepartmentId吗? ?同样,你需要有 SelectedValue='<%# Eval("**DepartmentId**") %>' - DepartmentName将是 SeletectText .

关于c# - 如何在GridView EditTemplate中设置DropDownList的SelectedValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3474933/

相关文章:

c# - 来自 MemoryStream 的 fo-dicom JPEG 图像压缩

c# - 系统.全局化.CultureNotFoundException : Culture is not supported

asp.net - 下拉列表中的第一项丢失属性

c# - WPF:绑定(bind)到目录

c# - 在 WPF 双向绑定(bind)中,如何检查触发绑定(bind)更改的是 UI 元素还是 ViewModel?

c# - VB.NET下的Late Binding Magic转换为C#

c# - Linq 分组依据和排序依据

c# - 遗传算法的 "Hello World!"有什么用?

c# - 为什么我的保存使用我的文本框的初始值而不是输入的值?

c# - 来自列表<object> 的组合框 DisplayMemberPath