简体   繁体   English

选中添加为模板字段的复选框时,如何从gridview控件中的一行中检索对应的数据

[英]How to retrieve the corresponding data from a row in a gridview control when checking a checkbox added as a Template field

I am a beginner in ASP.NET and I am not a code genius. 我是ASP.NET的初学者,而且不是代码天才。 I am hoping someone can help. 我希望有人能提供帮助。

So I created a web form and added a gridview control to it. 因此,我创建了一个Web表单,并向其中添加了gridview控件。 The gridview contains Two columns: Name_EN(the name of the subject), Select Subjects(a template column that contains a checkbox). gridview包含两列:Name_EN(主题名称),Select Subjects(包含复选框的模板列)。

Ultimately I want a student to use this web form to register his subjects. 最终,我希望学生使用此网络表单注册其学科。 So whenever he clicks a submit button, he will be registered in all the subjects that he has selected using the checkbox. 因此,每当他单击“提交”按钮时,他都将被注册到他使用复选框选择的所有主题中。

I thought I could test this with adding a button that will print -on the web form- the Names of the subjects that I select. 我以为我可以通过添加一个按钮进行测试,该按钮将在Web窗体上打印我选择的主题的名称。

This is the HTML and CSS code for the GridView. 这是GridView的HTML和CSS代码。

<asp:GridView ID="dgvUsers" runat="server" 
    CssClass="table table-hover table-striped" GridLines="None" 
    AutoGenerateColumns="False" DataSourceID="source1" >
    <Columns>
        <asp:BoundField DataField="Name_EN" HeaderText="Name_EN" SortExpression="Name_EN" />
        <asp:TemplateField HeaderText="Select Subjects">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("Name_EN", "{0}") %>' />
            </ItemTemplate>
        </asp:TemplateField>  
    </Columns>
    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
    <RowStyle CssClass="cursor-pointer" />
</asp:GridView>

<asp:SqlDataSource ID="source1" runat="server" ConnectionString="<%$ ConnectionStrings:BIS1ConnectionString %>" SelectCommand="spViewAvailableSubjects" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:SessionParameter Name="ID" SessionField="ID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

Then I need to view all the selected data from the "Name_EN" column but only for the rows where I checked the checkboxes when I click this Button: 然后,我需要查看从“ Name_EN”列中选择的所有数据,但仅适用于单击此按钮时选中复选框的行:

<asp:Button ID="ViewButton" runat="server" CssClass="btn btn-primary" Text="View Selected Subjects" OnClick="ViewButton_Click"/>

I am thinking to print the data using the Response.Write(); 我正在考虑使用Response.Write()打印数据; method. 方法。

But I am still a beginner with ASP.NET and I don't know how to implement that in code or how to connect the checkboxes with their corresponding data in the row. 但是我仍然是ASP.NET的初学者,我不知道如何在代码中实现该功能,也不知道如何将复选框与行中的相应数据连接起来。

I just want to retrieve the data and print them. 我只想检索数据并打印它们。

