0

我已经指定了一个SelectCommandin SqlDataSource.aspx文件)。此SelectCommand查询将返回数据,该数据将显示在 中GridView

现在idselect 命令中有一个名为 (Primary Key) 的属性,其数据不会显示在 上GridView,而是我想在单击相应的页面时将此“id”发送到另一个页面ButtonField

网格视图数据

上图显示GridView。每列代表活动字段,而每一行代表一个独特的活动。最后一列ButtonFields用于注册活动。

我想将学生发送到“注册活动页面”以及单击的行的“ID”。请注意,“id”的值未显示在GridView.

请建议我实现它的好方法。

我认为一种解决方案可能是:访问后端(.cs)文件中“id”的值,然后通过 ButtonField 命令发送 id 的值。但我不确定它是否可以在 .cs 文件中访问?

ASP 代码:

<asp:GridView ID="ShowActivitiesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
                <asp:BoundField DataField="location" HeaderText="Location" SortExpression="location" />
                <asp:BoundField DataField="date_of_activity" HeaderText="Date of Activity" SortExpression="date_of_activity" />
                <asp:BoundField DataField="organization" HeaderText="Organization" SortExpression="organization" />
                <asp:BoundField DataField="no_of_student" HeaderText="No of Student" SortExpression="no_of_student" />
                <asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
                <asp:ButtonField CommandName="RowCommand" HeaderText="Register For Activity" ShowHeader="True" Text="Register" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [id], [name], [location], [date_of_activity], [organization], [no_of_student], [description] FROM [Activities]">
        </asp:SqlDataSource>

4

1 回答 1

1

将 ButtonField 替换为 ItemTemplateaspx

    <asp:GridView ID="ShowActivitiesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
                <asp:BoundField DataField="location" HeaderText="Location" SortExpression="location" />
                <asp:BoundField DataField="date_of_activity" HeaderText="Date of Activity" SortExpression="date_of_activity" />
                <asp:BoundField DataField="organization" HeaderText="Organization" SortExpression="organization" />
                <asp:BoundField DataField="no_of_student" HeaderText="No of Student" SortExpression="no_of_student" />
                <asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
                <asp:ButtonField CommandName="RowCommand" HeaderText="Register For Activity" ShowHeader="True" Text="Register" />
                <asp:TemplateField HeaderText="Remove" ShowHeader="False">
                  <ItemTemplate>
                    <asp:LinkButton ID="btnRegisterActivity" runat="server" CausesValidation="false" Text="Register For Activity" CommandArgument='<%# Eval("id") %>' oncommand="btnRegisterActivity_Command" />
                  </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [id], [name], [location], [date_of_activity], [organization], [no_of_student], [description] FROM [Activities]">
    </asp:SqlDataSource>

在页面cs文件上

        protected void btnRegisterActivity_Command(object sender, CommandEventArgs e)
        {
            int myId = (int)e.CommandArgument;

            //Do whatever you want
        }
于 2019-05-02T13:16:21.453 回答