页面有100个button,一个listbox。动态产生控件及一些逻辑控制
来源:互联网 发布:js判断奇数偶数 编辑:程序博客网 时间:2024/06/01 20:31
100个button代码一样,点击了的buttonid显示在listbox内。
后置文件:
每行10个,最多只能选择5个,并且要同一行相邻的5个。
怎么控制着100个相同代码的button点击事件。
原贴: http://bbs.csdn.net/topics/390285487
应该说这个也不难,就当练练手吧。
<%@ Page Language="C#" AutoEventWireup="true" ViewStateEncryptionMode="Always" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server" > <div id="divLeftListBox" style="width:20%;float:left;" > <asp:ListBox ID="ListBox1" Width="100%" Height="300px" runat="server" AutoPostBack="True"></asp:ListBox> </div> <div id="divRightButtons" style="width:80%;float:left;" runat="server" > </div> </form></body></html>
后置文件:
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Collections.Generic;public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { BuildButtons(); } private void BuildButtons() { Table table1 = new Table(); for (int i = 1; i <= 10; i++) { TableRow tr = new TableRow(); for (int j = 1; j <= 10; j++) { TableCell td = new TableCell(); Button btn = new Button(); btn.ID = "btn" + SetButtonNum( i, j); btn.Text = btn.ID; btn.TabIndex =(short) ( (i - 1) * 10 + j ); btn.Click += Button_Public_Click; td.Controls.Add(btn); tr.Controls.Add(td); } table1.Controls.Add(tr); } divRightButtons.Controls.Add(table1); } private string SetButtonNum(int i, int j) { int num = (i - 1) * 10 + j; if (num < 10) return "00" + num.ToString(); if (num < 100) return "0" + num.ToString(); return num.ToString(); } protected void Button_Public_Click(object sender, EventArgs e) { //点击了的buttonid显示在listbox内。 Button btn = (Button) sender; string btnID = btn.ID; int btnNum =Convert.ToInt32( btnID.Substring(3) ); int sameRowMin = Convert.ToInt32(btnID.Substring(3,2)+"1"); int sameRowMax = sameRowMin + 9; //每行10个,最多只能选择5个,并且要同一行相邻的5个。 //同一行的选中按钮集合 List<string> listSameRowBtnID = new List<string>(); List<int> listSameRowNum = new List<int>(); //所有的选中按钮 List<string> listAllBtnID = new List<string>(); foreach (ListItem li in ListBox1.Items) { listAllBtnID.Add(li.Text); int btnNum2 = Convert.ToInt32(li.Text.Substring(3)); if (btnNum2 >= sameRowMin && btnNum2 <= sameRowMax && li.Text != btnID) { listSameRowBtnID.Add(li.Text); listSameRowNum.Add(btnNum2); } } if (listAllBtnID.Contains(btnID)) { Response.Write("已经加入了此按钮,不能重复加入!"); } else if (listSameRowBtnID.Count >= 5) { Response.Write("此行已加入了5个,不能再加入!"); } else if (listSameRowBtnID.Count > 0 && btnNum != listSameRowNum[0]-1 && btnNum != listSameRowNum[listSameRowNum.Count-1]+1 ) { Response.Write("每一行中的按钮在添加时,必须是紧邻的!"); } else { ListBox1.Items.Add(new ListItem(btnID)); } }}
- 页面有100个button,一个listbox。动态产生控件及一些逻辑控制
- 使用HtmlGenericControl控件动态产生一些HtmlControls
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValida
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- [转]ASP.NET一个页面多个Button按钮事件避免数据验证控件冲突方法
- 动态添加 button 控件及事件
- 动态添加 button 控件及事件
- WPF中ListBox的item中有Button或其他控件时点击事件的处理
- 3个按钮选一个 颜色产生变化 android button
- 统计一个页面有相同属性的控件有多少个
- MFC-combox和listbox、radio button、group box等控件及onTimer函数的应用
- listbox控件的一些操作
- listbox控件的一些操作
- listbox控件的一些操作
- listbox控件的一些操作
- JSF 初始化 页面数据的方法
- 看懂信息检索和网络数据挖掘领域论文的必备知识总结
- fdisk -l 和more /proc/partitions区别
- php5.4+xdebug
- linux程序分析工具介绍(一)—-”/proc”
- 页面有100个button,一个listbox。动态产生控件及一些逻辑控制
- 巧用同余定理
- windows操作系统的自启动项
- 福音!JS脚本可视化调试支持——基于Google v8引擎的脚本调试
- c# 标准控件 缩写
- Small World: 精简向量运算库
- HDOJ 2072
- JMAP、jstat命令输出结果说明
- Keil MDK 编译出现的警告和错误解决