Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 动态GridView下拉列表_Asp.net_Gridview_Drop Down Menu - Fatal编程技术网

Asp.net 动态GridView下拉列表

Asp.net 动态GridView下拉列表,asp.net,gridview,drop-down-menu,Asp.net,Gridview,Drop Down Menu,我有一个包含以下字段的gridview 讲师ID、讲师姓名、主题和gridview包含用于插入、编辑和删除的链接按钮 需要的是,当我点击时,“编辑主题”列应该包含下拉列表来选择主题。 如果GridView行未处于编辑模式或插入模式,则不应显示下拉列表,仅显示讲师讲授的主题 这可以通过将主题列设置为TemplateField来实现,如下所示: <asp:TemplateField Header="Subject"> <EditItemTem

我有一个包含以下字段的gridview

讲师ID、讲师姓名、主题和gridview包含用于插入、编辑和删除的链接按钮 需要的是,当我点击时,“编辑主题”列应该包含下拉列表来选择主题。
如果GridView行未处于编辑模式或插入模式,则不应显示下拉列表,仅显示讲师讲授的主题

这可以通过将主题列设置为TemplateField来实现,如下所示:

<asp:TemplateField Header="Subject">
                    <EditItemTemplate>
                        <asp:DropDownList ID="subjectDDl" runat="server"></asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="subjectLabel" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>


您可能已经添加了ButtonField列以调用编辑、插入和删除功能。

进行这种数据绑定的技巧是使用两个数据源,一个包含讲座id、讲师姓名和主题id,另一个仅包含主题列表。您可以使用任何数据源控件执行此操作-下面是使用SqlDataSource的演示:

<!-- First data source is the main data source for the gridview -->
<asp:SqlDataSource runat="server" id="LectureDataSource" 
SelectCommand="SELECT l.LectureId, l.Lecturer, l.SubjectId, s.Subject FROM Lecture l INNER JOIN Subject s ON l.SubjectId = s.SubjectID" 
UpdateCommand="UPDATE Lecture SET Lecturer = @Lecturer, SubjectId = @SubjectId"
DeleteCommand="DELETE FROM Lecture WHERE LectureId = @LectureId" InsertCommand="INSERT INTO Lecture (Lecturer, SubjectId) VALUES (@Lecturer, @SubjectID)"
>
<DeleteParameters>
    <asp:ControlParameter ControlID="LectureGridView" Name="LectureId" 
        PropertyName="SelectedValue" />
</DeleteParameters>
<UpdateParameters>
    <asp:Parameter Name="Lecturer" />
    <asp:Parameter Name="SubjectId" />
</UpdateParameters>
</asp:SqlDataSource>

<!-- Second data source is the data source for the Subject dropdownlist -->
<asp:SqlDataSource runat="server" ID="SubjectDataSource"  
 SelectCommand="SELECT SubjectId, Subject FROM Subject ORDER BY SubjectId" />

<asp:GridView runat="server" AutoGenerateColumns="False" DataSourceID="LectureDataSource"
    ID="LectureGridView" DataKeyNames="LectureId" >
    <!-- Setting the DataKeyNames property allows us to delete by using the SelectedValue -->
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />&nbsp;
                <asp:LinkButton Text="Delete" runat="server" CommandName="Delete" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton Text="Update" runat="server" CommandName="Update" />
                <asp:LinkButton Text="Cancel" runat="server" CommandName="Cancel" />
            </EditItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:BoundField DataField="LectureId" HeaderText="Lecture Id" ReadOnly="true" />
        <asp:TemplateField HeaderText="Lecturer">
            <ItemTemplate>
                <asp:Label Text='<%# Bind("Lecturer") %>' runat="server" ID="LecturerLabel" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="LecturerEditTextBox" runat="server" Text='<%# Bind("Lecturer") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <!-- The label is bound to the subject text field from the LectureDataSource -->
                <asp:Label ID="SubjectLabel" runat="server" Text='<%# Bind("Subject")  %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <!-- In Edit mode the dropdownlist is bound to the SubjectDataSource, but we also set the text value from the LectureDataSource -->
                <asp:DropDownList ID="SubjectEditDropDownList" runat="server" DataSourceID="SubjectDataSource"
                    DataTextField="Subject" DataValueField="SubjectId" Text='<%# Bind("SubjectId") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

您能给我们展示一下到目前为止您所拥有的代码和标记吗?回答一个没有明确目标的问题是困难的。