动态 GridView 下拉列表

发布于 2024-10-15 10:29:16 字数 167 浏览 2 评论 0原文

我有一个包含以下字段的网格视图:

讲座 ID、讲师姓名、主题,网格视图包含用于插入、编辑和删除的链接按钮。我的内容 需要的是,当我单击时,编辑主题列应包含下拉列表以选择主题。 如果 GridView 行未处于 EDIT 模式或 INSERT 模式,则不应显示 DROPDOWN,而仅显示讲师教授的主题。

I have a gridview with following fields

LECTURE ID,LECTURER NAME,SUBJECTS and gridview contains link Buttons to Insert,Edit and DELETE.what i
need is, when i click, EDIT SUBJECTS columns should contain DROPDOWN Lists to select subjects.
if the GridView row is not in the EDIT mode or INSERT mode DROPDOWN should not be displayed and display only the subject taught by the lecturer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪恋 2024-10-22 10:29:16

这可以通过将 SUBJECT 列设置为 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 列来调用 EDIT、INSERT 和 DELETE 功能。

This can be accomplished by making the SUBJECT column a TemplateField, as shown below:

<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>

You might have already added ButtonField columns to invoke EDIT, INSERT and DELETE functionality.

慕烟庭风 2024-10-22 10:29:16

进行这种数据绑定的技巧是使用两个数据源,一个包含讲座 ID、讲师姓名和主题 ID,另一个数据源仅包含主题列表。您可以使用任何 DataSource 控件来执行此操作 - 这是使用 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" /> 
                <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>

The trick with doing this kind of databinding is to use two data sources, one that carries the lecture id, lecturer name and subject id, and one that just has the list of subjects. You can do this with any of the DataSource controls - here's a demo using 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" /> 
                <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文