简体   繁体   English

如何在C#的下拉列表控件中设置数据文本值和数据文本字段

[英]how to set data text value and data text field in drop down list control in c#

This is my code dropdown list 这是我的代码下拉列表

<asp:TemplateField HeaderText="GroupName">
  <EditItemTemplate>
    <asp:DropDownList ID="DropDownList2" runat="server"  Width="100%" Height="10%" DataSourceID="SqlDataSource2" DataTextField="GroupName" DataValueField="GroupName"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:551680A3622135F7C7A89A2044CE6B6E_12 2014\20 12 14\BACKUP 19 NEW\19 NEW\SCHOOLMANAGEMENT\SCHOOLMANAGEMENT\APP_DATA\TCS_SCHOOL.MDFConnectionString %>" SelectCommand="SELECT [GroupName FROM [Group1]"></asp:SqlDataSource>
  </EditItemTemplate>
  <ItemTemplate>
    <asp:Label ID="lblgroup" runat="server"   Text='<%#Eval("GroupName") %>'></asp:Label>
  </ItemTemplate>
  <FooterTemplate>
    <asp:DropDownList ID="DropDownList2" runat="server"  Width="100%" Height="10%" DataSourceID="SqlDataSource2" DataTextField="GroupName" DataValueField="GroupName"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:551680A3622135F7C7A89A2044CE6B6E_12 2014\20 12 14\BACKUP 19 NEW\19 NEW\SCHOOLMANAGEMENT\SCHOOLMANAGEMENT\APP_DATA\TCS_SCHOOL.MDFConnectionString %>" SelectCommand="SELECT [GroupName] FROM [Group1]"></asp:SqlDataSource>
  </FooterTemplate>
</asp:TemplateField>

I want groupname in datatextfield and groupid in datavaluefield. 我想要datatextfield中的groupname和datavaluefield中的groupid。

How do I do this in C#? 如何在C#中执行此操作? How do I bind that two values to dropdownlist in C#?? 如何将这两个值绑定到C#中的dropdownlist?

I see that you have set groupName to both dataText field and dataValue field. 我看到您已经将groupName设置为dataText字段和dataValue字段。 Maybe thats the problem? 也许那是问题所在? Try to Set groupId in the dataValue field. 尝试在dataValue字段中设置groupId。

include GroupId in your Select command then set DataValueField="GroupId". 在Select命令中包括GroupId,然后设置DataValueField =“ GroupId”。

Hope it helps, Alessandro 希望能有所帮助,亚历山德罗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM