我正在尝试做 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/