所以我的网页上有一个gridview,目前该表从SQL表中返回值。
在我的桌子内,我已经允许编辑/更新'这是从网页中的gridview更新我的SQL表中的值, 这个功能很好,我唯一的问题是...在gridview的一个列中我想有选项 从下拉框中选择(与在其他列中输入文本框相反),
下拉框将链接到我在SQL中的列表,我尝试了各种技术并且无休止地搜索 网上尝试这样做,但我没有运气,
我想知道我是否可以得到一些帮助......
提前谢谢你。 这是我目前的gridview的aspx代码:
<asp:GridView ID="GridView2" runat="server" DataKeyNames="ID" AutoGenerateColumns="False" border="1" DataSourceID="SqlDataSource3" style="font-size: 0.9em; margin-left: 2%;" Width="85%">
<Columns>
<asp:BoundField DataField="contactName" HeaderText="contactName" SortExpression="contactName">
</asp:BoundField>
<asp:BoundField DataField="contactEmail" HeaderText="contactEmail" SortExpression="contactEmail">
</asp:BoundField>
<asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone">
</asp:BoundField>
<asp:BoundField DataField="jobRole" HeaderText="jobRole" SortExpression="jobRole">
</asp:BoundField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource3"
runat="server" ConnectionString="<%$ ConnectionStrings:saleConnectionString %>"
DeleteCommand="Delete from IMContactEmails where ID = @ID"
SelectCommand="SELECT contactName, contactEmail, Telephone, jobRole, ID FROM
IMContactEmails WHERE
(ProjectNo = @PID)" UpdateCommand="Update IMContactEmails Set
contactName=@contactName,contactEmail=CASE WHEN
@contactEmail IS NULL THEN contactEmail Else @contactEmail END,Telephone=CASE
WHEN
@Telephone IS NULL THEN Telephone ELSE @Telephone END,jobRole=CASE WHEN
@jobRole IS NULL THEN jobRole ELSE @jobRole END where ID=@ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="String" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter
ControlID="PIDvalTextBox" Name="PID" PropertyName="Text" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="contactName" />
<asp:Parameter Name="contactEmail" />
<asp:Parameter Name="Telephone" />
<asp:Parameter Name="jobRole" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
更新:
以下是我的collumns中使用TemplateField
的代码 <asp:GridView ID="GridView2" runat="server" DataKeyNames="ID" AutoGenerateColumns="False" border="1" DataSourceID="SqlDataSource3" style="font-size: 0.9em; margin-left: 2%;" Width="85%">
<Columns>
<asp:BoundField DataField="contactName" HeaderText="contactName" SortExpression="contactName">
</asp:BoundField>
<asp:BoundField DataField="contactEmail" HeaderText="contactEmail" SortExpression="contactEmail">
</asp:BoundField>
<asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone">
</asp:BoundField>
<asp:TemplateField HeaderText="jobRole">
<EditItemTemplate>
<asp:DropDownList ID="DDLjobRole" runat="server"
DataSourceID="SqlDataSource1" DataTextField="jobRole" DataValueField="jobRole"
SelectedValue='<%# Bind("jobRole")%>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
答案 0 :(得分:1)
在您的列中添加TemplateField
并在其上添加下拉列表。
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="Name" DataValueField="Name"
SelectedValue='<%# Bind("Name") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text=''<%# Bind("Name") %>''
</ItemTemplate>
</asp:TemplateField>
添加一个sql数据源并编写查询以绑定下拉列表,如下所示:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Your connection string"
SelectCommand="SELECT Name FROM dbo.Table"></asp:SqlDataSource>
试试吧。希望它有所帮助。