Can anyone help me with that? 有人可以帮我吗?

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration Page.aspx.cs" Inherits="IntegratedSystems.Registration_Page" MasterPageFile="Site1.Master" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <div class="container-fluid"> <div class="container-fluid"> <%-- <form draggable="true" runat="server" style="max-width: 230px; text-align: left;">--%> <ul style="max-width: 230px; text-align: left;" class="nav nav-pills nav-stacked"> <li class="text-left"> <asp:Hyperlink ID="HomeLink" runat="server" CssClass="btn " NavigateUrl="~/Student's Portal.aspx">Home</asp:Hyperlink> </li> <li class="text-left"> <asp:HyperLink ID="InstructorLink" runat="server" CssClass="btn ">Choose Instructors</asp:HyperLink> </li> <li class="text-left"> <asp:HyperLink ID="TimeLink" runat="server" CssClass="btn ">Choose Time</asp:HyperLink> </li> <li class="text-left"> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="HomeBtn_Click" CssClass="btn ">Logout</asp:LinkButton> </li> </ul> <br/> <label class="label label-info">These are the Available Subjects for your Registration</label> <asp:GridView ID="dgvUsers" runat="server" CssClass="table table-hover table-striped" GridLines="None" AutoGenerateColumns="False" DataSourceID="source1"> <Columns> <asp:BoundField DataField="Name_EN" HeaderText="Name_EN" SortExpression="Name_EN" /> <asp:TemplateField HeaderText="Select Subjects"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("Name_EN", "{0}") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> <RowStyle CssClass="cursor-pointer" /> </asp:GridView> <asp:GridView ID="GridView1" CssClass="table table-hover table-striped" GridLines="None" runat="server"> </asp:GridView> <asp:SqlDataSource ID="source1" runat="server" ConnectionString="<%$ ConnectionStrings:BIS1ConnectionString %>" SelectCommand="spViewAvailableSubjects" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:SessionParameter Name="ID" SessionField="ID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <br/> <asp:Button ID="ViewButton" runat="server" CssClass="btn btn-primary" Text="View Selected Subjects" OnClick="ViewButton_Click" /> <%--</form>--%> </div> </div> </asp:Content> 

And here is the code behind file for the ViewButton click event: 这是ViewButton click事件的文件背后的代码:

protected void ViewButton_Click(object sender, EventArgs e)
    {

        List<string> selectedSubjects = new List<string>();
        foreach (GridViewRow gvr in dgvUsers.Rows)
        {
            CheckBox chk = (CheckBox) gvr.Cells[1].FindControl("CheckBox1");
            if (chk.Checked)
            {
                selectedSubjects.Add(chk.Text);

            }
        }

        if (selectedSubjects.Count > 0)
        {
            GridView1.DataSource = selectedSubjects;
            GridView1.DataBind();
        }
    }

To iterate through your gridview: 要遍历gridview:

protected void ViewButton_Click(object sender, EventArgs e) {
    List<string> selectedSubjects = new List <string> ();
    foreach(GridViewRow gvr in dgvUsers.Rows) {
        CheckBox chk = (CheckBox) gvr.Cells[1].FindControl("CheckBox1");
        if (chk.Checked) {
            selectedSubjects.Add(chk.Text);
        }
    }

    if (selectedSubjects.Count > 0) {
        gvSelected.DataSource = selectedSubjects;
        gvSelected.DataBind();
    }
}

To display message to the user, add another gridview to the form: 要向用户显示消息,请向表单添加另一个gridview:

<asp:GridView ID="gvSelected" runat="server">
</asp:GridView>

Obviously, pretty it up as much as you can. 显然,要尽可能地使其美观。

I personally, would not advise using Response.Write in web forms; 我个人不建议在Web表单中使用Response.Write。 the whole point of them is to bind controls to code behind, not imitate classic asp. 它们的全部目的是将控件绑定到背后的代码,而不是模仿经典的ASP。

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

相关问题 从gridview控件中隐藏字段存储和检索数据 - store and retrieve data from hidden field from gridview control 双击相应行时检查 DataGrid 中的复选框 - Checking a CheckBox in a DataGrid when double clicking on the corresponding row 循环通过GridView行和检查复选框控件 - Looping through GridView rows and Checking Checkbox Control 从服务器端取消选中gridview模板字段中的复选框 - Uncheck the checkbox within the gridview template field from server side 从“模板字段”复选框选择和Gridview绑定域向表中添加记录 - Adding Records to Table from Template Field Checkbox selection and Gridview Boundfield 当我们从gridview选中的行中有相应的datatextfield时,如何显示下拉列表中的项目被选中 - How to show item in dropdownlist as being selected when we have corresponding datatextfield from gridview selected row 无法从GridView模板字段检索新值 - Can't retrieve new value from GridView template field 如何在gridview控件中显示空数据行 - How to show empty data row in gridview control 如何获取内容控件以在WPF网格视图中显示选定字段的数据模板? - How to get a Content control to present a data template for a selected field in a WPF gridview? 如何将GridView模板字段设置为模式弹出扩展器的目标控件? - How to set gridview template field as target control for modal popup extender?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM