如何在asp.net中获取GridView的特定数据行?
我有这张图片:
实施例: 我想得到studentID = 2011017997的特定数据行及其CourseNo ='CmpE 515';
如何获取他们的具体数据? GridViewRow是否具有内置函数之一来获取数据行?
这是aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidateSubjectTeacher.aspx.cs" Inherits="SoftwareAnalysisAndDesign.SAD.ValidateSubjectTeacher" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student Assessment Form</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="Bootstrap/css/bootstrap-theme.css" />
<link rel="stylesheet" href="Bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="Stylesheets/ValidateSubjectTeacherStyle.css"/>
<!--Side bar link-->
<link rel="stylesheet" href="SidebarBootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="SidebarBootstrap/css/simple-sidebar.css" />
</head>
<body>
<form id="form1" runat="server">
<div class="container" style="text-align: center; margin: 0 auto;">
<br />
<h1>Validation of Subjects</h1>
<br />
<asp:GridView runat="server" CssClass="table table-hover table-bordered" ID="ValidateSubject" Style="text-align: center"></asp:GridView>
</div>
<div style="float: right; padding-right: 75px;">
<button type="button" runat="server" class="btn btn-primary" onserverclick="ValidateSubject_Click">Validate</button>
</div>
</form>
<script src="jquery/jquery.min.js"></script>
<script src="Bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
这是我的aspx代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class ValidateSubjectTeacher : System.Web.UI.Page
{
CheckBox check = new CheckBox();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ValidateSubject"] == null)
{
Response.Redirect("TeacherPage.aspx", true);
}
if (!IsPostBack)
{
ValidateSubject.DataSource = Session["ValidateSubject"];
ValidateSubject.DataBind();
}
foreach (GridViewRow row in ValidateSubject.Rows)
{
check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox; //position Check column on last row in gridview
check.Enabled = true;
check.CheckedChanged += ValidateSubject_Click; //Bind the event on the button
check.AutoPostBack = true; //Set the AutoPostBack property to true
}
}
protected void ValidateSubject_Click(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give row
string validated = "Validated";
string notyetvalidated = "Not yet validated";
if (chk.Checked)
{
grvRow.Cells[10].Text = validated;
//Open Connection
using (SqlConnection conn = new SqlConnection("Data Source=Keith;Initial Catalog=SAD;Integrated Security=True"))
{
//Open Connection to database
try
{
conn.Open();
}
catch (Exception E)
{
Console.WriteLine(E.ToString());
}
using (SqlCommand cmd = new SqlCommand("Update AssessmentForm set Status = '@Validated' where StudentID = @studentID and CourseNo = '@Coursenumber'" ,conn))
{
cmd.Parameters.AddWithValue("@Validated", validated);
cmd.Parameters.AddWithValue("@studentID", );
cmd.Parameters.AddWithValue("@Coursenumber", );
}
//Close Connection to database
try
{
conn.Open();
}
catch (Exception E)
{
Console.WriteLine(E.ToString());
}
}
}
else
{
grvRow.Cells[10].Text = notyetvalidated;
}
}
}
}
如何在gridview中获取studentID和courseNo的值?我想获取学生ID和CourseNo的数据行,并将其放在我的一个参数中:
cmd.Parameters.AddWithValue("@studentID", );
cmd.Parameters.AddWithValue("@Coursenumber", );
此问题只是此链接的后续问题(Change text in specific row) 现在,这次我想在选中复选框事件并触发按钮ValidateSubject_Click事件时更新我的查询。
答案 0 :(得分:2)
由于您可以使用 gvRow 访问 GridViewRow ,因此您可以从行单元格1和2访问StudentID和Coursenumber,如下所示
var studentID = grvRow.Cells[0].Text;
var courseNumber = grvRow.Cells[1].Text;
答案 1 :(得分:1)
解决问题的另一个例子是:
<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" DataSourceID="StudentDataSource">
<Columns>
<asp:BoundField DataField="Serial" HeaderText="Serial" SortExpression="Serial" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:TemplateField HeaderText="Verified" SortExpression="Verified">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>'
AutoPostBack="true" OnCheckedChanged="chkVerified_CheckedChanged" />
<asp:HiddenField ID="hddSerial" runat="server" Value='<%# Bind("Serial") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblResult" runat="server" Text="You have validate: N/a"></asp:Label>
<asp:ObjectDataSource ID="StudentDataSource" runat="server" SelectMethod="Get" TypeName="WebFormApp._32846281.Models.Student"></asp:ObjectDataSource>
在您的代码中:
protected void chkVerified_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow grvRow = (GridViewRow)chk.NamingContainer;
HiddenField hf = grvRow.FindControl("hddSerial") as HiddenField;
lblResult.Text = string.Format("You have validate:{0}", hf.Value);
}
解决了这个问题:如果你重新排序列,首先是方法,你必须改变代码
在我的解决方案中,如果您重新排序列,则后面的代码是相同的。