1、vb.net
Public Sub onCheckChange(ByVal sender As Object, ByVal e As EventArgs)
Dim t As DataGridItem = DirectCast(DirectCast(sender, CheckBox).Parent.Parent, DataGridItem)
Dim i As Integer = t.ItemIndex
Dim chk As CheckBox = DirectCast(sender, CheckBox)
If chk.Checked Then
Session("indexID") = Session("indexID") & "," & DirectCast(t.Cells(1).Controls(0), LinkButton).Text
Else
Session("indexID") = Replace(Session("indexID"), "," & DirectCast(t.Cells(1).Controls(0), LinkButton).Text, "")
End If
Response.Write(Session("indexID"))
End Sub
2、C#
首先将CheckBox1的AutoPostBack属性设置为true
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
DataGridItem t = (DataGridItem)((CheckBox)sender).Parent.Parent;
int i = t.ItemIndex;
CheckBox tCheckBox1 = (CheckBox)sender;
if(tCheckBox1.Checked)
{
DataTable tempTable = (DataTable)Session["eduGrid"];
Session["Group"]=tempTable.Rows[i]["groupid"];
Session["PassScore"]=tempTable.Rows[i]["passgrade"];
Session["Title"]="学习中心"+Session["Group"].ToString()+"在"+tempTable.Rows[i]["ValidDuration"]+"的考试";
this.lbTestGroup.Text = Session["Group"].ToString();
this.lbPassScore.Text = Session["PassScore"].ToString();
this.txTestTitle.Text = Session["Title"].ToString();
}
}
本文介绍如何在VB.NET及C#中实现DataGrid与CheckBox的状态联动。通过监听CheckBox的状态变化更新DataGrid中相关项的选择状态,并同步到Session变量中。此方法适用于需要根据用户选择动态调整显示或处理的数据项的应用场景。
2684

被折叠的 条评论
为什么被折叠?



