相于Repeater中使用绑定下拉列表一事 .

关于其它控件绑定这一块网上有很多,但发现repeater这一块很少,结合网上知识总结如下:

    1,在repeater上抓取控件及进行数据绑定;

      一般是在数据绑定后,或着是在ItemDataBound事件中来处理,下面介绍两种方法:

 

       第一种在数据手动绑定后用for循环来查找下拉列表控件(其它控件原理一样),

     

[c-sharp] view plain copy print ?
  1. private void DateBindView()  
  2.  {  
  3.      DataSet ds = new DataSet();  
  4.      ds = BLL.UserInfoManager.GetList();  
  5.      rptContent.DataSource = ds;  
  6.      rptContent.DataBind();  
  7.      DropDownList dllOperator;  
  8.      for (int j = 0; j < this.rptContent.Items.Count; j++)  
  9.      {  
  10.          dllOperator = (DropDownList)this.rptContent.Items[j].FindControl("dllOperator");  
  11.          dllOperator.DataSource = BLL.StateInfoManager.GetList().Tables[0].DefaultView;  
  12.          dllOperator.DataTextField = "Remark";  
  13.          dllOperator.DataValueField = "stateid";  
  14.          dllOperator.DataBind();   
  15.   
  16.          DataRowView rowv = (DataRowView)this.rptContent.Items[j].DataItem;  
  17.          //设置初始项   
  18.          dllOperator.Items.Add(new ListItem("-Choose-"""));  
  19.          dllOperator.SelectedValue = ds.Tables[0].Rows[j][4].ToString();//获到当前列的状态ID用于绑定下拉列表   
  20.      }   
  21.  }  
<textarea style="DISPLAY: none" class="c-sharp" rows="15" cols="76" name="code"> private void DateBindView() { DataSet ds = new DataSet(); ds = BLL.UserInfoManager.GetList(); rptContent.DataSource = ds; rptContent.DataBind(); DropDownList dllOperator; for (int j = 0; j &lt; this.rptContent.Items.Count; j++) { dllOperator = (DropDownList)this.rptContent.Items[j].FindControl(&quot;dllOperator&quot;); dllOperator.DataSource = BLL.StateInfoManager.GetList().Tables[0].DefaultView; dllOperator.DataTextField = &quot;Remark&quot;; dllOperator.DataValueField = &quot;stateid&quot;; dllOperator.DataBind(); DataRowView rowv = (DataRowView)this.rptContent.Items[j].DataItem; //设置初始项 dllOperator.Items.Add(new ListItem(&quot;-Choose-&quot;, &quot;&quot;)); dllOperator.SelectedValue = ds.Tables[0].Rows[j][4].ToString();//获到当前列的状态ID用于绑定下拉列表 } }</textarea>

    

     第二种有ItemDataBound方法中进行查找下拉列表,并进行当前数据的绑定.这个地方很关键!

   

    

[c-sharp] view plain copy print ?
  1. protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  2.    {  
  3.        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
  4.        {  
  5.                DropDownList dllOperator = (DropDownList)e.Item.FindControl("dllOperator");  
  6.          
  7.                dllOperator.DataTextField = "Remark";  
  8.                dllOperator.DataValueField = "stateid";  
  9.                dllOperator.DataSource = BLL.StateInfoManager.GetList().Tables[0].DefaultView;  
  10.                //BLL.StateInfoManager.GetList().Tables[0].DefaultView;   
  11.                dllOperator.DataBind();  
  12.                dllOperator.Items.Add(new ListItem("-Choose-"""));  
  13.                //找到分类Repeater关联的数据项    
  14.                DataRowView rowv = (DataRowView)e.Item.DataItem;//获取当前项行数据再去访问那一列.注意坐标索引是从0开始   
  15.   
  16.                dllOperator.SelectedValue =rowv[4].ToString();  
  17.        }  
  18.    }  
<textarea style="DISPLAY: none" class="c-sharp" rows="15" cols="76" name="code"> protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DropDownList dllOperator = (DropDownList)e.Item.FindControl(&quot;dllOperator&quot;); dllOperator.DataTextField = &quot;Remark&quot;; dllOperator.DataValueField = &quot;stateid&quot;; dllOperator.DataSource = BLL.StateInfoManager.GetList().Tables[0].DefaultView; //BLL.StateInfoManager.GetList().Tables[0].DefaultView; dllOperator.DataBind(); dllOperator.Items.Add(new ListItem(&quot;-Choose-&quot;, &quot;&quot;)); //找到分类Repeater关联的数据项 DataRowView rowv = (DataRowView)e.Item.DataItem;//获取当前项行数据再去访问那一列.注意坐标索引是从0开始 dllOperator.SelectedValue =rowv[4].ToString(); } }</textarea>

 

 

     而前台只需一句代码就OK了

        <asp:DropDownList ID="dllOperator" runat="server"></asp:DropDownList>

你可能感兴趣的:(相于Repeater中使用绑定下拉列表一事 .)