当前位置:Gxlcms > asp.net > WebForm获取checkbox选中的值(几个简单的示例)

WebForm获取checkbox选中的值(几个简单的示例)

时间:2021-07-01 10:21:17 帮助过:18人阅读

PS:最近在做权限管理这个模块,发现用checkbox的地方挺多的,于是写了个简单的例子,以供以后学习和使用。

1.前端页面:

  1. <form id="form1" method="get" runat="server">
  2. <input name="chk_per" type="checkbox" value="3" />张三
  3. <input name="chk_per" type="checkbox" value="4" />李四
  4. <input name="chk_per" type="checkbox" value="5" />王五
  5. <input name="chk_per" type="checkbox" value="6" />赵六
  6. <input name="chk_per" type="checkbox" value="7" />孙琦
  7. <input name="chk_per" type="checkbox" value="8" />猪八
  8. <input type="submit" id="btnOK" value="提交" />
  9. </form>

2.后台方法:

  1. #region 获取从前端页面回传过来的 CheckBox 的值 void GetCheckBoxValue()
  2. /// <summary>
  3. /// 获取从前端页面回传过来的 CheckBox 的值
  4. /// <para>Request.Form["chk_per"] 以逗号分割,获取所有选中的 CheckBox 的值</para>
  5. /// </summary>
  6. private void GetCheckBoxValue()
  7. {
  8. string user = Request["chk_per"];
  9. string[] users = user.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
  10. string s = string.Empty;
  11. foreach (var item in users)
  12. {
  13. s += item + " | ";
  14. }
  15. }
  16. #endregion
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (IsPostBack)
  4. {
  5. //测试调用
  6. GetCheckBoxValue();
  7. }
  8. }

人气教程排行