页面有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));        }    }}



原创粉丝点击