[英]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.