我想在gridview中更改特定文本,如下图所示:
示例如果单击复选框按钮,特定行“状态”文本将更改为“已验证”。
这是我的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;
using System.Data.SqlClient;
using MSSQLConnector;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class TeacherPage : System.Web.UI.Page
{
private MSConnector connector = new MSConnector();
private DataSet SubjectlistData;
private DataTable SubjectlistTable;
string query = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Populate The Select Tag with Subjects
SubjectList.DataSource = Session["TeacherSubjectList"];
SubjectList.DataTextField = "CourseNo";
SubjectList.DataValueField = "CourseNo";
SubjectList.DataBind();
}
}
}
protected void TeacherSubjects_Click(object sender, EventArgs e)
{
string getText = SubjectList.SelectedItem.Text;
//Connection String
connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";
query = "select StudentID,CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room,Instructor,Amount,Status from assessmentform where CourseNo = '" + getText + "'";
SubjectlistData = connector.ExecuteQuery(query);
SubjectlistTable = SubjectlistData.Tables[0];
//Add a colum check row
SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));
Session["ValidateSubject"] = SubjectlistTable;
Response.Redirect("ValidateSubjectTeacher.aspx");
}
我使用以下方法在我的行中添加了一个复选框:
//Add a colum check row
SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));
Session["ValidateSubject"] = SubjectlistTable;
我将使用会话的gridview传递给另一个页面, 它在ValidatePage后面的aspx代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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();
}
//I add my checkbox in the last row using this
foreach (GridViewRow row in ValidateSubject.Rows)
{
check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox;
check.Enabled = true;
}
}
protected void ValidateSubject_Click(object sender, EventArgs e)
{
if(check.Checked)
{
//This condition here is I want to change the Status when check to validated.
}
}
}
在我的代码后面使用会话我绑定DataTable中的所有数据(FirstPage);
DataSet ds = connector.ExecuteQuery(query);
DataTable dt = dt.Table[0];
dt.DataBind();
Session["SubjectList"] = dt;
答案 0 :(得分:1)
您可以使用NamingContainer
这样的属性: -
protected void ValidateSubject_Click(object sender, EventArgs e)
{
CheckBox chk= (CheckBox)sender;
GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give you row
grvRow.Cells[10].Text = "Validated"; //Updated the cell text in that row.
//Or if Status is a control like label then //
Label StatusLabel = (Label)grvRow.FindControl("StatusLabel");
StatusLabel.Text = "Validated";
}
或者,您也可以使用RowDataBound
事件。
<强>更新强>
由于您直接绑定网格,即AutoGenerateColumns
设置为true,您必须以编程方式附加复选框的事件处理程序,如下所示: -
//I add my checkbox in the last row using this
foreach (GridViewRow row in ValidateSubject.Rows)
{
check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox;
check.Enabled = true;
check.CheckedChanged += ValidateSubject_Click; //Bind the event
check.AutoPostBack = true; //Set the AutoPostBack property to true
}
现在,在您的活动中,首先在复选框的帮助下找到该行,然后像这样更新Status
列: -
protected void ValidateSubject_Click(object sender, EventArgs e)
{
CheckBox chk= (CheckBox)sender;
GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give you row
if(chk.Checked)
{
grvRow.Cells[10].Text = "Validated";
}
}
答案 1 :(得分:1)
您可以使用这样的扩展方法:
public static class myExtensionsMethods
{
public static void validateRow(this object sender)
{
int columnIndex = 10;
GridViewRow myRow = ((Control)sender).Parent as GridViewRow;
myRow.Cells[columnIndex].Text = "Validated";
}
}
扩展方法允许您以这种方式调用它
protected void ValidateSubject_Click(object sender, EventArgs e)
{
if (check.Checked)
{
sender.validateRow();
}
}