repeater中巢狀放入RadioButtonList

请叫我七哥發表於2024-05-15
 <asp:Repeater ID="rptList" runat="server"  onitemdatabound="rptList_ItemDataBound" >
  <HeaderTemplate>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ltable">
    <tr> 
      <th  >操作型別</th>
    </tr>
  </HeaderTemplate>
  <ItemTemplate>
       <td align="center">
        <asp:RadioButtonList ID="rad_project_type" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
        </asp:RadioButtonList>
      </td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    <%#rptList.Items.Count == 0 ? "<tr><td align=\"center\" colspan=\"1\">暫無記錄</td></tr>" : ""%>
  </table>
  </FooterTemplate>
  </asp:Repeater>
        //資料繫結
        private void RptBind()
        {
            BLL.project bll = new BLL.project();
            DataTable dt = bll.GetList(100," id>0 ","id desc").Tables[0];
            this.rptList.DataSource = dt;
            this.rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                RadioButtonList rblst = (RadioButtonList)e.Item.FindControl("rad_project_type");
                //DataRowView drv = (DataRowView)e.Item.DataItem;
                BLL.project_type bll = new BLL.project_type();
                DataTable dt=bll.GetList(100, "id>0 ", "id desc").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    rblst.DataSource = dt;
                    rblst.DataTextField = "project_type_name";
                    rblst.DataValueField = "id";
                    rblst.DataBind();
                }
            }

             /*  這裡處理選擇之後獲得值,放在提交按鈕中
              string rb="";//單選框
              foreach (RepeaterItem item in this.rptList.Items)
              {
                  RadioButtonList rblst = (RadioButtonList)e.Item.FindControl("rad_project_type");
                  for (int i = 0; i < rblst.Items.Count; i++)
                  {
                      if (rblst.Items[i].Selected == true)
                      {
                          if (rb == "")
                          {
                              rb += rblst.Items[i].Value;
                          }
                          else
                          {
                              rb += "|"+rblst.Items[i].Value;
                          }
                      }
                  }
              }
              string[] s = rb.Split('|');
             
              */
              
            
        }

相關文章