GridView 中嵌套 Repeater ,显示分级数据:
在 GridView1 的 GridView1_DataBound 事件中这样写:
protected
void
GridView1_DataBound(
object
sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Label LabelID = row.FindControl( " Label1 " ) as Label;
string parentID = LabelID.Text.ToString();
Repeater subRepeater = row.FindControl( " Repeater1 " ) as Repeater;
string sql = " select * from CandidateUser where WantPosition = ' " + parentID + " ' order by UID asc " ;
DataSet ds = newdb.CommonDataSet(sql);
subRepeater.DataSource = ds.Tables[ 0 ].DefaultView;
subRepeater.DataBind();
}
}
{
foreach (GridViewRow row in GridView1.Rows)
{
Label LabelID = row.FindControl( " Label1 " ) as Label;
string parentID = LabelID.Text.ToString();
Repeater subRepeater = row.FindControl( " Repeater1 " ) as Repeater;
string sql = " select * from CandidateUser where WantPosition = ' " + parentID + " ' order by UID asc " ;
DataSet ds = newdb.CommonDataSet(sql);
subRepeater.DataSource = ds.Tables[ 0 ].DefaultView;
subRepeater.DataBind();
}
}
然后,当点击按钮,提交此页面,需要循环 GridView1 和 Repeater1 中的数据取值,存入数据库,如何取值呢,这样做:
protected
void
Button1_Click(
object
sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Label LabelID = row.FindControl( " Label1 " ) as Label; // 竞聘职位的ID号Label
parentID = LabelID.Text.ToString(); // 竞聘职位的ID号
Repeater subRepeater = row.FindControl( " Repeater1 " ) as Repeater;
foreach (RepeaterItem subrow in subRepeater.Items)
{
Label LabelUserID = subrow.FindControl( " Label2 " ) as Label; // 竞聘人的身份ID号Label
CandidateUserID = LabelUserID.Text.ToString(); // 竞聘人的身份ID号
TextBox txbScore = subrow.FindControl( " TextBox1 " ) as TextBox; // 分数 TextBox1
CandidateScore = txbScore.Text.ToString(); // 分数
CheckBox ckbGiveUp = subrow.FindControl( " CheckBox1 " ) as CheckBox; // 分数 TextBox1
if (ckbGiveUp.Checked == true )
{
giveup = " 1 " ;
}
else
{
giveup = " 0 " ;
}
sql = " insert into VoteTable (PositionID,CandidateUserID,CandidateUserScore,GiveUpVote,WhoSendTheVote) values (' " + parentID + " ',' " + CandidateUserID + " ',' " + CandidateScore + " ',' " + giveup + " ',' " + getName + " ') " ;
newdb.ModifyData(sql);
}
}
}
{
foreach (GridViewRow row in GridView1.Rows)
{
Label LabelID = row.FindControl( " Label1 " ) as Label; // 竞聘职位的ID号Label
parentID = LabelID.Text.ToString(); // 竞聘职位的ID号
Repeater subRepeater = row.FindControl( " Repeater1 " ) as Repeater;
foreach (RepeaterItem subrow in subRepeater.Items)
{
Label LabelUserID = subrow.FindControl( " Label2 " ) as Label; // 竞聘人的身份ID号Label
CandidateUserID = LabelUserID.Text.ToString(); // 竞聘人的身份ID号
TextBox txbScore = subrow.FindControl( " TextBox1 " ) as TextBox; // 分数 TextBox1
CandidateScore = txbScore.Text.ToString(); // 分数
CheckBox ckbGiveUp = subrow.FindControl( " CheckBox1 " ) as CheckBox; // 分数 TextBox1
if (ckbGiveUp.Checked == true )
{
giveup = " 1 " ;
}
else
{
giveup = " 0 " ;
}
sql = " insert into VoteTable (PositionID,CandidateUserID,CandidateUserScore,GiveUpVote,WhoSendTheVote) values (' " + parentID + " ',' " + CandidateUserID + " ',' " + CandidateScore + " ',' " + giveup + " ',' " + getName + " ') " ;
newdb.ModifyData(sql);
}
}
}