关于循环 GridView 及 Repeater 中的数据行的问题!


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();
            
        }

    }

然后,当点击按钮,提交此页面,需要循环 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);
         
            }


        }

 
 

}

转载于:https://www.cnblogs.com/tiger8000/archive/2008/01/02/1023306.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值